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 ($log, $level, @lines) {...});
33
34 Emitted when a new message gets logged.
35
36 $log->on(message => sub ($log, $level, @lines) { say "$level: ", @lines });
37
39 Mojo::Log implements the following attributes.
40
41 color
42 my $bool = $log->color;
43 $log = $log->color($bool);
44
45 Colorize log messages with the levels "warn", "error" and "fatal" using
46 Term::ANSIColor, defaults to the value of the "MOJO_LOG_COLOR"
47 environment variables. Note that this attribute is EXPERIMENTAL and
48 might change without warning!
49
50 format
51 my $cb = $log->format;
52 $log = $log->format(sub {...});
53
54 A callback for formatting log messages.
55
56 $log->format(sub ($time, $level, @lines) { "[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n" });
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 but with journald log
96 level prefix, suitable for systemd environments, defaults to the value
97 of the "MOJO_LOG_SHORT" environment variables.
98
100 Mojo::Log inherits all methods from Mojo::EventEmitter and implements
101 the following new ones.
102
103 append
104 $log->append("[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n");
105
106 Append message to "handle".
107
108 context
109 my $new = $log->context('[extra]', '[information]');
110
111 Construct a new child Mojo::Log object that will include context
112 information with every log message.
113
114 # Log with context
115 my $log = Mojo::Log->new;
116 my $context = $log->context('[17a60115]');
117 $context->debug('This is a log message with context information');
118 $context->info('And another');
119
120 debug
121 $log = $log->debug('You screwed up, but that is ok');
122 $log = $log->debug('All', 'cool');
123 $log = $log->debug(sub {...});
124
125 Emit "message" event and log "debug" message.
126
127 error
128 $log = $log->error('You really screwed up this time');
129 $log = $log->error('Wow', 'seriously');
130 $log = $log->error(sub {...});
131
132 Emit "message" event and log "error" message.
133
134 fatal
135 $log = $log->fatal('Its over...');
136 $log = $log->fatal('Bye', 'bye');
137 $log = $log->fatal(sub {...});
138
139 Emit "message" event and log "fatal" message.
140
141 info
142 $log = $log->info('You are bad, but you prolly know already');
143 $log = $log->info('Ok', 'then');
144 $log = $log->info(sub {...});
145
146 Emit "message" event and log "info" message.
147
148 is_level
149 my $bool = $log->is_level('debug');
150
151 Check active log "level".
152
153 # True
154 $log->level('debug')->is_level('debug');
155 $log->level('debug')->is_level('info');
156
157 # False
158 $log->level('info')->is_level('debug');
159 $log->level('fatal')->is_level('warn');
160
161 new
162 my $log = Mojo::Log->new;
163 my $log = Mojo::Log->new(level => 'warn');
164 my $log = Mojo::Log->new({level => 'warn'});
165
166 Construct a new Mojo::Log object and subscribe to "message" event with
167 default logger.
168
169 warn
170 $log = $log->warn('Dont do that Dave...');
171 $log = $log->warn('No', 'really');
172 $log = $log->warn(sub {...});
173
174 Emit "message" event and log "warn" message.
175
177 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
178
179
180
181perl v5.34.0 2021-07-22 Mojo::Log(3)