Acme-CPANModulesBundle-Import-PerlDancerAdvent-2018
view release on metacpan or search on metacpan
devdata/http_advent.perldancer.org_2018_19 view on Meta::CPAN
<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">/* <![CDATA[ */
!window.jQuery && document.write('<script src="/javascripts/jquery.js"><\/script>')
/* ]]> */</script>
<!-- Prettyfy -->
<link href="/css/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="/javascripts/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<div id="page">
<div id="sidebar">
<a href="/" class="homelink">Dancer Advent Calendar</a><br />
<p>
The PerlDancer Advent Calendar is a community-driven project that aims
to showcase the Dancer Perl web framework.
</p>
<p>
Each day of December until Christmas, one article about Dancer. Stay tuned for new moves!
</p>
<ul id="sidebar-items">
<li>
<h3>About Dancer</h3>
<ul class="links">
<li><a href="http://www.perldancer.org/">Dancer homepage</a></li>
<li><a href="http://twitter.com/PerlDancer">Official Twitter</a></li>
<li><a href="http://github.com/PerlDancer/Dancer">Dancer on GitHub</a></li>
<li><a href="http://github.com/PerlDancer/Dancer2">Dancer 2 on GitHub</a></li>
<li><a class="feed" href="/feed/2018">RSS</a></li>
</ul>
</li>
</ul>
</div>
<div id="content">
<div class="pod-document"><h1><a name="logging_with_dancer2__logger__log4perl"></a>Logging with Dancer2::Logger::Log4perl</h1>
<h2><a name="history"></a>History</h2>
<p>It's late summer 2018, and there has been a hole in the logging ecosystem of Dancer2 for some time
now - we've been missing a Log4perl plugin. There are certainly a lot of great logging options
available (<code>Dancer2::Logger::LogAny</code>, <code>Dancer2::Logger::Syslog</code>, and <code>Dancer2::Logger::LogReport</code>
to name a few), and a couple of them even include appenders for Log4perl. In looking at our own
needs, however, they all seemed to be overkill.</p>
<p>At <code>$work</code>, everything is based on Log4perl (and, in our client-facing Java apps, Log4j), so
there has never been need for another logger. We'd been happily using <code>Dancer::Logger::Log4perl</code>
for years, and wanted to continue to use something familar to us. Before investing some time in
writing our own Log4perl plugin for Dancer2, we expanded our search beyond metacpan, and were
rewarded in doing so.</p>
<p>We stumbled across a project from Ryan Larscheidt and Jon Miner at the University of Wisconsin.
They wrote a Log4perl plugin for Dancer2 with the intent of releasing it, but as their priorities
and projects shifted, it never made its way to CPAN. I used to go to MadMongers (Madison Perl Mongers),
so before long, I was able to track them down and they gave me their blessing to release it.</p>
<p>A little packaging and a few tests later, <code>Dancer2::Logger::Log4perl</code> was on its way to CPAN!</p>
<h2><a name="configuration"></a>Configuration</h2>
<p>First, we need to create a basic Log4perl configuration. Create a new Dancer2 application (I'll call
mine <code>TestLog4perl</code>), and in the application directory create a <i>log4perl.conf</i> file with the following:</p>
<pre class="prettyprint">log4perl.rootLogger = DEBUG, LOG1
log4perl.appender.LOG1 = Log::Log4perl::Appender::File
log4perl.appender.LOG1.filename = logs/mylog.log
log4perl.appender.LOG1.mode = append
log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.LOG1.layout.ConversionPattern = %d %p %m %n</pre>
<p>The first one defines our root application logger. The first parameter after the equals sign says
what is the minimum level we should log. Since we are saying the minimum log level should be <code>DEBUG</code>,
any messages from Dancer2 itself, and anything logged at the <code>core</code> level will be ignored.</p>
<p>After the minimum log level is a comma-separated list of appenders to write to. For now, we will create a
single appender named <code>LOG1</code> (we will see how to add a second appender below). This will write
to a file in the <i>logs/</i> directory named <i>mylog.log</i>, using the <a href="https://metacpan.org/pod/Log::Log4perl::Appender::File">Log::Log4perl::Appender::File</a>
appender. When the app is started, it will append to an existing log file of that name (or create the file
if necessary), and will write to it with a format specified by <a href="https://metacpan.org/pod/Log::Log4perl::Layout::PatternLayout">Log::Log4perl::Layout::PatternLayout</a>.</p>
<p>Each appender can have its own configuration directives. Please consult the pod for each appender for a list
of its configuration parameters.</p>
<p>Next, we have to tell our application that we are using <code>Dancer2::Logger::Log4perl</code> as our preferred logger.
Edit your <i>environments/development.yml</i> file, and comment out the <code>logger: "console"</code> line. Replace it
with the following:</p>
<pre class="prettyprint">logger: log4perl
log: core
engines:
logger:
log4perl:
config_file: log4perl.conf</pre>
<p>This tells Dancer2 to use <code>Dancer2::Logger::Log4perl</code> as its logging engine, and to send all levels of message to
it. Finally, Log4perl should look for its configuration file in the root application directory in a file called
<i>log4perl.conf</i>.</p>
<h2><a name="usage"></a>Usage</h2>
<p>Using Log4perl is simple: use the logging keywords you are already familiar with in Dancer:</p>
<pre class="prettyprint">get '/' => sub {
debug "I'M IN UR INDEX";
template 'index' => { 'title' => 'TestLog4perl' };
};</pre>
<p>Start your application and visit <code>http://localhost:5000/</code>. You will see the following in your <i>logs/mylog.log</i> file:</p>
<pre class="prettyprint">2018/12/18 21:36:02 DEBUG I'M IN UR INDEX</pre>
<h2><a name="hey__i_can_t_see_my_log_messages_on_the_screen_"></a>Hey, I can't see my log messages on the screen!</h2>
<p>That's because we didn't add a screen appender! With Log4perl, adding another appender is easy. Let's
add another section to our <i>log4perl.conf</i> file:</p>
<pre class="prettyprint">log4perl.appender.SCREEN = Log::Log4perl::Appender::Screen
log4perl.appender.SCREEN.stderr = 0
log4perl.appender.SCREEN.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.SCREEN.layout.ConversionPattern = %m %n</pre>
<p>This creates another appender named <code>SCREEN</code>. We then need to tell our root logger to use this appender
as well:</p>
<pre class="prettyprint">log4perl.rootLogger = DEBUG, LOG1, SCREEN</pre>
( run in 1.330 second using v1.01-cache-2.11-cpan-39bf76dae61 )