| 1 | package Mojo::Log; | ||||||||||||||||||||
| 2 | 21 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 21 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 21 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 21 | ▶ | use Mojo::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 21 | ▶ | use Mojo::Util qw(encode); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 21 | ▶ | use Term::ANSIColor qw(colored); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 21 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 15 | ▶ | has color => sub { $ENV{MOJO_LOG_COLOR} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 17 | ▶ | has format => sub { $_[0]->short ? \&_short : $_[0]->color ? \&_color : \&_default }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||||||||
| 13 | 6 | ▶ | has handle => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # STDERR | ||||||||||||||||||||
| 16 | 6 | ▶ | return \*STDERR unless my $path = shift->path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # File | ||||||||||||||||||||
| 19 | 2 | return Mojo::File->new($path)->open('>>'); | |||||||||||||||||||
| 20 | 21 | }; | |||||||||||||||||||
| 21 | 9 | ▶ | has history => sub { [] }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 21 | has level => 'trace'; | |||||||||||||||||||
| 23 | 21 | has max_history_size => 10; | |||||||||||||||||||
| 24 | 21 | has 'path'; | |||||||||||||||||||
| 25 | 16 | ▶ | has short => sub { $ENV{MOJO_LOG_SHORT} }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # Supported log levels | ||||||||||||||||||||
| 28 | 21 | my %LEVEL = (trace => 1, debug => 2, info => 3, warn => 4, error => 5, fatal => 6); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Systemd magic numbers | ||||||||||||||||||||
| 31 | 21 | my %MAGIC = (trace => 7, debug => 6, info => 5, warn => 4, error => 3, fatal => 2); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Colors | ||||||||||||||||||||
| 34 | 21 | my %COLORS = (warn => ['yellow'], error => ['red'], fatal => ['white on_red']); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 14 | ▶ | sub append { | ||||||||||||||||||
|
Subroutine
append: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 37 | 14 | my ($self, $msg) = @_; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 14 | ▶ | return unless my $handle = $self->handle; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 14 | flock $handle, LOCK_EX; | |||||||||||||||||||
| 41 | 14 | ▶ | $handle->print(encode('UTF-8', $msg)) or croak "Can't write to log: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 14 | flock $handle, LOCK_UN; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 16 | ▶ | sub capture { | ||||||||||||||||||
|
Subroutine
capture: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 46 | 16 | my ($self, $level) = @_; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 16 | ▶ | croak 'Log messages are already being captured' if $self->{capturing}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 15 | my $original = $self->level; | |||||||||||||||||||
| 51 | 15 | ▶ | $self->level($level || $original); | ||||||||||||||||||
|
Condition66%2/3
$level || $original
MC/DC0%0/2
$level || $original
$level
$original
Truth table66%2/3
A || B
A= $levelB= $original
| |||||||||||||||||||||
| 52 | 15 | my $subscribers = $self->subscribers('message'); | |||||||||||||||||||
| 53 | 15 | $self->unsubscribe('message'); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 15 | ▶ | my $capture = Mojo::Log::_Capture->new(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 56 | 15 | delete $self->level($original)->unsubscribe('message')->{capturing}; | |||||||||||||||||||
| 57 | 15 | $self->on(message => $_) for @$subscribers; | |||||||||||||||||||
| 58 | 15 | }); | |||||||||||||||||||
| 59 | 15 | my $messages = $capture->{messages}; | |||||||||||||||||||
| 60 | $self->on( | ||||||||||||||||||||
| 61 | 48 | ▶ | message => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 48 | my $self = shift; | |||||||||||||||||||
| 63 | 48 | push @$messages, $self->format->(time, @_); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | 15 | ); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 15 | return $capture; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 165 | ▶ | sub context { | ||||||||||||||||||
|
Subroutine
context: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 71 | 165 | my ($self, @context) = @_; | |||||||||||||||||||
| 72 | 165 | ▶ | return $self->new(parent => $self->{parent} || $self, context => \@context, level => $self->level); | ||||||||||||||||||
|
Condition66%2/3
$self->{'parent'} || $self
MC/DC0%0/2
$self->{'parent'} || $self
$self->{'parent'}
$self
Truth table66%2/3
A || B
A= $self->{'parent'}B= $self
| |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 10 | ▶ | sub debug { 2 >= $LEVEL{$_[0]->level} ? _log(@_, 'debug') : $_[0] } | ||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 34 | ▶ | sub error { 5 >= $LEVEL{$_[0]->level} ? _log(@_, 'error') : $_[0] } | ||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 8 | ▶ | sub fatal { 6 >= $LEVEL{$_[0]->level} ? _log(@_, 'fatal') : $_[0] } | ||||||||||||||||||
|
Subroutine
fatal: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 2 | ▶ | sub info { 3 >= $LEVEL{$_[0]->level} ? _log(@_, 'info') : $_[0] } | ||||||||||||||||||
|
Subroutine
info: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 30 | ▶ | sub is_level { $LEVEL{pop()} >= $LEVEL{shift->level} } | ||||||||||||||||||
|
Subroutine
is_level: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 230 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 84 | 230 | my $self = shift->SUPER::new(@_); | |||||||||||||||||||
| 85 | 230 | $self->on(message => \&_message); | |||||||||||||||||||
| 86 | 230 | return $self; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 558 | ▶ | sub trace { 1 >= $LEVEL{$_[0]->level} ? _log(@_, 'trace') : $_[0] } | ||||||||||||||||||
|
Subroutine
trace: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 2 | ▶ | sub warn { 4 >= $LEVEL{$_[0]->level} ? _log(@_, 'warn') : $_[0] } | ||||||||||||||||||
|
Subroutine
warn: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 6 | ▶ | sub _color { | ||||||||||||||||||
|
Subroutine
_color: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 93 | 6 | my $msg = _default(shift, my $level = shift, @_); | |||||||||||||||||||
| 94 | 6 | ▶ | return $COLORS{$level} ? colored($COLORS{$level}, $msg) : $msg; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 71 | ▶ | sub _default { | ||||||||||||||||||
|
Subroutine
_default: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 98 | 71 | my ($time, $level) = (shift, shift); | |||||||||||||||||||
| 99 | 71 | my ($s, $m, $h, $day, $month, $year) = localtime $time; | |||||||||||||||||||
| 100 | 71 | ▶ | $time = sprintf '%04d-%02d-%02d %02d:%02d:%08.5f', $year + 1900, $month + 1, $day, $h, $m, | ||||||||||||||||||
|
Condition100%2/2
(split(/\./u, $time, 0))[1] // 0
MC/DC100%1/1
(split(/\./u, $time, 0))[1] // 0
(split(/\./u, $time, 0))[1]
Truth table100%2/2
A
A= (split(/\./u, $time, 0))[1]
| |||||||||||||||||||||
| 101 | "$s." . ((split /\./, $time)[1] // 0); | ||||||||||||||||||||
| 102 | 71 | return "[$time] [$$] [$level] " . join(' ', @_) . "\n"; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 77 | ▶ | sub _log { | ||||||||||||||||||
|
Subroutine
_log: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 106 | 77 | my ($self, $level) = (shift, pop); | |||||||||||||||||||
| 107 | 77 | ▶ | my @msgs = ref $_[0] eq 'CODE' ? $_[0]() : @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 77 | ▶ | unshift @msgs, @{$self->{context}} if $self->{context}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 77 | ▶ | ($self->{parent} || $self)->emit('message', $level, @msgs); | ||||||||||||||||||
|
Condition66%2/3
$self->{'parent'} || $self
MC/DC0%0/2
$self->{'parent'} || $self
$self->{'parent'}
$self
Truth table66%2/3
A || B
A= $self->{'parent'}B= $self
| |||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 14 | ▶ | sub _message { | ||||||||||||||||||
|
Subroutine
_message: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 113 | 14 | my ($self, $level) = (shift, shift); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 14 | my $max = $self->max_history_size; | |||||||||||||||||||
| 116 | 14 | my $history = $self->history; | |||||||||||||||||||
| 117 | 14 | push @$history, my $msg = [time, $level, @_]; | |||||||||||||||||||
| 118 | 14 | ▶ | shift @$history while @$history > $max; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 14 | $self->append($self->format->(@$msg)); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 10 | ▶ | sub _short { | ||||||||||||||||||
|
Subroutine
_short: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 124 | 10 | my ($time, $level) = (shift, shift); | |||||||||||||||||||
| 125 | 10 | my ($magic, $short) = ("<$MAGIC{$level}>", substr($level, 0, 1)); | |||||||||||||||||||
| 126 | 10 | return "${magic}[$$] [$short] " . join(' ', @_) . "\n"; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | package Mojo::Log::_Capture; | ||||||||||||||||||||
| 130 | 21 | ▶ | use Mojo::Base -base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 131 | use overload | ||||||||||||||||||||
| 132 | 0 | ▶ | bool => sub {1}, | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 133 | 7 | ▶ | '@{}' => sub { shift->{messages} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 134 | 23 | ▶ | '""' => sub { join '', @{shift->{messages}} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 135 | 21 | fallback => 1; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 21 | ▶ | use Mojo::Util qw(scope_guard); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 15 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 140 | 15 | my ($class, $cb) = @_; | |||||||||||||||||||
| 141 | 15 | return $class->SUPER::new(guard => scope_guard($cb), messages => []); | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 21 | 1; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =encoding utf8 | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head1 NAME | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Mojo::Log - Simple logger | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head1 SYNOPSIS | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | use Mojo::Log; | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | # Log to STDERR | ||||||||||||||||||||
| 157 | my $log = Mojo::Log->new; | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | # Customize log file location and minimum log level | ||||||||||||||||||||
| 160 | my $log = Mojo::Log->new(path => '/var/log/mojo.log', level => 'warn'); | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | # Log messages | ||||||||||||||||||||
| 163 | $log->trace('Doing stuff'); | ||||||||||||||||||||
| 164 | $log->debug('Not sure what is happening here'); | ||||||||||||||||||||
| 165 | $log->info('FYI: it happened again'); | ||||||||||||||||||||
| 166 | $log->warn('This might be a problem'); | ||||||||||||||||||||
| 167 | $log->error('Garden variety error'); | ||||||||||||||||||||
| 168 | $log->fatal('Boom'); | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head1 DESCRIPTION | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | L<Mojo::Log> is a simple logger for L<Mojo> projects. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =head1 EVENTS | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | L<Mojo::Log> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones. | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head2 message | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | $log->on(message => sub ($log, $level, @lines) {...}); | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Emitted when a new message gets logged. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | $log->on(message => sub ($log, $level, @lines) { say "$level: ", @lines }); | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | L<Mojo::Log> implements the following attributes. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head2 color | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | my $bool = $log->color; | ||||||||||||||||||||
| 193 | $log = $log->color($bool); | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | Colorize log messages with the levels C<warn>, C<error> and C<fatal> using L<Term::ANSIColor>, defaults to the value of | ||||||||||||||||||||
| 196 | the C<MOJO_LOG_COLOR> environment variables. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head2 format | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | my $cb = $log->format; | ||||||||||||||||||||
| 201 | $log = $log->format(sub {...}); | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | A callback for formatting log messages. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | $log->format(sub ($time, $level, @lines) { "[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n" }); | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head2 handle | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | my $handle = $log->handle; | ||||||||||||||||||||
| 210 | $log = $log->handle(IO::Handle->new); | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | Log filehandle used by default L</"message"> event, defaults to opening L</"path"> or C<STDERR>. | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | =head2 history | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | my $history = $log->history; | ||||||||||||||||||||
| 217 | $log = $log->history([[time, 'debug', 'That went wrong']]); | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | The last few logged messages. | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =head2 level | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | my $level = $log->level; | ||||||||||||||||||||
| 224 | $log = $log->level('debug'); | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | Active log level, defaults to C<trace>. Available log levels are C<trace>, C<debug>, C<info>, C<warn>, C<error> and | ||||||||||||||||||||
| 227 | C<fatal>, in that order. | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =head2 max_history_size | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | my $size = $log->max_history_size; | ||||||||||||||||||||
| 232 | $log = $log->max_history_size(5); | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Maximum number of logged messages to store in L</"history">, defaults to C<10>. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | =head2 path | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | my $path = $log->path | ||||||||||||||||||||
| 239 | $log = $log->path('/var/log/mojo.log'); | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | Log file path used by L</"handle">. | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | =head2 short | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | my $bool = $log->short; | ||||||||||||||||||||
| 246 | $log = $log->short($bool); | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | Generate short log messages without a timestamp but with journald log level prefix, suitable for systemd environments, | ||||||||||||||||||||
| 249 | defaults to the value of the C<MOJO_LOG_SHORT> environment variables. | ||||||||||||||||||||
| 250 | |||||||||||||||||||||
| 251 | =head1 METHODS | ||||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | L<Mojo::Log> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones. | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | =head2 append | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | $log->append("[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n"); | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | Append message to L</"handle">. | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | =head2 capture | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | my $messages = $log->capture; | ||||||||||||||||||||
| 264 | my $messages = $log->capture('debug'); | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | Capture log messages for as long as the returned object exists, useful for testing log messages. | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | # Test your log messages | ||||||||||||||||||||
| 269 | my $messages = $log->capture('trace'); | ||||||||||||||||||||
| 270 | $log->fatal('Something very bad happened'); | ||||||||||||||||||||
| 271 | $log->trace('Just some debug information'); | ||||||||||||||||||||
| 272 | like $messages, qr/Something very bad happened/, 'logs contain fatal message'; | ||||||||||||||||||||
| 273 | like $messages->[-1], qr/Just some debug information/, 'trace message was last'; | ||||||||||||||||||||
| 274 | undef $messages; | ||||||||||||||||||||
| 275 | |||||||||||||||||||||
| 276 | =head2 context | ||||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | my $new = $log->context('[extra]', '[information]'); | ||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | Construct a new child L<Mojo::Log> object that will include context information with every log message. | ||||||||||||||||||||
| 281 | |||||||||||||||||||||
| 282 | # Log with context | ||||||||||||||||||||
| 283 | my $log = Mojo::Log->new; | ||||||||||||||||||||
| 284 | my $context = $log->context('[17a60115]'); | ||||||||||||||||||||
| 285 | $context->debug('This is a log message with context information'); | ||||||||||||||||||||
| 286 | $context->info('And another'); | ||||||||||||||||||||
| 287 | |||||||||||||||||||||
| 288 | =head2 debug | ||||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | $log = $log->debug('You screwed up, but that is ok'); | ||||||||||||||||||||
| 291 | $log = $log->debug('All', 'cool'); | ||||||||||||||||||||
| 292 | $log = $log->debug(sub {...}); | ||||||||||||||||||||
| 293 | |||||||||||||||||||||
| 294 | Emit L</"message"> event and log C<debug> message. | ||||||||||||||||||||
| 295 | |||||||||||||||||||||
| 296 | =head2 error | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | $log = $log->error('You really screwed up this time'); | ||||||||||||||||||||
| 299 | $log = $log->error('Wow', 'seriously'); | ||||||||||||||||||||
| 300 | $log = $log->error(sub {...}); | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | Emit L</"message"> event and log C<error> message. | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | =head2 fatal | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | $log = $log->fatal('Its over...'); | ||||||||||||||||||||
| 307 | $log = $log->fatal('Bye', 'bye'); | ||||||||||||||||||||
| 308 | $log = $log->fatal(sub {...}); | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | Emit L</"message"> event and log C<fatal> message. | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | =head2 info | ||||||||||||||||||||
| 313 | |||||||||||||||||||||
| 314 | $log = $log->info('You are bad, but you prolly know already'); | ||||||||||||||||||||
| 315 | $log = $log->info('Ok', 'then'); | ||||||||||||||||||||
| 316 | $log = $log->info(sub {...}); | ||||||||||||||||||||
| 317 | |||||||||||||||||||||
| 318 | Emit L</"message"> event and log C<info> message. | ||||||||||||||||||||
| 319 | |||||||||||||||||||||
| 320 | =head2 is_level | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | my $bool = $log->is_level('debug'); | ||||||||||||||||||||
| 323 | |||||||||||||||||||||
| 324 | Check active log L</"level">. | ||||||||||||||||||||
| 325 | |||||||||||||||||||||
| 326 | # True | ||||||||||||||||||||
| 327 | $log->level('debug')->is_level('debug'); | ||||||||||||||||||||
| 328 | $log->level('debug')->is_level('info'); | ||||||||||||||||||||
| 329 | |||||||||||||||||||||
| 330 | # False | ||||||||||||||||||||
| 331 | $log->level('info')->is_level('debug'); | ||||||||||||||||||||
| 332 | $log->level('fatal')->is_level('warn'); | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | =head2 new | ||||||||||||||||||||
| 335 | |||||||||||||||||||||
| 336 | my $log = Mojo::Log->new; | ||||||||||||||||||||
| 337 | my $log = Mojo::Log->new(level => 'warn'); | ||||||||||||||||||||
| 338 | my $log = Mojo::Log->new({level => 'warn'}); | ||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | Construct a new L<Mojo::Log> object and subscribe to L</"message"> event with default logger. | ||||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | =head2 trace | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | $log = $log->trace('Whatever'); | ||||||||||||||||||||
| 345 | $log = $log->trace('Who', 'cares'); | ||||||||||||||||||||
| 346 | $log = $log->trace(sub {...}); | ||||||||||||||||||||
| 347 | |||||||||||||||||||||
| 348 | Emit L</"message"> event and log C<trace> message. | ||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | =head2 warn | ||||||||||||||||||||
| 351 | |||||||||||||||||||||
| 352 | $log = $log->warn('Dont do that Dave...'); | ||||||||||||||||||||
| 353 | $log = $log->warn('No', 'really'); | ||||||||||||||||||||
| 354 | $log = $log->warn(sub {...}); | ||||||||||||||||||||
| 355 | |||||||||||||||||||||
| 356 | Emit L</"message"> event and log C<warn> message. | ||||||||||||||||||||
| 357 | |||||||||||||||||||||
| 358 | =head1 SEE ALSO | ||||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||
| 361 | |||||||||||||||||||||
| 362 | =cut | ||||||||||||||||||||