Blame


1 c30e3741 2023-07-06 thelion # IRCNOW lib
2 c30e3741 2023-07-06 thelion
3 c30e3741 2023-07-06 thelion
4 c30e3741 2023-07-06 thelion ## Logger
5 c30e3741 2023-07-06 thelion
6 c30e3741 2023-07-06 thelion example usage:
7 c30e3741 2023-07-06 thelion ```perl
8 c30e3741 2023-07-06 thelion use strict;
9 c30e3741 2023-07-06 thelion use warnings;
10 c30e3741 2023-07-06 thelion
11 c30e3741 2023-07-06 thelion use lib qw(./lib);
12 c30e3741 2023-07-06 thelion use Logger;
13 c30e3741 2023-07-06 thelion
14 c30e3741 2023-07-06 thelion my $logger = new Logger(Logger->SUGAR, ":timestamp [:loglevel%5] :package%8 :fileline%10 :msg");
15 c30e3741 2023-07-06 thelion
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");
20 c30e3741 2023-07-06 thelion
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
26 c30e3741 2023-07-06 thelion ```
27 c30e3741 2023-07-06 thelion
28 c30e3741 2023-07-06 thelion ### formating the logger
29 c30e3741 2023-07-06 thelion
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
34 c30e3741 2023-07-06 thelion * file
35 c30e3741 2023-07-06 thelion * fileline (file with line number)
36 c30e3741 2023-07-06 thelion * line
37 c30e3741 2023-07-06 thelion * msg
38 c30e3741 2023-07-06 thelion
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.
40 c30e3741 2023-07-06 thelion ex:
41 c30e3741 2023-07-06 thelion
42 c30e3741 2023-07-06 thelion `:timestamp [:loglevel%5] :package%8 :fileline%10 :msg`
43 c30e3741 2023-07-06 thelion
44 c30e3741 2023-07-06 thelion note: if your field value is longer than the specified length, it will print out the full value.
45 c30e3741 2023-07-06 thelion ex:
46 c30e3741 2023-07-06 thelion format: `:timestamp [:loglevel%2] :package%8 :fileline%10 :msg`
47 c30e3741 2023-07-06 thelion result:
48 c30e3741 2023-07-06 thelion ```
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
53 c30e3741 2023-07-06 thelion ```
54 c758c436 2023-07-06 thelion
55 c758c436 2023-07-06 thelion ### sugar vs json
56 c758c436 2023-07-06 thelion
57 c758c436 2023-07-06 thelion there are two options to print logs:
58 c758c436 2023-07-06 thelion * sugar
59 c758c436 2023-07-06 thelion * JSON
60 c758c436 2023-07-06 thelion
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.
63 c758c436 2023-07-06 thelion
64 c758c436 2023-07-06 thelion example of JSON:
65 c758c436 2023-07-06 thelion ```
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" }
70 c758c436 2023-07-06 thelion ```
71 c758c436 2023-07-06 thelion
72 c758c436 2023-07-06 thelion example of Sugar:
73 c758c436 2023-07-06 thelion ```
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
78 c758c436 2023-07-06 thelion ```
79 c758c436 2023-07-06 thelion