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->debug('Not sure what is happening here');
19 $log->info('FYI: it happened again');
20 $log->warn('This might be a problem');
21 $log->error('Garden variety error');
22 $log->fatal('Boom');
23
25 Mojo::Log is a simple logger for Mojo projects.
26
28 Mojo::Log inherits all events from Mojo::EventEmitter and can emit the
29 following new ones.
30
31 message
32 $log->on(message => sub {
33 my ($log, $level, @lines) = @_;
34 ...
35 });
36
37 Emitted when a new message gets logged.
38
39 $log->on(message => sub {
40 my ($log, $level, @lines) = @_;
41 say "$level: ", @lines;
42 });
43
45 Mojo::Log implements the following attributes.
46
47 format
48 my $cb = $log->format;
49 $log = $log->format(sub {...});
50
51 A callback for formatting log messages.
52
53 $log->format(sub {
54 my ($time, $level, @lines) = @_;
55 return "[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n";
56 });
57
58 handle
59 my $handle = $log->handle;
60 $log = $log->handle(IO::Handle->new);
61
62 Log filehandle used by default "message" event, defaults to opening
63 "path" or "STDERR".
64
65 history
66 my $history = $log->history;
67 $log = $log->history([[time, 'debug', 'That went wrong']]);
68
69 The last few logged messages.
70
71 level
72 my $level = $log->level;
73 $log = $log->level('debug');
74
75 Active log level, defaults to "debug". Available log levels are
76 "debug", "info", "warn", "error" and "fatal", in that order.
77
78 max_history_size
79 my $size = $log->max_history_size;
80 $log = $log->max_history_size(5);
81
82 Maximum number of logged messages to store in "history", defaults to
83 10.
84
85 path
86 my $path = $log->path
87 $log = $log->path('/var/log/mojo.log');
88
89 Log file path used by "handle".
90
91 short
92 my $bool = $log->short;
93 $log = $log->short($bool);
94
95 Generate short log messages without a timestamp, suitable for systemd,
96 defaults to the value of the "MOJO_LOG_SHORT" environment variables.
97
99 Mojo::Log inherits all methods from Mojo::EventEmitter and implements
100 the following new ones.
101
102 append
103 $log->append("[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n");
104
105 Append message to "handle".
106
107 context
108 my $new = $log->context('[extra] [information]');
109
110 Construct a new child Mojo::Log object that will include context
111 information with every log message.
112
113 # Log with context
114 my $log = Mojo::Log->new;
115 my $context = $log->context('[17a60115]');
116 $context->debug('This is a log message with context information');
117 $context->info('And another');
118
119 debug
120 $log = $log->debug('You screwed up, but that is ok');
121 $log = $log->debug('All', 'cool');
122 $log = $log->debug(sub {...});
123
124 Emit "message" event and log "debug" message.
125
126 error
127 $log = $log->error('You really screwed up this time');
128 $log = $log->error('Wow', 'seriously');
129 $log = $log->error(sub {...});
130
131 Emit "message" event and log "error" message.
132
133 fatal
134 $log = $log->fatal('Its over...');
135 $log = $log->fatal('Bye', 'bye');
136 $log = $log->fatal(sub {...});
137
138 Emit "message" event and log "fatal" message.
139
140 info
141 $log = $log->info('You are bad, but you prolly know already');
142 $log = $log->info('Ok', 'then');
143 $log = $log->info(sub {...});
144
145 Emit "message" event and log "info" message.
146
147 is_level
148 my $bool = $log->is_level('debug');
149
150 Check active log "level".
151
152 # True
153 $log->level('debug')->is_level('debug');
154 $log->level('debug')->is_level('info');
155
156 # False
157 $log->level('info')->is_level('debug');
158 $log->level('fatal')->is_level('warn');
159
160 new
161 my $log = Mojo::Log->new;
162 my $log = Mojo::Log->new(level => 'warn');
163 my $log = Mojo::Log->new({level => 'warn'});
164
165 Construct a new Mojo::Log object and subscribe to "message" event with
166 default logger.
167
168 warn
169 $log = $log->warn('Dont do that Dave...');
170 $log = $log->warn('No', 'really');
171 $log = $log->warn(sub {...});
172
173 Emit "message" event and log "warn" message.
174
176 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
177
178
179
180perl v5.32.0 2020-07-28 Mojo::Log(3)