Acme-CPANModulesBundle-Import-PerlDancerAdvent-2018
view release on metacpan or search on metacpan
devdata/http_advent.perldancer.org_2018_16 view on Meta::CPAN
sub job_queue {
return MyJob::JobQueue->new;
}
get '/my/api/route/:guid/:group/:force' => sub {
my $guid = route_parameters->get( 'guid' );
my $group = route_parameters->get( 'group' );
my $force = route_parameters->get( 'force' );
debug "GENERATING XML ONLY FOR $guid";
job_queue->queue_job({
name => "InstantXML",
guid => $guid,
title => "Instant XML Generator",
queue => 'InstantXML',
job_args => [ $self->request_path, $guid, $group, $force ],
});
}</pre>
<h2><a name="creating_and_configuring_the_job_queue_worker"></a>Creating and Configuring the Job Queue Worker</h2>
devdata/http_advent.perldancer.org_2018_19 view on Meta::CPAN
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>
devdata/http_advent.perldancer.org_2018_23 view on Meta::CPAN
<pre class="prettyprint">logger: "Console::Colored"</pre>
<p>Your log will instantly become cheerful and seasonal according to this default configuration:</p>
<pre class="prettyprint"># config.yml (these are the defaults)
engines:
logger:
Console::Colored:
colored_origin: "cyan"
colored_levels:
core: "bold bright_white"
debug: "bold bright_blue"
info: "bold green"
warning: "bold yellow"
error: "bold yellow on_red"
colored_messages:
core: "bold bright_white"
debug: "bold bright_blue"
info: "bold green"
warning: "bold yellow"
error: "bold yellow on_red"</pre>
<img src="/images/2018/23/log-1.png">
<p>The <code>colored_origin</code> refers to the part of the message that shows the package that this
message originated in, as well as the file name and line.</p>
<pre class="prettyprint">>> Dancer2 v0.207000 server 28764 listening on http://0.0.0.0:3000
[main:28764] debug @2018-12-19 20:31:06> Hello World in test.pl l. 6
^^^^ ^^^^^^^ ^</pre>
<p>The <code>colored_levels</code> are the log levels themselves.</p>
<pre class="prettyprint">[main:28764] debug @2018-12-19 20:31:06> Hello World in test.pl l. 6
^^^^^</pre>
<p>And the <code>colored_messages</code> refer to the actual message.</p>
<pre class="prettyprint">[main:28764] debug @2018-12-19 20:31:06> Hello World in test.pl l. 6
^^^^^^^^^^^</pre>
<p>The colors are the same kind of color strings used by <a href="https://metacpan.org/module/Term::ANSIColor">Term::ANSIColor</a>. The first
color refers to the foreground, and the second one to the background. You can add an optional <code>bold</code>.</p>
<pre class="prettyprint">[bold] [foreground] [on_background]</pre>
<h2><a name="changing_the_log_message"></a>Changing the log message</h2>
<p>If you don't like the default log format, you can change it with the <code>log_format</code> setting, which is
documented in detail in <a href="https://metacpan.org/module/Dancer2::Core::Role::Logger">Dancer2::Core::Role::Logger</a>. You can just stick it in with the other configuration.</p>
devdata/http_advent.perldancer.org_2018_23 view on Meta::CPAN
<p>Usually you would run a single-worker server during development, so there is no need for request IDs.
And since the message is now colored, we can also drop the log level. How about a simple format like
the following:</p>
<pre class="prettyprint">%t %f:%l> %m
# 20/Dec/2018 16:41:07 MyApp.pm:22> Hello World</pre>
<h2><a name="i_know_regular_expressions_"></a>I know regular expressions!</h2>
<p>Sometimes just reading the log is not enough. You're looking for a specific piece of information. There
are three pages of log for a single request, and debugging is hard work. Grepping is an option,
but you need to look for a complex pattern. Maybe a product SKU, a date or some other piece of information.</p>
<p>With Dancer2::Logger::Console::Colored you can actually make it come out with a background color so you
can spot it easily, without having to change your code (or your added debugging log statements). Instead,
you can put a regular expression into your configuration file.</p>
<pre class="prettyprint"># config.yml
engines:
logger:
Console::Colored:
colored_regex:
- re: "customer number \d+"
color: "bold red"</pre>
<p>This will find customer numbers according to the pattern and make them bold and red. You can supply
a regular expression pattern and a color setting.</p>
<img src="/images/2018/23/log-2.png">
<p>Note that there is a list under the <code>colored_regex</code> key. This means you can actually have
multiple patterns. Even really simple ones that make <code>print</code> style debugging easy
and attractive.</p>
<pre class="prettyprint"># config.yml
engines:
logger:
Console::Colored:
colored_regex:
- re: "customer number \d+"
color: "bold red"
- re: "\bhere\b"
color: "white on_green"</pre>
devdata/http_advent.perldancer.org_2018_23 view on Meta::CPAN
<pre class="prettyprint"># config.yml
engines:
logger:
Console::Colored:
colored_regex:
- re: ".+here.+"
color: "white on_red"</pre>
<img src="/images/2018/23/log-3.png">
<p>This opens up a lot of possibilities, for debugging as well as for general logging during development.</p>
<h2><a name="what_else"></a>What else?</h2>
<p>There are still some feature ideas in the backlog, but it's already pretty useful. If you would
like to see additional features, please feel free to
[open an issue on github](https://github.com/simbabque/Dancer2-Logger-Console-Colored/issues).</p>
<p>And if you happen to use Mojolicious as well and need something similar, take a look at
<a href="https://metacpan.org/module/Mojo::Log::Colored">Mojo::Log::Colored</a>, which is in the early stages of development, but works quite well.</p>
<h2><a name="author"></a>Author</h2>
<p>This article has been written by Julien Fiegehenn (simbabque) for the Perl Dancer
( run in 1.119 second using v1.01-cache-2.11-cpan-49f99fa48dc )