1 c30e3741 2023-07-06 thelion # IRCNOW lib
6 c30e3741 2023-07-06 thelion example usage:
8 c30e3741 2023-07-06 thelion use strict;
9 c30e3741 2023-07-06 thelion use warnings;
11 c30e3741 2023-07-06 thelion use lib qw(./lib);
12 c30e3741 2023-07-06 thelion use Logger;
14 c30e3741 2023-07-06 thelion my $logger = new Logger(Logger->SUGAR, ":timestamp [:loglevel%5] :package%8 :fileline%10 :msg");
16 c30e3741 2023-07-06 thelion $logger->debug("debug");
17 c30e3741 2023-07-06 thelion $logger->info("info");
18 c30e3741 2023-07-06 thelion $logger->warn("warn");
19 c30e3741 2023-07-06 thelion $logger->error("error");
21 c30e3741 2023-07-06 thelion # output:
22 c30e3741 2023-07-06 thelion # 2023-07-02 19:22:03 [DEBUG] main test.pl:12 debug
23 c30e3741 2023-07-06 thelion # 2023-07-02 19:22:03 [INFO ] main test.pl:13 info
24 c30e3741 2023-07-06 thelion # 2023-07-02 19:22:03 [WARN ] main test.pl:14 warn
25 c30e3741 2023-07-06 thelion # 2023-07-02 19:22:03 [ERROR] main test.pl:15 error
28 c30e3741 2023-07-06 thelion ### formating the logger
30 c30e3741 2023-07-06 thelion logger has these fields:
31 c30e3741 2023-07-06 thelion * timestamp
32 c30e3741 2023-07-06 thelion * loglevel
33 c30e3741 2023-07-06 thelion * package
35 c30e3741 2023-07-06 thelion * fileline (file with line number)
39 c30e3741 2023-07-06 thelion prepend the field with a `:` and postfix the field with a `%` followed by a number. Add spaces in between.
42 c30e3741 2023-07-06 thelion `:timestamp [:loglevel%5] :package%8 :fileline%10 :msg`
44 c30e3741 2023-07-06 thelion note: if your field value is longer than the specified length, it will print out the full value.
46 c30e3741 2023-07-06 thelion format: `:timestamp [:loglevel%2] :package%8 :fileline%10 :msg`
49 c30e3741 2023-07-06 thelion 2023-07-02 19:21:54 [DEBUG] main test.pl debug
50 c30e3741 2023-07-06 thelion 2023-07-02 19:21:54 [INFO] main test.pl info
51 c30e3741 2023-07-06 thelion 2023-07-02 19:21:54 [WARN] main test.pl warn
52 c30e3741 2023-07-06 thelion 2023-07-02 19:21:54 [ERROR] main test.pl error
55 c758c436 2023-07-06 thelion ### sugar vs json
57 c758c436 2023-07-06 thelion there are two options to print logs:
61 c758c436 2023-07-06 thelion JSON logging is meant for being picked up by services such as grafana or the elk stack.
62 c758c436 2023-07-06 thelion Sugar logging is meant for human readable output to stdout.
64 c758c436 2023-07-06 thelion example of JSON:
66 c758c436 2023-07-06 thelion { "level": "DEBUG", "msg": "debug", "package": "main", "file": "test.pl:12" }
67 c758c436 2023-07-06 thelion { "level": "INFO", "msg": "info", "package": "main", "file": "test.pl:13" }
68 c758c436 2023-07-06 thelion { "level": "WARN", "msg": "warn", "package": "main", "file": "test.pl:14" }
69 c758c436 2023-07-06 thelion { "level": "ERROR", "msg": "error", "package": "main", "file": "test.pl:15" }
72 c758c436 2023-07-06 thelion example of Sugar:
74 c758c436 2023-07-06 thelion 2023-07-02 19:22:03 [DEBUG] main test.pl:12 debug
75 c758c436 2023-07-06 thelion 2023-07-02 19:22:03 [INFO ] main test.pl:13 info
76 c758c436 2023-07-06 thelion 2023-07-02 19:22:03 [WARN ] main test.pl:14 warn
77 c758c436 2023-07-06 thelion 2023-07-02 19:22:03 [ERROR] main test.pl:15 error