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. Note that this method is EXPERIMENTAL and
115 might change without warning!
116
117 # Test your log messages
118 my $messages = $log->capture('trace');
119 $log->fatal('Something very bad happened');
120 $log->trace('Just some debug information');
121 like $messages, qr/Something very bad happened/, 'logs contain fatal message';
122 like $messages->[-1], qr/Just some debug information/, 'trace message was last';
123 undef $messages;
124
125 context
126 my $new = $log->context('[extra]', '[information]');
127
128 Construct a new child Mojo::Log object that will include context
129 information with every log message.
130
131 # Log with context
132 my $log = Mojo::Log->new;
133 my $context = $log->context('[17a60115]');
134 $context->debug('This is a log message with context information');
135 $context->info('And another');
136
137 debug
138 $log = $log->debug('You screwed up, but that is ok');
139 $log = $log->debug('All', 'cool');
140 $log = $log->debug(sub {...});
141
142 Emit "message" event and log "debug" message.
143
144 error
145 $log = $log->error('You really screwed up this time');
146 $log = $log->error('Wow', 'seriously');
147 $log = $log->error(sub {...});
148
149 Emit "message" event and log "error" message.
150
151 fatal
152 $log = $log->fatal('Its over...');
153 $log = $log->fatal('Bye', 'bye');
154 $log = $log->fatal(sub {...});
155
156 Emit "message" event and log "fatal" message.
157
158 info
159 $log = $log->info('You are bad, but you prolly know already');
160 $log = $log->info('Ok', 'then');
161 $log = $log->info(sub {...});
162
163 Emit "message" event and log "info" message.
164
165 is_level
166 my $bool = $log->is_level('debug');
167
168 Check active log "level".
169
170 # True
171 $log->level('debug')->is_level('debug');
172 $log->level('debug')->is_level('info');
173
174 # False
175 $log->level('info')->is_level('debug');
176 $log->level('fatal')->is_level('warn');
177
178 new
179 my $log = Mojo::Log->new;
180 my $log = Mojo::Log->new(level => 'warn');
181 my $log = Mojo::Log->new({level => 'warn'});
182
183 Construct a new Mojo::Log object and subscribe to "message" event with
184 default logger.
185
186 trace
187 $log = $log->trace('Whatever');
188 $log = $log->trace('Who', 'cares');
189 $log = $log->trace(sub {...});
190
191 Emit "message" event and log "trace" message.
192
193 warn
194 $log = $log->warn('Dont do that Dave...');
195 $log = $log->warn('No', 'really');
196 $log = $log->warn(sub {...});
197
198 Emit "message" event and log "warn" message.
199
201 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
202
203
204
205perl v5.36.0 2022-07-22 Mojo::Log(3)