README.md
1# NAME
2
3Log::Minimal - Minimal but customizable logger.
4
5# SYNOPSIS
6
7 use Log::Minimal;
8
9 critf("%s","foo"); # 2010-10-20T00:25:17 [CRITICAL] foo at example.pl line 12
10 warnf("%d %s %s", 1, "foo", $uri);
11 infof('foo');
12 debugf("foo"); print if $ENV{LM_DEBUG} is true
13
14 # with full stack trace
15 critff("%s","foo");
16 # 2010-10-20T00:25:17 [CRITICAL] foo at lib/Example.pm line 10, example.pl line 12
17 warnff("%d %s %s", 1, "foo", $uri);
18 infoff('foo');
19 debugff("foo"); print if $ENV{LM_DEBUG} is true
20
21 my $serialize = ddf({ 'key' => 'value' });
22
23 # die with formatted message
24 croakf('foo');
25 croakff('%s %s', $code, $message);
26
27# DESCRIPTION
28
29Log::Minimal is Minimal but customizable log module.
30
31# EXPORT FUNCTIONS
32
33- critf(($message:Str|$format:Str,@list:Array));
34
35 critf("could't connect to example.com");
36 critf("Connection timeout timeout:%d, host:%s", 2, "example.com");
37
38 Display CRITICAL messages.
39 When two or more arguments are passed to the function,
40 the first argument is treated as a format of printf.
41
42 local $Log::Minimal::AUTODUMP = 1;
43 critf({ foo => 'bar' });
44 critf("dump is %s", { foo => 'bar' });
45
46 If $Log::Minimal::AUTODUMP is true, reference or object message is serialized with
47 Data::Dumper automatically.
48
49- warnf(($message:Str|$format:Str,@list:Array));
50
51 Display WARN messages.
52
53- infof(($message:Str|$format:Str,@list:Array));
54
55 Display INFO messages.
56
57- debugf(($message:Str|$format:Str,@list:Array));
58
59 Display DEBUG messages, if $ENV{LM\_DEBUG} is true.
60
61- critff(($message:Str|$format:Str,@list:Array));
62
63 critff("could't connect to example.com");
64 critff("Connection timeout timeout:%d, host:%s", 2, "example.com");
65
66 Display CRITICAL messages with stack trace.
67
68- warnff(($message:Str|$format:Str,@list:Array));
69
70 Display WARN messages with stack trace.
71
72- infoff(($message:Str|$format:Str,@list:Array));
73
74 Display INFO messages with stack trace.
75
76- debugff(($message:Str|$format:Str,@list:Array));
77
78 Display DEBUG messages with stack trace, if $ENV{LM\_DEBUG} is true.
79
80- croakf(($message:Str|$format:Str,@list:Array));
81
82 die with formatted $message
83
84 croakf("critical error");
85 # 2011-06-10T16:27:26 [ERROR] critical error at sample.pl line 23
86
87- croakff(($message:Str|$format:Str,@list:Array));
88
89 die with formatted $message with stack trace
90
91- ddf($value:Any)
92
93 Utility method that serializes given value with Data::Dumper;
94
95 my $serialize = ddf($hashref);
96
97
98
99# ENVIRONMENT VALUE
100
101- $ENV{LM\_DEBUG}
102
103 To print debugf and debugff messages, $ENV{LM\_DEBUG} must be true.
104
105 You can change variable name from LM\_DEBUG to arbitrary string which is specified by "env\_debug" in use line. Changed variable name affects only in package locally.
106
107 use Log::Minimal env_debug => 'FOO_DEBUG';
108
109
110 $ENV{LM_DEBUG} = 1;
111 $ENV{FOO_DEBUG} = 0;
112 debugf("hello"); # no output
113
114
115 $ENV{FOO_DEBUG} = 1;
116 debugf("world"); # print message
117
118- $ENV{LM\_COLOR}
119
120 $ENV{LM\_COLOR} is used as default value of $Log::Minimal::COLOR
121
122- $ENV{LM\_DEFAULT\_COLOR}
123
124 $ENV{LM\_DEFAULT\_COLOR} is used as default value of $Log::Minimal::DEFAULT\_COLOR
125
126 Format of value is "LEVEL=FG;BG:LEVEL=FG;BG:...". "FG" and "BG" are optional.
127
128 For example:
129
130 export LM_DEFAULT_COLOR='debug=red:info=;cyan:critical=yellow;red'
131
132# CUSTOMIZE
133
134- $Log::Minimal::COLOR
135
136 Coloring log messages. Disabled by default.
137
138- $Log::Minimal::PRINT
139
140 To change the method of outputting the log, set $Log::Minimal::PRINT.
141
142 # with PSGI Application. output log with request uri.
143 my $app = sub {
144 my $env = shift;
145 local $Log::Minimal::PRINT = sub {
146 my ( $time, $type, $message, $trace,$raw_message) = @_;
147 $env->{psgi.errors}->print(
148 "$time [$env->{SCRIPT_NAME}] [$type] $message at $trace\n");
149 };
150 run_app(...);
151 }
152
153 $message includes color sequences, If you want raw message text, use $raw\_message.
154 default is
155
156 sub {
157 my ( $time, $type, $message, $trace,$raw_message) = @_;
158 warn "$time [$type] $message at $trace\n";
159 }
160
161- $Log::Minimal::DIE
162
163 To change the format of die message, set $Log::Minimal::DIE.
164
165 local $Log::Minimal::PRINT = sub {
166 my ( $time, $type, $message, $trace) = @_;
167 die "[$type] $message at $trace\n"; # not need time
168 };
169
170 default is
171
172 sub {
173 my ( $time, $type, $message, $trace) = @_;
174 die "$time [$type] $message at $trace\n";
175 }
176
177- $Log::Minimal::LOG\_LEVEL
178
179 Set level to output log.
180
181 local $Log::Minimal::LOG_LEVEL = "WARN";
182 infof("foo"); #print nothing
183 warnf("foo");
184
185 Support levels are DEBUG,INFO,WARN,CRITICAL and NONE.
186 If NONE is set, no output except croakf and croakff. Default log level is DEBUG.
187
188- $Log::Minimal::AUTODUMP
189
190 Serialize message with Data::Dumper.
191
192 warnf("%s", {foo => 'bar'}); # HASH(0x100804ed0)
193
194 local $Log::Minimal::AUTODUMP = 1;
195 warnf("dump is %s", {foo=>'bar'}); #dump is {foo=>'bar'}
196
197 my $uri = URI->new("http://search.cpan.org/");
198 warnf("uri: '%s'", $uri); # uri: 'http://search.cpan.org/'
199
200 If message is object and has overload methods like '""' or '0+',
201 Log::Minimal uses it instead of Data::Dumper.
202
203- $Log::Minimal::TRACE\_LEVEL
204
205 Like a $Carp::CarpLevel, this variable determines how many additional call frames are to be skipped.
206 Defaults to 0.
207
208- $Log::Minimal::ESCAPE\_WHITESPACE
209
210 If this value is true, whitespace other than space will be represented as \[\\n\\t\\r\].
211 Defaults to 0.
212
213# AUTHOR
214
215Masahiro Nagano <kazeburo {at} gmail.com>
216
217# THANKS TO
218
219Yuji Shimada (xaicron)
220
221Yoshihiro Sugi (sugyan)
222
223# SEE ALSO
224
225# LICENSE
226
227This library is free software; you can redistribute it and/or modify
228it under the same terms as Perl itself.
229