1Mojo::Log(3)          User Contributed Perl Documentation         Mojo::Log(3)
2
3
4

NAME

6       Mojo::Log - Simple logger
7

SYNOPSIS

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

DESCRIPTION

26       Mojo::Log is a simple logger for Mojo projects.
27

EVENTS

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

ATTRIBUTES

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

METHODS

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   context
110         my $new = $log->context('[extra]', '[information]');
111
112       Construct a new child Mojo::Log object that will include context
113       information with every log message.
114
115         # Log with context
116         my $log = Mojo::Log->new;
117         my $context = $log->context('[17a60115]');
118         $context->debug('This is a log message with context information');
119         $context->info('And another');
120
121   debug
122         $log = $log->debug('You screwed up, but that is ok');
123         $log = $log->debug('All', 'cool');
124         $log = $log->debug(sub {...});
125
126       Emit "message" event and log "debug" message.
127
128   error
129         $log = $log->error('You really screwed up this time');
130         $log = $log->error('Wow', 'seriously');
131         $log = $log->error(sub {...});
132
133       Emit "message" event and log "error" message.
134
135   fatal
136         $log = $log->fatal('Its over...');
137         $log = $log->fatal('Bye', 'bye');
138         $log = $log->fatal(sub {...});
139
140       Emit "message" event and log "fatal" message.
141
142   info
143         $log = $log->info('You are bad, but you prolly know already');
144         $log = $log->info('Ok', 'then');
145         $log = $log->info(sub {...});
146
147       Emit "message" event and log "info" message.
148
149   is_level
150         my $bool = $log->is_level('debug');
151
152       Check active log "level".
153
154         # True
155         $log->level('debug')->is_level('debug');
156         $log->level('debug')->is_level('info');
157
158         # False
159         $log->level('info')->is_level('debug');
160         $log->level('fatal')->is_level('warn');
161
162   new
163         my $log = Mojo::Log->new;
164         my $log = Mojo::Log->new(level => 'warn');
165         my $log = Mojo::Log->new({level => 'warn'});
166
167       Construct a new Mojo::Log object and subscribe to "message" event with
168       default logger.
169
170   trace
171         $log = $log->trace('Whatever');
172         $log = $log->trace('Who', 'cares');
173         $log = $log->trace(sub {...});
174
175       Emit "message" event and log "trace" message.
176
177   warn
178         $log = $log->warn('Dont do that Dave...');
179         $log = $log->warn('No', 'really');
180         $log = $log->warn(sub {...});
181
182       Emit "message" event and log "warn" message.
183

SEE ALSO

185       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
186
187
188
189perl v5.34.0                      2022-01-21                      Mojo::Log(3)
Impressum