/usr/share/perl5/Mojo/Log.pm is in libmojolicious-perl 7.59+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | package Mojo::Log;
use Mojo::Base 'Mojo::EventEmitter';
use Carp 'croak';
use Fcntl ':flock';
use Mojo::File;
use Mojo::Util 'encode';
has format => sub { shift->short ? \&_short : \&_default };
has handle => sub {
# STDERR
return \*STDERR unless my $path = shift->path;
# File
return Mojo::File->new($path)->open('>>');
};
has history => sub { [] };
has level => 'debug';
has max_history_size => 10;
has 'path';
has short => sub { $ENV{MOJO_LOG_SHORT} };
# Supported log levels
my %LEVEL = (debug => 1, info => 2, warn => 3, error => 4, fatal => 5);
sub append {
my ($self, $msg) = @_;
return unless my $handle = $self->handle;
flock $handle, LOCK_EX;
$handle->print(encode('UTF-8', $msg)) or croak "Can't write to log: $!";
flock $handle, LOCK_UN;
}
sub debug { shift->_log(debug => @_) }
sub error { shift->_log(error => @_) }
sub fatal { shift->_log(fatal => @_) }
sub info { shift->_log(info => @_) }
sub is_level { $LEVEL{pop()} >= $LEVEL{$ENV{MOJO_LOG_LEVEL} || shift->level} }
sub new {
my $self = shift->SUPER::new(@_);
$self->on(message => \&_message);
return $self;
}
sub warn { shift->_log(warn => @_) }
sub _default {
'[' . localtime(shift) . '] [' . shift() . '] ' . join "\n", @_, '';
}
sub _log { shift->emit('message', shift, @_) }
sub _message {
my ($self, $level) = (shift, shift);
return unless $self->is_level($level);
my $max = $self->max_history_size;
my $history = $self->history;
push @$history, my $msg = [time, $level, @_];
shift @$history while @$history > $max;
$self->append($self->format->(@$msg));
}
sub _short { shift; '[' . shift() . '] ' . join "\n", @_, '' }
1;
=encoding utf8
=head1 NAME
Mojo::Log - Simple logger
=head1 SYNOPSIS
use Mojo::Log;
# Log to STDERR
my $log = Mojo::Log->new;
# Customize log file location and minimum log level
my $log = Mojo::Log->new(path => '/var/log/mojo.log', level => 'warn');
# Log messages
$log->debug('Not sure what is happening here');
$log->info('FYI: it happened again');
$log->warn('This might be a problem');
$log->error('Garden variety error');
$log->fatal('Boom');
=head1 DESCRIPTION
L<Mojo::Log> is a simple logger for L<Mojo> projects.
=head1 EVENTS
L<Mojo::Log> inherits all events from L<Mojo::EventEmitter> and can emit the
following new ones.
=head2 message
$log->on(message => sub {
my ($log, $level, @lines) = @_;
...
});
Emitted when a new message gets logged.
$log->on(message => sub {
my ($log, $level, @lines) = @_;
say "$level: ", @lines;
});
=head1 ATTRIBUTES
L<Mojo::Log> implements the following attributes.
=head2 format
my $cb = $log->format;
$log = $log->format(sub {...});
A callback for formatting log messages.
$log->format(sub {
my ($time, $level, @lines) = @_;
return "[Thu May 15 17:47:04 2014] [info] I ♥ Mojolicious\n";
});
=head2 handle
my $handle = $log->handle;
$log = $log->handle(IO::Handle->new);
Log filehandle used by default L</"message"> event, defaults to opening
L</"path"> or C<STDERR>.
=head2 history
my $history = $log->history;
$log = $log->history([[time, 'debug', 'That went wrong']]);
The last few logged messages.
=head2 level
my $level = $log->level;
$log = $log->level('debug');
Active log level, defaults to C<debug>. Available log levels are C<debug>,
C<info>, C<warn>, C<error> and C<fatal>, in that order. Note that the
C<MOJO_LOG_LEVEL> environment variable can override this value.
=head2 max_history_size
my $size = $log->max_history_size;
$log = $log->max_history_size(5);
Maximum number of logged messages to store in L</"history">, defaults to C<10>.
=head2 path
my $path = $log->path
$log = $log->path('/var/log/mojo.log');
Log file path used by L</"handle">.
=head2 short
my $bool = $log->short;
$log = $log->short($bool);
Generate short log messages without a timestamp, suitable for systemd, defaults
to the value of the C<MOJO_LOG_SHORT> environment variables.
=head1 METHODS
L<Mojo::Log> inherits all methods from L<Mojo::EventEmitter> and implements the
following new ones.
=head2 append
$log->append("[Thu May 15 17:47:04 2014] [info] I ♥ Mojolicious\n");
Append message to L</"handle">.
=head2 debug
$log = $log->debug('You screwed up, but that is ok');
$log = $log->debug('All', 'cool');
Emit L</"message"> event and log C<debug> message.
=head2 error
$log = $log->error('You really screwed up this time');
$log = $log->error('Wow', 'seriously');
Emit L</"message"> event and log C<error> message.
=head2 fatal
$log = $log->fatal('Its over...');
$log = $log->fatal('Bye', 'bye');
Emit L</"message"> event and log C<fatal> message.
=head2 info
$log = $log->info('You are bad, but you prolly know already');
$log = $log->info('Ok', 'then');
Emit L</"message"> event and log C<info> message.
=head2 is_level
my $bool = $log->is_level('debug');
Check active log L</"level">.
# True
$log->level('debug')->is_level('debug');
$log->level('debug')->is_level('info');
# False
$log->level('info')->is_level('debug');
$log->level('fatal')->is_level('warn');
=head2 new
my $log = Mojo::Log->new;
my $log = Mojo::Log->new(level => 'warn');
my $log = Mojo::Log->new({level => 'warn'});
Construct a new L<Mojo::Log> object and subscribe to L</"message"> event with
default logger.
=head2 warn
$log = $log->warn('Dont do that Dave...');
$log = $log->warn('No', 'really');
Emit L</"message"> event and log C<warn> message.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
|