<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Mojo::Log;
use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
use Mojo::Util 'encode';

has handle =&gt; sub {

  # File
  if (my $path = shift-&gt;path) {
    croak qq{Can't open log file "$path": $!}
      unless open my $file, '&gt;&gt;', $path;
    return $file;
  }

  # STDERR
  return \*STDERR;
};
has level =&gt; 'debug';
has 'path';

# Supported log level
my $LEVEL = {debug =&gt; 1, info =&gt; 2, warn =&gt; 3, error =&gt; 4, fatal =&gt; 5};

sub new {
  my $self = shift-&gt;SUPER::new(@_);
  $self-&gt;on(message =&gt; \&amp;_message);
  return $self;
}

sub debug { shift-&gt;log(debug =&gt; @_) }
sub error { shift-&gt;log(error =&gt; @_) }
sub fatal { shift-&gt;log(fatal =&gt; @_) }

sub format {
  my ($self, $level, @lines) = @_;
  return encode 'UTF-8',
    '[' . localtime(time) . "] [$level] " . join("\n", @lines, '');
}

sub info { shift-&gt;log(info =&gt; @_) }

sub is_debug { shift-&gt;is_level('debug') }
sub is_error { shift-&gt;is_level('error') }
sub is_fatal { shift-&gt;is_level('fatal') }
sub is_info  { shift-&gt;is_level('info') }

sub is_level {
  my ($self, $level) = @_;
  return $LEVEL-&gt;{lc $level} &gt;= $LEVEL-&gt;{$ENV{MOJO_LOG_LEVEL} || $self-&gt;level};
}

sub is_warn { shift-&gt;is_level('warn') }

sub log { shift-&gt;emit('message', lc(shift), @_) }

sub warn { shift-&gt;log(warn =&gt; @_) }

sub _message {
  my ($self, $level) = (shift, shift);

  return unless $self-&gt;is_level($level) &amp;&amp; (my $handle = $self-&gt;handle);

  flock $handle, LOCK_EX;
  $handle-&gt;print($self-&gt;format($level, @_)) or croak "Can't write to log: $!";
  flock $handle, LOCK_UN;
}

1;

=encoding utf8

=head1 NAME

Mojo::Log - Simple logger

=head1 SYNOPSIS

  use Mojo::Log;

  # Log to STDERR
  my $log = Mojo::Log-&gt;new;

  # Customize log file location and minimum log level
  my $log = Mojo::Log-&gt;new(path =&gt; '/var/log/mojo.log', level =&gt; 'warn');

  # Log messages
  $log-&gt;debug('Why is this not working?');
  $log-&gt;info('FYI: it happened again.');
  $log-&gt;warn('This might be a problem.');
  $log-&gt;error('Garden variety error.');
  $log-&gt;fatal('Boom!');

=head1 DESCRIPTION

L&lt;Mojo::Log&gt; is a simple logger for L&lt;Mojo&gt; projects.

=head1 EVENTS

L&lt;Mojo::Log&gt; inherits all events from L&lt;Mojo::EventEmitter&gt; and can emit the
following new ones.

=head2 message

  $log-&gt;on(message =&gt; sub {
    my ($log, $level, @lines) = @_;
    ...
  });

Emitted when a new message gets logged.

  $log-&gt;unsubscribe('message');
  $log-&gt;on(message =&gt; sub {
    my ($log, $level, @lines) = @_;
    say "$level: ", @lines;
  });

=head1 ATTRIBUTES

L&lt;Mojo::Log&gt; implements the following attributes.

=head2 handle

  my $handle = $log-&gt;handle;
  $log       = $log-&gt;handle(IO::Handle-&gt;new);

Log filehandle used by default L&lt;/"message"&gt; event, defaults to opening
L&lt;/"path"&gt; or C&lt;STDERR&gt;.

=head2 level

  my $level = $log-&gt;level;
  $log      = $log-&gt;level('debug');

Active log level, defaults to C&lt;debug&gt;. Available log levels are C&lt;debug&gt;,
C&lt;info&gt;, C&lt;warn&gt;, C&lt;error&gt; and C&lt;fatal&gt;, in that order. Note that the
MOJO_LOG_LEVEL environment variable can override this value.

=head2 path

  my $path = $log-&gt;path
  $log     = $log-&gt;path('/var/log/mojo.log');

Log file path used by L&lt;/"handle"&gt;.

=head1 METHODS

L&lt;Mojo::Log&gt; inherits all methods from L&lt;Mojo::EventEmitter&gt; and implements
the following new ones.

=head2 new

  my $log = Mojo::Log-&gt;new;

Construct a new L&lt;Mojo::Log&gt; object and subscribe to L&lt;/"message"&gt; event with
default logger.

=head2 debug

  $log = $log-&gt;debug('You screwed up, but that is ok.');
  $log = $log-&gt;debug('All', 'cool!');

Log debug message.

=head2 error

  $log = $log-&gt;error('You really screwed up this time.');
  $log = $log-&gt;error('Wow', 'seriously!');

Log error message.

=head2 fatal

  $log = $log-&gt;fatal('Its over...');
  $log = $log-&gt;fatal('Bye', 'bye!');

Log fatal message.

=head2 format

  my $msg = $log-&gt;format(debug =&gt; 'Hi there!');
  my $msg = $log-&gt;format(debug =&gt; 'Hi', 'there!');

Format log message.

=head2 info

  $log = $log-&gt;info('You are bad, but you prolly know already.');
  $log = $log-&gt;info('Ok', 'then!');

Log info message.

=head2 is_level

  my $bool = $log-&gt;is_level('debug');

Check log level.

=head2 is_debug

  my $bool = $log-&gt;is_debug;

Check for debug log level.

=head2 is_error

  my $bool = $log-&gt;is_error;

Check for error log level.

=head2 is_fatal

  my $bool = $log-&gt;is_fatal;

Check for fatal log level.

=head2 is_info

  my $bool = $log-&gt;is_info;

Check for info log level.

=head2 is_warn

  my $bool = $log-&gt;is_warn;

Check for warn log level.

=head2 log

  $log = $log-&gt;log(debug =&gt; 'This should work.');
  $log = $log-&gt;log(debug =&gt; 'This', 'too!');

Emit L&lt;/"message"&gt; event.

=head2 warn

  $log = $log-&gt;warn('Dont do that Dave...');
  $log = $log-&gt;warn('No', 'really!');

Log warn message.

=head1 SEE ALSO

L&lt;Mojolicious&gt;, L&lt;Mojolicious::Guides&gt;, L&lt;http://mojolicio.us&gt;.

=cut
</pre></body></html>