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("[Thu May 15 17:47:04 2014] [info] I ♥ Mojolicious\n");
104
105 Append message to "handle".
106
107 debug
108 $log = $log->debug('You screwed up, but that is ok');
109 $log = $log->debug('All', 'cool');
110 $log = $log->debug(sub {...});
111
112 Emit "message" event and log "debug" message.
113
114 error
115 $log = $log->error('You really screwed up this time');
116 $log = $log->error('Wow', 'seriously');
117 $log = $log->error(sub {...});
118
119 Emit "message" event and log "error" message.
120
121 fatal
122 $log = $log->fatal('Its over...');
123 $log = $log->fatal('Bye', 'bye');
124 $log = $log->fatal(sub {...});
125
126 Emit "message" event and log "fatal" message.
127
128 info
129 $log = $log->info('You are bad, but you prolly know already');
130 $log = $log->info('Ok', 'then');
131 $log = $log->info(sub {...});
132
133 Emit "message" event and log "info" message.
134
135 is_level
136 my $bool = $log->is_level('debug');
137
138 Check active log "level".
139
140 # True
141 $log->level('debug')->is_level('debug');
142 $log->level('debug')->is_level('info');
143
144 # False
145 $log->level('info')->is_level('debug');
146 $log->level('fatal')->is_level('warn');
147
148 new
149 my $log = Mojo::Log->new;
150 my $log = Mojo::Log->new(level => 'warn');
151 my $log = Mojo::Log->new({level => 'warn'});
152
153 Construct a new Mojo::Log object and subscribe to "message" event with
154 default logger.
155
156 warn
157 $log = $log->warn('Dont do that Dave...');
158 $log = $log->warn('No', 'really');
159 $log = $log->warn(sub {...});
160
161 Emit "message" event and log "warn" message.
162
164 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
165
166
167
168perl v5.28.0 2018-11-08 Mojo::Log(3)