|
1 | 1 | // Logging Library
|
2 |
| -// Copyright (c) 2015 - 2017 Sport Trades Ltd, 2020 Jaskirat Rajasansir |
| 2 | +// Copyright (c) 2015 - 2017 Sport Trades Ltd, 2020 - 2021 Jaskirat Rajasansir |
3 | 3 |
|
4 | 4 | // Documentation: https://github.com/BuaBook/kdb-common/wiki/log.q
|
5 | 5 |
|
6 |
| -.require.lib each `util`type`time; |
| 6 | +.require.lib each `util`type`time`cargs; |
7 | 7 |
|
8 | 8 |
|
9 | 9 | / Functions to determine which logger to use. The dictionary key is a symbol reference to the logger function if the
|
10 | 10 | / value function is true.
|
11 | 11 | / NOTE: .log.loggers.basic should always be last so it is the fallback if all others are not available
|
12 | 12 | .log.cfg.loggers:()!();
|
13 |
| -.log.cfg.loggers[`.log.loggers.color]: { (not ""~getenv`KDB_COLORS) | `logColors in key .Q.opt .z.x }; |
14 |
| -.log.cfg.loggers[`.log.loggers.syslog]:{ (not ""~getenv`KDB_LOG_SYSLOG) | `logSyslog in key .Q.opt .z.x }; |
15 |
| -.log.cfg.loggers[`.log.loggers.json]: { (not ""~getenv`KDB_LOG_JSON) | `logJson in key .Q.opt .z.x }; |
| 13 | +.log.cfg.loggers[`.log.loggers.color]: { (not ""~getenv`KDB_COLORS) | `logColors in key .cargs.get[] }; |
| 14 | +.log.cfg.loggers[`.log.loggers.syslog]:{ (not ""~getenv`KDB_LOG_SYSLOG) | `logSyslog in key .cargs.get[] }; |
| 15 | +.log.cfg.loggers[`.log.loggers.json]: { (not ""~getenv`KDB_LOG_JSON) | `logJson in key .cargs.get[] }; |
16 | 16 | .log.cfg.loggers[`.log.loggers.basic]: { 1b };
|
17 | 17 |
|
18 | 18 |
|
|
41 | 41 | .log.levels[`FATAL]:(-2i; 2i; "\033[4;31m");
|
42 | 42 |
|
43 | 43 | / Process identification
|
44 |
| -/ @see .log.init |
45 |
| -.log.process:`; |
| 44 | +/ NOTE: If this is set prior to the library being initialised, it will not be overwritten during '.log.init' |
| 45 | +.log.process:""; |
46 | 46 |
|
47 | 47 |
|
48 | 48 | .log.init:{
|
49 | 49 | if[.util.inDebugMode[];
|
50 | 50 | .log.level:`DEBUG;
|
51 | 51 | ];
|
52 | 52 |
|
| 53 | + if[0 = count .log.process; |
| 54 | + .log.process:"pid-",string .z.i; |
| 55 | + ]; |
| 56 | + |
53 | 57 | / setLogger calls setLevel
|
54 | 58 | .log.setLogger[];
|
55 | 59 |
|
56 |
| - .log.process:`$"pid-",string .z.i; |
| 60 | + .log.i.setInterfaceImplementations[]; |
57 | 61 | };
|
58 | 62 |
|
59 | 63 |
|
|
99 | 103 |
|
100 | 104 | .log.level:newLevel;
|
101 | 105 |
|
102 |
| - if[`if in key .require.loadedLibs; |
103 |
| - .log.i.setInterfaceImplementations[]; |
104 |
| - ]; |
105 |
| - |
106 | 106 | -1 "\nLogging enabled [ Level: ",string[.log.level]," ] [ Current Logger: `",string[.log.currentLogger]," ]\n";
|
107 | 107 | };
|
108 | 108 |
|
|
118 | 118 | :(<=). key[.log.levels]?/: .log.level,level;
|
119 | 119 | };
|
120 | 120 |
|
| 121 | +/ String log formatter with slf4j-stule parameterised formatting |
| 122 | +/ @returns (StringList) List of log elements in string format |
| 123 | +/ @see http://www.slf4j.org/faq.html#logging_performance |
| 124 | +.log.strFormatter:{[lvl; message] |
| 125 | + if[0h = type message; |
| 126 | + message:"" sv ("{}" vs first message),'(.type.ensureString each 1_ message),enlist ""; |
| 127 | + ]; |
| 128 | + |
| 129 | + elems:(.time.today[]; .time.nowAsTime[]; lvl; .log.process; `system^.z.u; .z.w; message); |
| 130 | + elems:@[elems; where not .type.isString each elems; string]; |
| 131 | + |
| 132 | + :elems; |
| 133 | + }; |
| 134 | + |
121 | 135 |
|
122 | 136 | / Basic logger
|
123 | 137 | .log.loggers.basic:{[fd;lvl;message]
|
124 |
| - logElems:(.time.today[];.time.nowAsTime[];lvl;.log.process;`system^.z.u;.z.w;message); |
125 |
| - logElems:@[logElems; where not .type.isString each logElems; string]; |
126 |
| - |
127 |
| - fd " " sv logElems; |
| 138 | + fd " " sv .log.strFormatter[lvl; message]; |
128 | 139 | };
|
129 | 140 |
|
130 | 141 | / Logger with color highlighting of the level based on the configuration in .log.levels
|
131 |
| -/ @see .log.levels |
132 |
| -/ @see .log.resetColor |
133 |
| -/ @see .log.loggers.basic |
134 | 142 | .log.loggers.color:{[fd;lvl;message]
|
135 | 143 | lvl:(.log.levels[lvl]`color),string[lvl],.log.resetColor;
|
136 |
| - |
137 |
| - .log.loggers.basic[fd; lvl; message]; |
| 144 | + fd " " sv .log.strFormatter[lvl; message]; |
138 | 145 | };
|
139 | 146 |
|
140 | 147 | / Non-color logger with the additional syslog priority prefix at the start of the log line. This is useful
|
141 | 148 | / when capturing log output into systemd (via 'systemd-cat').
|
142 |
| -/ NOTE: This function does not defer to '.log.loggers.basic' for logging |
143 |
| -/ @see .log.levels |
144 | 149 | .log.loggers.syslog:{[fd;lvl;message]
|
145 | 150 | syslogLvl:"<",string[.log.levels[lvl]`syslog],">";
|
146 |
| - |
147 |
| - logElems:(syslogLvl;.time.today[];.time.nowAsTime[];lvl;.log.process;`system^.z.u;.z.w;message); |
148 |
| - logElems:@[logElems; where not .type.isString each logElems; string]; |
149 |
| - |
150 |
| - fd " " sv logElems; |
| 151 | + fd " " sv enlist[syslogLvl],.log.strFormatter[lvl; message]; |
151 | 152 | };
|
152 | 153 |
|
153 | 154 | / JSON logger
|
| 155 | +/ NOTE: This logger does not do the slf4j-style parameterised replacement of the message but prints as the supplied list |
154 | 156 | .log.loggers.json:{[fd;lvl;message]
|
155 |
| - logElems:`date`time`level`processId`user`handle`message!(.time.today[];.time.nowAsTime[];lvl;.log.process;`system^.z.u;.z.w;message); |
| 157 | + logElems:`date`time`level`processId`user`handle`message!(.time.today[]; .time.nowAsTime[]; lvl; .log.process; `system^.z.u; .z.w; message); |
156 | 158 | fd .j.j logElems;
|
157 | 159 | };
|
158 | 160 |
|
| 161 | + |
| 162 | +/ Sets the interface functions for other kdb-common component and libraries if the interface 'if' library is defined |
| 163 | +/ in the current process |
| 164 | +/ @see .require.loadedLibs |
| 165 | +/ @see .if.setImplementationsFor |
| 166 | +/ @see .if.bindInterfacesFor |
159 | 167 | .log.i.setInterfaceImplementations:{
|
| 168 | + if[not `if in key .require.loadedLibs; |
| 169 | + :(::); |
| 170 | + ]; |
| 171 | + |
160 | 172 | allLevels:lower exec level from .log.levels;
|
161 | 173 |
|
162 | 174 | ifFuncs:` sv/: `.log`if,/:allLevels;
|
|
0 commit comments