1Mojo::Log(3) User Contributed Perl Documentation Mojo::Log(3)
2
3
4
6 Mojo::Log - Simple logger
7
9 use Mojo::Log;
10
11 # Log to STDERR
12 my $log = Mojo::Log->new;
13
14 # Customize log file location and minimum log level
15 my $log = Mojo::Log->new(path => '/var/log/mojo.log', level => 'warn');
16
17 # Log messages
18 $log->trace('Doing stuff');
19 $log->debug('Not sure what is happening here');
20 $log->info('FYI: it happened again');
21 $log->warn('This might be a problem');
22 $log->error('Garden variety error');
23 $log->fatal('Boom');
24
26 Mojo::Log is a simple logger for Mojo projects.
27
29 Mojo::Log inherits all events from Mojo::EventEmitter and can emit the
30 following new ones.
31
32 message
33 $log->on(message => sub ($log, $level, @lines) {...});
34
35 Emitted when a new message gets logged.
36
37 $log->on(message => sub ($log, $level, @lines) { say "$level: ", @lines });
38
40 Mojo::Log implements the following attributes.
41
42 color
43 my $bool = $log->color;
44 $log = $log->color($bool);
45
46 Colorize log messages with the levels "warn", "error" and "fatal" using
47 Term::ANSIColor, defaults to the value of the "MOJO_LOG_COLOR"
48 environment variables. Note that this attribute is EXPERIMENTAL and
49 might change without warning!
50
51 format
52 my $cb = $log->format;
53 $log = $log->format(sub {...});
54
55 A callback for formatting log messages.
56
57 $log->format(sub ($time, $level, @lines) { "[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n" });
58
59 handle
60 my $handle = $log->handle;
61 $log = $log->handle(IO::Handle->new);
62
63 Log filehandle used by default "message" event, defaults to opening
64 "path" or "STDERR".
65
66 history
67 my $history = $log->history;
68 $log = $log->history([[time, 'debug', 'That went wrong']]);
69
70 The last few logged messages.
71
72 level
73 my $level = $log->level;
74 $log = $log->level('debug');
75
76 Active log level, defaults to "trace". Available log levels are
77 "trace", "debug", "info", "warn", "error" and "fatal", in that order.
78
79 max_history_size
80 my $size = $log->max_history_size;
81 $log = $log->max_history_size(5);
82
83 Maximum number of logged messages to store in "history", defaults to
84 10.
85
86 path
87 my $path = $log->path
88 $log = $log->path('/var/log/mojo.log');
89
90 Log file path used by "handle".
91
92 short
93 my $bool = $log->short;
94 $log = $log->short($bool);
95
96 Generate short log messages without a timestamp but with journald log
97 level prefix, suitable for systemd environments, defaults to the value
98 of the "MOJO_LOG_SHORT" environment variables.
99
101 Mojo::Log inherits all methods from Mojo::EventEmitter and implements
102 the following new ones.
103
104 append
105 $log->append("[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n");
106
107 Append message to "handle".
108
109 capture
110 my $messages = $log->capture;
111 my $messages = $log->capture('debug');
112
113 Capture log messages for as long as the returned object exists, useful
114 for testing log messages.
115
116 # Test your log messages
117 my $messages = $log->capture('trace');
118 $log->fatal('Something very bad happened');
119 $log->trace('Just some debug information');
120 like $messages, qr/Something very bad happened/, 'logs contain fatal message';
121 like $messages->[-1], qr/Just some debug information/, 'trace message was last';
122 undef $messages;
123
124 context
125 my $new = $log->context('[extra]', '[information]');
126
127 Construct a new child Mojo::Log object that will include context
128 information with every log message.
129
130 # Log with context
131 my $log = Mojo::Log->new;
132 my $context = $log->context('[17a60115]');
133 $context->debug('This is a log message with context information');
134 $context->info('And another');
135
136 debug
137 $log = $log->debug('You screwed up, but that is ok');
138 $log = $log->debug('All', 'cool');
139 $log = $log->debug(sub {...});
140
141 Emit "message" event and log "debug" message.
142
143 error
144 $log = $log->error('You really screwed up this time');
145 $log = $log->error('Wow', 'seriously');
146 $log = $log->error(sub {...});
147
148 Emit "message" event and log "error" message.
149
150 fatal
151 $log = $log->fatal('Its over...');
152 $log = $log->fatal('Bye', 'bye');
153 $log = $log->fatal(sub {...});
154
155 Emit "message" event and log "fatal" message.
156
157 info
158 $log = $log->info('You are bad, but you prolly know already');
159 $log = $log->info('Ok', 'then');
160 $log = $log->info(sub {...});
161
162 Emit "message" event and log "info" message.
163
164 is_level
165 my $bool = $log->is_level('debug');
166
167 Check active log "level".
168
169 # True
170 $log->level('debug')->is_level('debug');
171 $log->level('debug')->is_level('info');
172
173 # False
174 $log->level('info')->is_level('debug');
175 $log->level('fatal')->is_level('warn');
176
177 new
178 my $log = Mojo::Log->new;
179 my $log = Mojo::Log->new(level => 'warn');
180 my $log = Mojo::Log->new({level => 'warn'});
181
182 Construct a new Mojo::Log object and subscribe to "message" event with
183 default logger.
184
185 trace
186 $log = $log->trace('Whatever');
187 $log = $log->trace('Who', 'cares');
188 $log = $log->trace(sub {...});
189
190 Emit "message" event and log "trace" message.
191
192 warn
193 $log = $log->warn('Dont do that Dave...');
194 $log = $log->warn('No', 'really');
195 $log = $log->warn(sub {...});
196
197 Emit "message" event and log "warn" message.
198
200 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
201
202
203
204perl v5.38.0 2023-09-11 Mojo::Log(3)