Acme-CPANModulesBundle-Import-PerlDancerAdvent-2018
    
    
  
  
  
view release on metacpan or search on metacpan
devdata/http_advent.perldancer.org_2018_23 view on Meta::CPAN
it on by setting your logger in your environment config file:</p>
<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>
<pre class="prettyprint"># config.yml (this is the default)
engines:
  logger:
    Console::Colored:
      log_format: "[%a:%P] %L @%T> %m in %f l. %l"</pre>
devdata/http_advent.perldancer.org_2018_23 view on Meta::CPAN
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>
<p>If you want to highlight an entire log message in a specific color, you can set a pattern that captures
everything. However, this works on the message only, and not on the entire formatted line of log output.
(Patches welcome!)</p>
<pre class="prettyprint"># config.yml
engines:
  logger:
    Console::Colored:
( run in 1.920 second using v1.01-cache-2.11-cpan-c333fce770f )