1Log::Contextual::WarnLoUgsgeerr(C3o)ntributed Perl DocumLeongt:a:tCioonntextual::WarnLogger(3)
2
3
4
6 Log::Contextual::WarnLogger - logger for libraries using
7 Log::Contextual
8
10 version 0.008001
11
13 package My::Package;
14 use Log::Contextual::WarnLogger;
15 use Log::Contextual qw( :log ),
16 -default_logger => Log::Contextual::WarnLogger->new({
17 env_prefix => 'MY_PACKAGE',
18 levels => [ qw(debug info notice warning error critical alert emergency) ],
19 });
20
21 # warns '[info] program started' if $ENV{MY_PACKAGE_TRACE} is set
22 log_info { 'program started' }; # no-op because info is not in levels
23 sub foo {
24 # warns '[debug] entered foo' if $ENV{MY_PACKAGE_DEBUG} is set
25 log_debug { 'entered foo' };
26 ...
27 }
28
30 This module is a simple logger made for libraries using
31 Log::Contextual. We recommend the use of this logger as your default
32 logger as it is simple and useful for most users, yet users can use
33 "set_logger" in Log::Contextual to override your choice of logger in
34 their own code thanks to the way Log::Contextual works.
35
37 new
38 Arguments: "Dict[ env_prefix => Str, levels => List ] $conf"
39
40 my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' });
41
42 or:
43
44 my $l = Log::Contextual::WarnLogger->new({
45 env_prefix => 'BAR',
46 levels => [ 'level1', 'level2' ],
47 });
48
49 Creates a new logger object where "env_prefix" defines what the prefix
50 is for the environment variables that will be checked for the log
51 levels.
52
53 The log levels may be customized, but if not defined, these are used:
54
55 trace
56 debug
57 info
58 warn
59 error
60 fatal
61
62 For example, if "env_prefix" is set to "FREWS_PACKAGE" the following
63 environment variables will be used:
64
65 FREWS_PACKAGE_UPTO
66
67 FREWS_PACKAGE_TRACE
68 FREWS_PACKAGE_DEBUG
69 FREWS_PACKAGE_INFO
70 FREWS_PACKAGE_WARN
71 FREWS_PACKAGE_ERROR
72 FREWS_PACKAGE_FATAL
73
74 Note that "UPTO" is a convenience variable. If you set
75 "FOO_UPTO=TRACE" it will enable all log levels. Similarly, if you set
76 it to "FATAL" only fatal will be enabled.
77
78 $level
79 Arguments: @anything
80
81 All of the following six methods work the same. The basic pattern is:
82
83 sub $level {
84 my $self = shift;
85
86 warn "[$level] " . join qq{\n}, @_;
87 if $self->is_$level;
88 }
89
90 trace
91
92 $l->trace( 'entered method foo with args ' join q{,}, @args );
93
94 debug
95
96 $l->debug( 'entered method foo' );
97
98 info
99
100 $l->info( 'started process foo' );
101
102 warn
103
104 $l->warn( 'possible misconfiguration at line 10' );
105
106 error
107
108 $l->error( 'non-numeric user input!' );
109
110 fatal
111
112 $l->fatal( '1 is never equal to 0!' );
113
114 If different levels are specified, appropriate functions named for your
115 custom levels work as you expect.
116
117 Note: "fatal" does not call "die" for you, see "EXCEPTIONS AND ERROR
118 HANDLING" in Log::Contextual
119
120 is_$level
121 All of the following six functions just return true if their respective
122 environment variable is enabled.
123
124 is_trace
125
126 say 'tracing' if $l->is_trace;
127
128 is_debug
129
130 say 'debuging' if $l->is_debug;
131
132 is_info
133
134 say q{info'ing} if $l->is_info;
135
136 is_warn
137
138 say 'warning' if $l->is_warn;
139
140 is_error
141
142 say 'erroring' if $l->is_error;
143
144 is_fatal
145
146 say q{fatal'ing} if $l->is_fatal;
147
148 If different levels are specified, appropriate is_$level functions work
149 as you would expect.
150
152 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
153
155 This software is copyright (c) 2018 by Arthur Axel "fREW" Schmidt.
156
157 This is free software; you can redistribute it and/or modify it under
158 the same terms as the Perl 5 programming language system itself.
159
160
161
162perl v5.32.0 2020-07-28 Log::Contextual::WarnLogger(3)