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->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

DESCRIPTION

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

EVENTS

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

ATTRIBUTES

39       Mojo::Log implements the following attributes.
40
41   format
42         my $cb = $log->format;
43         $log   = $log->format(sub {...});
44
45       A callback for formatting log messages.
46
47         $log->format(sub ($time, $level, @lines) { "[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n" });
48
49   handle
50         my $handle = $log->handle;
51         $log       = $log->handle(IO::Handle->new);
52
53       Log filehandle used by default "message" event, defaults to opening
54       "path" or "STDERR".
55
56   history
57         my $history = $log->history;
58         $log        = $log->history([[time, 'debug', 'That went wrong']]);
59
60       The last few logged messages.
61
62   level
63         my $level = $log->level;
64         $log      = $log->level('debug');
65
66       Active log level, defaults to "debug". Available log levels are
67       "debug", "info", "warn", "error" and "fatal", in that order.
68
69   max_history_size
70         my $size = $log->max_history_size;
71         $log     = $log->max_history_size(5);
72
73       Maximum number of logged messages to store in "history", defaults to
74       10.
75
76   path
77         my $path = $log->path
78         $log     = $log->path('/var/log/mojo.log');
79
80       Log file path used by "handle".
81
82   short
83         my $bool = $log->short;
84         $log     = $log->short($bool);
85
86       Generate short log messages without a timestamp, suitable for systemd,
87       defaults to the value of the "MOJO_LOG_SHORT" environment variables.
88

METHODS

90       Mojo::Log inherits all methods from Mojo::EventEmitter and implements
91       the following new ones.
92
93   append
94         $log->append("[2018-11-08 14:20:13.77168] [28320] [info] I ♥ Mojolicious\n");
95
96       Append message to "handle".
97
98   context
99         my $new = $log->context('[extra] [information]');
100
101       Construct a new child Mojo::Log object that will include context
102       information with every log message.
103
104         # Log with context
105         my $log = Mojo::Log->new;
106         my $context = $log->context('[17a60115]');
107         $context->debug('This is a log message with context information');
108         $context->info('And another');
109
110   debug
111         $log = $log->debug('You screwed up, but that is ok');
112         $log = $log->debug('All', 'cool');
113         $log = $log->debug(sub {...});
114
115       Emit "message" event and log "debug" message.
116
117   error
118         $log = $log->error('You really screwed up this time');
119         $log = $log->error('Wow', 'seriously');
120         $log = $log->error(sub {...});
121
122       Emit "message" event and log "error" message.
123
124   fatal
125         $log = $log->fatal('Its over...');
126         $log = $log->fatal('Bye', 'bye');
127         $log = $log->fatal(sub {...});
128
129       Emit "message" event and log "fatal" message.
130
131   info
132         $log = $log->info('You are bad, but you prolly know already');
133         $log = $log->info('Ok', 'then');
134         $log = $log->info(sub {...});
135
136       Emit "message" event and log "info" message.
137
138   is_level
139         my $bool = $log->is_level('debug');
140
141       Check active log "level".
142
143         # True
144         $log->level('debug')->is_level('debug');
145         $log->level('debug')->is_level('info');
146
147         # False
148         $log->level('info')->is_level('debug');
149         $log->level('fatal')->is_level('warn');
150
151   new
152         my $log = Mojo::Log->new;
153         my $log = Mojo::Log->new(level => 'warn');
154         my $log = Mojo::Log->new({level => 'warn'});
155
156       Construct a new Mojo::Log object and subscribe to "message" event with
157       default logger.
158
159   warn
160         $log = $log->warn('Dont do that Dave...');
161         $log = $log->warn('No', 'really');
162         $log = $log->warn(sub {...});
163
164       Emit "message" event and log "warn" message.
165

SEE ALSO

167       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
168
169
170
171perl v5.32.1                      2021-02-07                      Mojo::Log(3)
Impressum