1Log::Handler::Examples(U3s)er Contributed Perl DocumentatLioogn::Handler::Examples(3)
2
3
4

NAME

6       Log::Handler::Examples - Examples.
7

CREATE LOGGER

9       Quite simple
10
11           use Log::Handler;
12
13           my $log = Log::Handler->new();
14           $log->add( screen => \%options );
15
16       Create a application wide logger
17
18           my $log = Log::Handler->create_logger("myapp");
19           $log->add( screen => \%options );
20
21       Once created you can use the application logger in all modules of your
22       project:
23
24           package MyApp;
25           use Log::Handler;
26
27           my $log = Log::Handler->create_logger("myapp");
28           $log->add( screen => \%options );
29
30           package MyApp::Admin;
31           use Log::Handler;
32
33           my $log = Log::Handler->get_logger("myapp");
34           $log->info("message");
35

ADD OUTPUTS

37           use Log::Handler;
38
39           my $log = Log::Handler->new();
40
41           $log->add( dbi     => \%options );
42           $log->add( email   => \%options );
43           $log->add( file    => \%options );
44           $log->add( forward => \%options );
45           $log->add( screen  => \%options );
46           $log->add( socket  => \%options );
47
48       This is the same like
49
50           $log->add( "Log::Handler::Output::DBI"     => \%options );
51           $log->add( "Log::Handler::Output::Email"   => \%options );
52           $log->add( "Log::Handler::Output::File"    => \%options );
53           $log->add( "Log::Handler::Output::Forward" => \%options );
54           $log->add( "Log::Handler::Output::Screen"  => \%options );
55           $log->add( "Log::Handler::Output::Socket"  => \%options );
56

RELOAD THE LOGGER

58   Quite simple
59           use Log::Handler;
60
61           my $log = Log::Handler->new();
62
63           $log->config(config => "logger.conf");
64
65           $log->reload(config => "logger.conf");
66
67   Reload on HUP
68           use Log::Handler;
69
70           my $log = Log::Handler->new();
71
72           $log->config(config => "logger.conf");
73
74           $SIG{HUP} = sub {
75               unless ($log->reload(config => "logger.conf")) {
76                   warn "unable to reload configuration";
77                   warn $log->errstr;
78               }
79           };
80
81   Validate first
82       It's possible to make a configuration check before you reload:
83
84           $log->validate(config => "logger.conf")
85               or warn $log->errstr;
86

LOG VIA DBI

88           use Log::Handler;
89
90           my $log = Log::Handler->new();
91
92           $log->add(
93               dbi => {
94                   database   => "database",
95                   driver     => "mysql",
96                   host       => "127.0.0.1",
97                   port       => 3306,
98                   user       => "user",
99                   password   => "password",
100                   table      => "messages",
101                   columns    => [ qw/level ctime cdate pid hostname caller progname mtime message/ ],
102                   values     => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ],
103                   maxlevel   => "error",
104                   minlevel   => "emergency"
105                   newline    => 0,
106                   message_pattern => "%L %T %D %P %H %C %S %t %m",
107               }
108           );
109
110           $log->error("log an error");
111
112       Or with "dbname"
113
114           $log->add(
115               dbi => {
116                   dbname     => "database",
117                   driver     => "Pg",
118                   host       => "127.0.0.1",
119                   port       => 5432,
120                   user       => "user",
121                   password   => "password",
122                   table      => "messages",
123                   columns    => [ qw/level ctime cdate pid hostname caller progname mtime message/ ],
124                   values     => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ],
125                   maxlevel   => "error",
126                   minlevel   => "emergency"
127                   newline    => 0,
128                   message_pattern => "%L %T %D %P %H %C %S %t %m",
129               }
130           );
131
132       Or with "data_source"
133
134           $log->add(
135               dbi => {
136                   data_source => "dbi:SQLite:dbname=database.sqlite",
137                   table       => "messages",
138                   columns     => [ qw/level ctime cdate pid hostname caller progname mtime message/ ],
139                   values      => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ],
140                   maxlevel    => "error",
141                   minlevel    => "emergency"
142                   newline     => 0,
143                   message_pattern => "%L %T %D %P %H %C %S %t %m",
144               }
145           );
146

LOG VIA EMAIL

148           use Log::Handler;
149
150           my $log = Log::Handler->new();
151
152           $log->add(
153               email => {
154                   host     => "mx.bar.example",
155                   hello    => "EHLO my.domain.example",
156                   timeout  => 30,
157                   from     => "bar@foo.example",
158                   to       => "foo@bar.example",
159                   subject  => "your subject",
160                   buffer   => 0,
161                   maxlevel => "emergency",
162                   minlevel => "emergency",
163                   message_pattern => '%L',
164               }
165           );
166
167           $log->emergency("log an emergency issue");
168

LOG VIA SENDMAIL

170           use Log::Handler;
171
172           my $log = Log::Handler->new();
173
174           $log->add(
175               sendmail => {
176                   from     => "bar@foo.example",
177                   to       => "foo@bar.example",
178                   subject  => "your subject",
179                   maxlevel => "error",
180                   minlevel => "error",
181                   message_pattern => '%L',
182               }
183           );
184
185           $log->emergency("message");
186

LOG VIA FILE

188           use Log::Handler;
189
190           my $log = Log::Handler->new();
191
192           $log->add(
193               file => {
194                   filename => "file1.log",
195                   maxlevel => 7,
196                   minlevel => 0
197               }
198           );
199
200           $log->error("log an error");
201

LOG VIA FORWARD

203           use Log::Handler;
204
205           my $log = Log::Handler->new();
206
207           $log->add(
208               forward => {
209                   forward_to      => \&my_func,
210                   message_pattern => [ qw/%L %T %P %H %C %S %t/ ],
211                   message_layout  => "%m",
212                   maxlevel        => "info",
213               }
214           );
215
216           $log->info("log a information");
217
218           sub my_func {
219               my $params = shift;
220               print Dumper($params);
221           }
222

LOG VIA SCREEN

224           use Log::Handler;
225
226           my $log = Log::Handler->new();
227
228           $log->add(
229               screen => {
230                   log_to   => "STDERR",
231                   maxlevel => "info",
232               }
233           );
234
235           $log->info("log to the screen");
236

LOG VIA SOCKET

238           use Log::Handler;
239
240           my $log = Log::Handler->new();
241
242           $log->add(
243               socket => {
244                   peeraddr => "127.0.0.1",
245                   peerport => 44444,
246                   maxlevel => "info",
247                   die_on_errors => 0,
248               }
249           );
250
251           while ( 1 ) {
252               $log->info("test")
253                   or warn "unable to send message: ", $log->errstr;
254               sleep 1;
255           }
256
257   SIMPLE SOCKET SERVER (TCP)
258           use strict;
259           use warnings;
260           use IO::Socket::INET;
261           use Log::Handler::Output::File;
262
263           my $sock = IO::Socket::INET->new(
264               LocalAddr => "127.0.0.1",
265               LocalPort => 44444,
266               Listen    => 2,
267           ) or die $!;
268
269           my $file = Log::Handler::Output::File->new(
270               filename => "file.log",
271               fileopen => 1,
272               reopen   => 1,
273           );
274
275           while ( 1 ) {
276               $file->log(message => "waiting for next connection\n");
277
278               while (my $request = $sock->accept) {
279                   my $ipaddr = sprintf("%-15s", $request->peerhost);
280                   while (my $message = <$request>) {
281                       $file->log(message => "$ipaddr - $message");
282                   }
283               }
284           }
285

DIFFERENT OUTPUTS

287           use Log::Handler;
288
289           my $log = Log::Handler->new();
290
291           $log->add(
292               file => {
293                   filename => "common.log",
294                   maxlevel => 6,
295                   minlevel => 5,
296               }
297           );
298
299           $log->add(
300               file => {
301                   filename => "error.log",
302                   maxlevel => 4,
303                   minlevel => 0,
304               }
305           );
306
307           $log->add(
308               email => {
309                   host     => "mx.bar.example",
310                   hello    => "EHLO my.domain.example",
311                   timeout  => 120,
312                   from     => "bar@foo.example",
313                   to       => "foo@bar.example",
314                   subject  => "your subject",
315                   buffer   => 0,
316                   maxlevel => 0,
317               }
318           );
319
320           # log to common.log
321           $log->info("this is a info message");
322
323           # log to error.log
324           $log->warning("this is a warning");
325
326           # log to error.log and to foo@bar.example
327           $log->emergency("this is a emergency message");
328

FILTER MESSAGES

330           my $log = Log::Handler->new();
331
332           $log->add(
333               screen => {
334                   maxlevel => 6,
335                   filter_message => {
336                       match1    => "foo",
337                       match2    => "bar",
338                       match3    => "baz",
339                       condition => "(match1 && match2) && !match3"
340                   }
341               }
342           );
343
344           $log->info("foo");
345           $log->info("foo bar");
346           $log->info("foo baz");
347
348   FILTER CALLER
349       This example shows you how it's possilbe to debug messages only from a
350       special namespace.
351
352           my $log = Log::Handler->new();
353
354           $log->add(
355               file => {
356                   filename => "file1.log",
357                   maxlevel => "warning",
358               }
359           );
360
361           $log->add(
362               screen => {
363                   maxlevel => "debug",
364                   message_layout => "message from %p - %m",
365                   filter_caller  => qr/^Foo::Bar\z/,
366               }
367           );
368
369           $log->warning("a warning here");
370
371           package Foo::Bar;
372           $log->info("an info here");
373           1;
374
375   ANOTHER FILTER
376           filter_message => "as string"
377
378           filter_message => qr/as regexp/
379
380           filter_message => sub { shift->{message} =~ /as code ref/ }
381
382           # or with conditions
383
384           filter_message => {
385               match1    => "as string",
386               match2    => qr/as regexp/,
387               condition => "match1 || match2",
388           }
389
390           filter_caller => "as string"
391
392           filter_caller => qr/as regexp/
393

CONFIG

395       Examples:
396
397           my $log = Log::Handler->new( config => "logger.conf" );
398
399           # or
400
401           $log->add( config => "logger.conf" );
402
403           # or
404
405           $log->config( config => "logger.conf" );
406
407       Example with Config::General.
408
409       Script:
410
411           use Log::Handler;
412
413           my $log = Log::Handler->new();
414
415           $log->config( config => "logger.conf" );
416
417       Config (logger.conf):
418
419           <file>
420               alias    = common
421               filename = example.log
422               maxlevel = info
423               minlevel = warn
424           </file>
425
426           <file>
427               alias    = error
428               filename = example-error.log
429               maxlevel = warn
430               minlevel = emergency
431           </file>
432
433           <file>
434               alias    = debug
435               filename = example-debug.log
436               maxlevel = debug
437               minlevel = debug
438           </file>
439
440           <screen>
441               log_to   = STDERR
442               dump     = 1
443               maxlevel = debug
444               minlevel = debug
445           </screen>
446

CHECK FOR ACTIVE LEVELS

448       It can be very useful if you want to check if a level is active.
449
450           use Log::Handler;
451           use Data::Dumper;
452
453           my $log = Log::Handler->new();
454
455           $log->add(
456               file => {
457                   filename   => "file1.log",
458                   maxlevel   => 4,
459               }
460           );
461
462           my %hash = (foo => 1, bar => 2);
463
464       Now you want to dump the hash, but not in any case.
465
466           if ( $log->is_debug ) {
467               my $dump = Dumper(\%hash);
468               $log->debug($dump);
469           }
470
471       This would dump the hash only if the level debug is active.
472

AUTHOR

474       Jonny Schulz <jschulz.cpan(at)bloonix.de>.
475
476
477
478perl v5.36.0                      2022-07-22         Log::Handler::Examples(3)
Impressum