.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Log 3pm" .TH Mojo::Log 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::Log \- Simple logger .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& 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 => \*(Aq/var/log/mojo.log\*(Aq, level => \*(Aqwarn\*(Aq); \& \& # Log messages \& $log\->trace(\*(AqDoing stuff\*(Aq); \& $log\->debug(\*(AqNot sure what is happening here\*(Aq); \& $log\->info(\*(AqFYI: it happened again\*(Aq); \& $log\->warn(\*(AqThis might be a problem\*(Aq); \& $log\->error(\*(AqGarden variety error\*(Aq); \& $log\->fatal(\*(AqBoom\*(Aq); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::Log is a simple logger for Mojo projects. .SH EVENTS .IX Header "EVENTS" Mojo::Log inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS message .IX Subsection "message" .Vb 1 \& $log\->on(message => sub ($log, $level, @lines) {...}); .Ve .PP Emitted when a new message gets logged. .PP .Vb 1 \& $log\->on(message => sub ($log, $level, @lines) { say "$level: ", @lines }); .Ve .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojo::Log implements the following attributes. .SS color .IX Subsection "color" .Vb 2 \& my $bool = $log\->color; \& $log = $log\->color($bool); .Ve .PP Colorize log messages with the levels \f(CW\*(C`warn\*(C'\fR, \f(CW\*(C`error\*(C'\fR and \f(CW\*(C`fatal\*(C'\fR using Term::ANSIColor, defaults to the value of the \f(CW\*(C`MOJO_LOG_COLOR\*(C'\fR environment variables. Note that this attribute is \fBEXPERIMENTAL\fR and might change without warning! .SS format .IX Subsection "format" .Vb 2 \& my $cb = $log\->format; \& $log = $log\->format(sub {...}); .Ve .PP A callback for formatting log messages. .PP .Vb 1 \& $log\->format(sub ($time, $level, @lines) { "[2018\-11\-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\en" }); .Ve .SS handle .IX Subsection "handle" .Vb 2 \& my $handle = $log\->handle; \& $log = $log\->handle(IO::Handle\->new); .Ve .PP Log filehandle used by default "message" event, defaults to opening "path" or \f(CW\*(C`STDERR\*(C'\fR. .SS history .IX Subsection "history" .Vb 2 \& my $history = $log\->history; \& $log = $log\->history([[time, \*(Aqdebug\*(Aq, \*(AqThat went wrong\*(Aq]]); .Ve .PP The last few logged messages. .SS level .IX Subsection "level" .Vb 2 \& my $level = $log\->level; \& $log = $log\->level(\*(Aqdebug\*(Aq); .Ve .PP Active log level, defaults to \f(CW\*(C`trace\*(C'\fR. Available log levels are \f(CW\*(C`trace\*(C'\fR, \f(CW\*(C`debug\*(C'\fR, \f(CW\*(C`info\*(C'\fR, \f(CW\*(C`warn\*(C'\fR, \f(CW\*(C`error\*(C'\fR and \&\f(CW\*(C`fatal\*(C'\fR, in that order. .SS max_history_size .IX Subsection "max_history_size" .Vb 2 \& my $size = $log\->max_history_size; \& $log = $log\->max_history_size(5); .Ve .PP Maximum number of logged messages to store in "history", defaults to \f(CW10\fR. .SS path .IX Subsection "path" .Vb 2 \& my $path = $log\->path \& $log = $log\->path(\*(Aq/var/log/mojo.log\*(Aq); .Ve .PP Log file path used by "handle". .SS short .IX Subsection "short" .Vb 2 \& my $bool = $log\->short; \& $log = $log\->short($bool); .Ve .PP Generate short log messages without a timestamp but with journald log level prefix, suitable for systemd environments, defaults to the value of the \f(CW\*(C`MOJO_LOG_SHORT\*(C'\fR environment variables. .SH METHODS .IX Header "METHODS" Mojo::Log inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS append .IX Subsection "append" .Vb 1 \& $log\->append("[2018\-11\-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\en"); .Ve .PP Append message to "handle". .SS capture .IX Subsection "capture" .Vb 2 \& my $messages = $log\->capture; \& my $messages = $log\->capture(\*(Aqdebug\*(Aq); .Ve .PP Capture log messages for as long as the returned object exists, useful for testing log messages. .PP .Vb 7 \& # Test your log messages \& my $messages = $log\->capture(\*(Aqtrace\*(Aq); \& $log\->fatal(\*(AqSomething very bad happened\*(Aq); \& $log\->trace(\*(AqJust some debug information\*(Aq); \& like $messages, qr/Something very bad happened/, \*(Aqlogs contain fatal message\*(Aq; \& like $messages\->[\-1], qr/Just some debug information/, \*(Aqtrace message was last\*(Aq; \& undef $messages; .Ve .SS context .IX Subsection "context" .Vb 1 \& my $new = $log\->context(\*(Aq[extra]\*(Aq, \*(Aq[information]\*(Aq); .Ve .PP Construct a new child Mojo::Log object that will include context information with every log message. .PP .Vb 5 \& # Log with context \& my $log = Mojo::Log\->new; \& my $context = $log\->context(\*(Aq[17a60115]\*(Aq); \& $context\->debug(\*(AqThis is a log message with context information\*(Aq); \& $context\->info(\*(AqAnd another\*(Aq); .Ve .SS debug .IX Subsection "debug" .Vb 3 \& $log = $log\->debug(\*(AqYou screwed up, but that is ok\*(Aq); \& $log = $log\->debug(\*(AqAll\*(Aq, \*(Aqcool\*(Aq); \& $log = $log\->debug(sub {...}); .Ve .PP Emit "message" event and log \f(CW\*(C`debug\*(C'\fR message. .SS error .IX Subsection "error" .Vb 3 \& $log = $log\->error(\*(AqYou really screwed up this time\*(Aq); \& $log = $log\->error(\*(AqWow\*(Aq, \*(Aqseriously\*(Aq); \& $log = $log\->error(sub {...}); .Ve .PP Emit "message" event and log \f(CW\*(C`error\*(C'\fR message. .SS fatal .IX Subsection "fatal" .Vb 3 \& $log = $log\->fatal(\*(AqIts over...\*(Aq); \& $log = $log\->fatal(\*(AqBye\*(Aq, \*(Aqbye\*(Aq); \& $log = $log\->fatal(sub {...}); .Ve .PP Emit "message" event and log \f(CW\*(C`fatal\*(C'\fR message. .SS info .IX Subsection "info" .Vb 3 \& $log = $log\->info(\*(AqYou are bad, but you prolly know already\*(Aq); \& $log = $log\->info(\*(AqOk\*(Aq, \*(Aqthen\*(Aq); \& $log = $log\->info(sub {...}); .Ve .PP Emit "message" event and log \f(CW\*(C`info\*(C'\fR message. .SS is_level .IX Subsection "is_level" .Vb 1 \& my $bool = $log\->is_level(\*(Aqdebug\*(Aq); .Ve .PP Check active log "level". .PP .Vb 3 \& # True \& $log\->level(\*(Aqdebug\*(Aq)\->is_level(\*(Aqdebug\*(Aq); \& $log\->level(\*(Aqdebug\*(Aq)\->is_level(\*(Aqinfo\*(Aq); \& \& # False \& $log\->level(\*(Aqinfo\*(Aq)\->is_level(\*(Aqdebug\*(Aq); \& $log\->level(\*(Aqfatal\*(Aq)\->is_level(\*(Aqwarn\*(Aq); .Ve .SS new .IX Subsection "new" .Vb 3 \& my $log = Mojo::Log\->new; \& my $log = Mojo::Log\->new(level => \*(Aqwarn\*(Aq); \& my $log = Mojo::Log\->new({level => \*(Aqwarn\*(Aq}); .Ve .PP Construct a new Mojo::Log object and subscribe to "message" event with default logger. .SS trace .IX Subsection "trace" .Vb 3 \& $log = $log\->trace(\*(AqWhatever\*(Aq); \& $log = $log\->trace(\*(AqWho\*(Aq, \*(Aqcares\*(Aq); \& $log = $log\->trace(sub {...}); .Ve .PP Emit "message" event and log \f(CW\*(C`trace\*(C'\fR message. .SS warn .IX Subsection "warn" .Vb 3 \& $log = $log\->warn(\*(AqDont do that Dave...\*(Aq); \& $log = $log\->warn(\*(AqNo\*(Aq, \*(Aqreally\*(Aq); \& $log = $log\->warn(sub {...}); .Ve .PP Emit "message" event and log \f(CW\*(C`warn\*(C'\fR message. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .