The document discusses using the Log::Minimal module in Perl to perform logging at different levels. It demonstrates calling the critf(), warnf(), infoff(), and debugff() functions to log messages tagged with severity levels. It also shows how to configure log formatting and filtering based on level. The document then discusses using Log::Minimal with the Plack framework to log requests.
1 of 63
Downloaded 16 times
More Related Content
Operation Oriented Web Applications / Yokohama pm7
7. #!/usr/bin/env perl
use strict;
use warnings;
use Log::Minimal;
critf("%s","foo");
warnf("%d %s", 1, "foo");
sub hoge {
infoff("foo");
debugff("bar");
};
hoge();
local $Log::Minimal::AUTODUMP = 1;
infof({ key => 'val' });
warnf("data is %s", { key => 'val' });
8. $ LM_DEBUG=1 perl /tmp/logminimal.pl
2011-05-11T15:57:49 [CRITICAL] foo at /tmp/logminimal.pl line 7
2011-05-11T15:57:49 [WARN] 1 foo at /tmp/logminimal.pl line 8
2011-05-11T15:57:49 [INFO] foo at /tmp/logminimal.pl line 11 ,/tmp/
logminimal.pl line 14
2011-05-11T15:57:49 [DEBUG] bar at /tmp/logminimal.pl line 12 ,/tmp/
logminimal.pl line 14
2011-05-11T15:57:49 [INFO] {'key' => 'val'} at /tmp/logminimal.pl line 17
2011-05-11T15:57:49 [WARN] data is {'key' => 'val'} at /tmp/logminimal.pl
line 18
9. local $Log::Minimal::PRINT = sub {
my ( $time, $type, $message, $trace) = @_;
print STDERR “[$type] $message $trace”;
};
local $Log::Minimal::LOG_LEVEL = "WARN";
infof("foo"); #print nothing
warnf(“xaicron++”);
10. local $Log::Minimal::AUTODUMP = 1;
warnf(“response => %s”,[ 200, [‘Content-Type’,‘text/
plain’],[‘OK’]]);
# 2011-05-11T15:56:14 [WARN] response => [200,
['Content-Type','text/plain'],['OK']] at ..
sub myerror {
local $Log::Minimal::TRACE_LEVEL = 1;
infof(@_);
}
myerror(“foo”);
16. use Log::Minimal;
use Plack::Builder;
builder {
enable "Log::Minimal", autodump => 1;
sub {
my $env = shift;
warnf("warn message");
debugf("debug message");
...
}
};
$ plackup -a demo.psgi
HTTP::Server::PSGI: Accepting connections at http://0:5000/
2011-05-11T16:32:24 [WARN] [/foo/bar/baz] warn message at /
tmp/demo.psgi line 8
2011-05-11T16:32:24 [DEBUG] [/foo/bar/baz] debug message at /
tmp/demo.psgi line 9
52. CREATE TABLE objects (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fid INT UNSIGNED NOT NULL,
bucket_id INT UNSIGNED NOT NULL,
rid SMALLINT UNSIGNED NOT NULL,
gid SMALLINT UNSIGNED NOT NULL,
filename VARCHAR(1024),
INDEX (fid, bucket_id),
INDEX (bucket_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin
SELECT * FROM objects WHERE fid = murmur($filename) AND
bucket_id = $bucket AND filename = $filename
53. CREATE TABLE entries_int (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fid INT UNSIGNED NOT NULL,
bid INT UNSIGNED NOT NULL,
filename VARCHAR(255) NOT NULL,
INDEX (fid, bid),
INDEX (bid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin
CREATE TABLE entries_char (
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
filename VARCHAR(255) NOT NULL,
bid INT UNSIGNED NOT NULL,
UNIQUE INDEX (filename, bid),
INDEX (bid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_bin