Nic Sandfield > Mojar-1.062 > Mojar::Log



Annotate this POD


View/Report Bugs
Module Version: 0.011   Source   Latest Release: Mojar-2.051


Mojar::Log - Simple logger


  use Mojar::Log;

  # Log to STDERR without timestamps
  my $log = Mojar::Log->new(pattern => undef);

  # Customise log file location and timestamp pattern
  my $log = Mojar::Log->new(path => '/tmp/abc.log', pattern => '[%F %X]');

  # Log messages
  $log->info(q{We're charging per character);
  $log->error(q{You won't believe this});
  $log->fatal('OH NOES!');


Mojar::Log is a simple logger extending Mojo::Log. The additions are the ability to set defaults at 'use' time (via a helper) and a more ISO-ish timestamp with support for customising that pattern.


Standard usage is by creating a Log object as in the SYNOPSIS. For shared code, large codebases, or long-running processes, that is the only recommended usage. In standalone classes and scripts it's often more convenient to set the parameters via an implicit helper. In a standalone script this could be done as:

  use Mojar::Log (
    path => '/var/log/something.log',
    level => 'info',
    pattern => '%F %X...'

If the same 'use' is employed in a class then it could be done as:

  package MyClass;
  use Mojar::Log (...);

  my $o = MyClass->new(...);
  $o->log->debug('Go!');  # object method
  MyClass->log->debug('Go!');  # class method

Employing use-time parameters entails the implicit creation of a method (helper) and a hash entry. The helper works by assigning a Log object to the caller's 'log' hash key, so only works with objects implemented as hashrefs. In other words, only use the convenience of the helper if that's what you were going to do anyway. [Using the package name to hold a hashref is considered hacky and makes debugging a little trickier; creating a log object per caller object is inefficient in a lot of cases. If either of those trouble you, please re-read the intro to this section then return to the SYNOPSIS.]

Thanks to Mojolicious being so versatile, you can even use Mojar::Log in those projects, taking advantage of the introduced 'pattern' attribute.

  package MyApp;
  use Mojo::Base 'Mojolicious';
  use Mojar::Log;
  sub startup {
    my $self = shift;
    $self->log(Mojar::Log->new(pattern => '...', path => '...'));

And in a Lite app:

  use Mojolicious::Lite;
  use Mojar::Log;
  app->log(Mojar::Log->new(pattern => '...', path => '...'));

After which you can use the usual Mojolicious 'log' method.


Mojar::Log inherits its attributes from Mojo::Log and adds the following.


  $pattern = $log->pattern;
  $log     = $log->pattern('%Y%m%d %H:%M:%S');

Pattern to use for the timestamp. The default pattern (above) is a fairly minimal 17 characters. The timestamp can be disabled altogether by setting it to 'undef'.

  $log->pattern(undef)->info('Timeless!');  # $log then uses no timestamp

See Time::CTime for the full list of specifiers, but a few common choices are the following.

  $log->pattern('[%FT%X]');  # ISO 8601 timestamp with secs and brackets
  $log->pattern('[%F %R]');  # ISO 8601 timestamp omitting 'T' and secs
  $log->pattern('%y%m%d %H%M%S');  # A more minimal 13 chars

On a linux system, you can test your pattern by calling date.

  date +'%Y.%m.%d %H.%M.%S'


Mojo::Log, the parent class which provides the majority of documentation.

syntax highlighting: