@oino-ts/common 0.7.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,13 +10,15 @@ exports.OINOConsoleLog = exports.OINOLog = exports.OINOLogLevel = void 0;
10
10
  var OINOLogLevel;
11
11
  (function (OINOLogLevel) {
12
12
  /** Debug messages */
13
- OINOLogLevel[OINOLogLevel["debug"] = 0] = "debug";
13
+ OINOLogLevel[OINOLogLevel["debug"] = 1] = "debug";
14
14
  /** Informational messages */
15
- OINOLogLevel[OINOLogLevel["info"] = 1] = "info";
15
+ OINOLogLevel[OINOLogLevel["info"] = 2] = "info";
16
16
  /** Warning messages */
17
- OINOLogLevel[OINOLogLevel["warn"] = 2] = "warn";
17
+ OINOLogLevel[OINOLogLevel["warning"] = 3] = "warning";
18
18
  /** Error messages */
19
- OINOLogLevel[OINOLogLevel["error"] = 3] = "error";
19
+ OINOLogLevel[OINOLogLevel["error"] = 4] = "error";
20
+ /** Exception messages */
21
+ OINOLogLevel[OINOLogLevel["exception"] = 5] = "exception";
20
22
  })(OINOLogLevel || (exports.OINOLogLevel = OINOLogLevel = {}));
21
23
  /**
22
24
  * Abstract base class for logging implementations supporting
@@ -26,16 +28,16 @@ var OINOLogLevel;
26
28
  */
27
29
  class OINOLog {
28
30
  static _instance;
29
- _logLevel = OINOLogLevel.warn;
31
+ _logLevels = { "*|*|*": OINOLogLevel.warning };
30
32
  /**
31
33
  * Abstract logging method to implement the actual logging operation.
32
34
  *
33
- * @param logLevel level of the log events
35
+ * @param logLevel default loglevel for all log events
34
36
  *
35
37
  */
36
- constructor(logLevel = OINOLogLevel.warn) {
38
+ constructor(logLevel = OINOLogLevel.warning) {
37
39
  // console.log("OINOLog.constructor: logLevel=" + logLevel)
38
- this._logLevel = logLevel;
40
+ this._logLevels["*|*|*"] = logLevel;
39
41
  }
40
42
  /**
41
43
  * Abstract logging method to implement the actual logging operation.
@@ -46,10 +48,16 @@ class OINOLog {
46
48
  * @param data structured data associated with the log event
47
49
  *
48
50
  */
49
- static _log(level, levelStr, message, data) {
50
- // console.log("_log: level=" + level + ", levelStr=" + levelStr + ", message=" + message + ", data=" + data)
51
- if ((OINOLog._instance) && (OINOLog._instance._logLevel <= level)) {
52
- OINOLog._instance?._writeLog(levelStr, message, data);
51
+ static _log(level, levelStr, domain, channel, method, message, data) {
52
+ const log_levels = OINOLog._instance._logLevels;
53
+ // console.log(log_levels)
54
+ const min_level = log_levels[domain + "|" + channel + "|" + method] ||
55
+ log_levels[domain + "|" + channel + "|*"] ||
56
+ log_levels[domain + "|*|*"] ||
57
+ log_levels["*|*|*"];
58
+ // console.log("_log: level=" + level + ", min_level=" + min_level + ", levelStr=" + levelStr + ", message=" + message, data)
59
+ if ((OINOLog._instance) && (level >= min_level)) {
60
+ OINOLog._instance?._writeLog(levelStr, domain, channel, method, message, data);
53
61
  }
54
62
  }
55
63
  /**
@@ -65,55 +73,89 @@ class OINOLog {
65
73
  }
66
74
  }
67
75
  /**
68
- * Set log level.
76
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
77
+ * Multiple settings can be combined to set different logging accuracy specifically
78
+ *
79
+ * For example:
80
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
81
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
82
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
69
83
  *
70
84
  * @param logLevel log level to use
85
+ * @param domain domain of the log event (default: "*" for all)
86
+ * @param channel channel of the log event (default: "*" for all)
87
+ * @param method method of the log event (default: "*" for all)
71
88
  *
72
89
  */
73
- static setLogLevel(logLevel) {
90
+ static setLogLevel(logLevel, domain = "*", channel = "*", method = "*") {
74
91
  if (OINOLog._instance) {
75
- OINOLog._instance._logLevel = logLevel;
92
+ OINOLog._instance._logLevels[domain + "|" + channel + "|" + method] = logLevel;
76
93
  }
77
94
  }
78
95
  /**
79
- * Log error event.
96
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
80
97
  *
98
+ * @param domain domain of the log event
99
+ * @param channel channel of the log event
100
+ * @param method method of the log event
81
101
  * @param message message of the log event
82
102
  * @param data structured data associated with the log event
83
103
  *
84
104
  */
85
- static error(message, data) {
86
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data);
105
+ static exception(domain, channel, method, message, data) {
106
+ OINOLog._log(OINOLogLevel.exception, "EXCEPTION", domain, channel, method, message, data);
87
107
  }
88
108
  /**
89
- * Log warning event.
109
+ * Log error event. Error events are printed as a single line.
90
110
  *
111
+ * @param domain domain of the log event
112
+ * @param channel channel of the log event
113
+ * @param method method of the log event
91
114
  * @param message message of the log event
92
115
  * @param data structured data associated with the log event
93
116
  *
94
117
  */
95
- static warning(message, data) {
96
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data);
118
+ static error(domain, channel, method, message, data) {
119
+ OINOLog._log(OINOLogLevel.error, "ERROR", domain, channel, method, message, data);
97
120
  }
98
121
  /**
99
- * Log info event.
122
+ * Log warning event. Warning events are printed as a single line.
100
123
  *
124
+ * @param domain domain of the log event
125
+ * @param channel channel of the log event
126
+ * @param method method of the log event
101
127
  * @param message message of the log event
102
128
  * @param data structured data associated with the log event
103
129
  *
104
130
  */
105
- static info(message, data) {
106
- OINOLog._log(OINOLogLevel.info, "INFO", message, data);
131
+ static warning(domain, channel, method, message, data) {
132
+ OINOLog._log(OINOLogLevel.warning, "WARN", domain, channel, method, message, data);
107
133
  }
108
134
  /**
109
- * Log debug event.
135
+ * Log info event. Info events are printed as a single line.
110
136
  *
137
+ * @param domain domain of the log event
138
+ * @param channel channel of the log event
139
+ * @param method method of the log event
111
140
  * @param message message of the log event
112
141
  * @param data structured data associated with the log event
113
142
  *
114
143
  */
115
- static debug(message, data) {
116
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data);
144
+ static info(domain, channel, method, message, data) {
145
+ OINOLog._log(OINOLogLevel.info, "INFO", domain, channel, method, message, data);
146
+ }
147
+ /**
148
+ * Log debug event. Debug events are prettyprinted.
149
+ *
150
+ * @param domain domain of the log event
151
+ * @param channel channel of the log event
152
+ * @param method method of the log event
153
+ * @param message message of the log event
154
+ * @param data structured data associated with the log event
155
+ *
156
+ */
157
+ static debug(domain, channel, method, message, data) {
158
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", domain, channel, method, message, data);
117
159
  }
118
160
  }
119
161
  exports.OINOLog = OINOLog;
@@ -126,25 +168,38 @@ class OINOConsoleLog extends OINOLog {
126
168
  * Constructor of `OINOConsoleLog`
127
169
  * @param logLevel logging level
128
170
  */
129
- constructor(logLevel = OINOLogLevel.warn) {
171
+ constructor(logLevel = OINOLogLevel.warning) {
130
172
  super(logLevel);
131
173
  }
132
- _writeLog(level, message, data) {
133
- let log = "OINOLog " + level + ": " + message;
134
- if (data) {
135
- log += " " + JSON.stringify(data);
174
+ _writeLog(level, domain, channel, method, message, data) {
175
+ if (message === undefined) {
176
+ console.log("OINOLog missing message: " + (new Error()).stack);
136
177
  }
137
- if (level == "ERROR") {
138
- console.error(log);
178
+ let log = "OINOLog." + level + " | " + domain + " | " + channel + " | " + method + ": " + message;
179
+ let logger_func;
180
+ if ((level == "ERROR") || (level == "EXCEPTION")) {
181
+ logger_func = console.error;
139
182
  }
140
183
  else if (level == "WARN") {
141
- console.warn(log);
184
+ logger_func = console.warn;
142
185
  }
143
186
  else if (level == "INFO") {
144
- console.info(log);
187
+ logger_func = console.info;
188
+ }
189
+ else {
190
+ logger_func = console.log;
191
+ }
192
+ if (data && (level == "DEBUG")) {
193
+ logger_func(log, data);
194
+ }
195
+ else if (data && (level == "EXCEPTION")) {
196
+ logger_func(log + JSON.stringify(data, null, 2).replaceAll(/[^\\]\\n/g, "\n")); // preserve newlines for stack traces
197
+ }
198
+ else if (data) {
199
+ logger_func(log + " " + JSON.stringify(data));
145
200
  }
146
201
  else {
147
- console.log(log);
202
+ logger_func(log);
148
203
  }
149
204
  }
150
205
  }
@@ -7,13 +7,15 @@
7
7
  export var OINOLogLevel;
8
8
  (function (OINOLogLevel) {
9
9
  /** Debug messages */
10
- OINOLogLevel[OINOLogLevel["debug"] = 0] = "debug";
10
+ OINOLogLevel[OINOLogLevel["debug"] = 1] = "debug";
11
11
  /** Informational messages */
12
- OINOLogLevel[OINOLogLevel["info"] = 1] = "info";
12
+ OINOLogLevel[OINOLogLevel["info"] = 2] = "info";
13
13
  /** Warning messages */
14
- OINOLogLevel[OINOLogLevel["warn"] = 2] = "warn";
14
+ OINOLogLevel[OINOLogLevel["warning"] = 3] = "warning";
15
15
  /** Error messages */
16
- OINOLogLevel[OINOLogLevel["error"] = 3] = "error";
16
+ OINOLogLevel[OINOLogLevel["error"] = 4] = "error";
17
+ /** Exception messages */
18
+ OINOLogLevel[OINOLogLevel["exception"] = 5] = "exception";
17
19
  })(OINOLogLevel || (OINOLogLevel = {}));
18
20
  /**
19
21
  * Abstract base class for logging implementations supporting
@@ -23,16 +25,16 @@ export var OINOLogLevel;
23
25
  */
24
26
  export class OINOLog {
25
27
  static _instance;
26
- _logLevel = OINOLogLevel.warn;
28
+ _logLevels = { "*|*|*": OINOLogLevel.warning };
27
29
  /**
28
30
  * Abstract logging method to implement the actual logging operation.
29
31
  *
30
- * @param logLevel level of the log events
32
+ * @param logLevel default loglevel for all log events
31
33
  *
32
34
  */
33
- constructor(logLevel = OINOLogLevel.warn) {
35
+ constructor(logLevel = OINOLogLevel.warning) {
34
36
  // console.log("OINOLog.constructor: logLevel=" + logLevel)
35
- this._logLevel = logLevel;
37
+ this._logLevels["*|*|*"] = logLevel;
36
38
  }
37
39
  /**
38
40
  * Abstract logging method to implement the actual logging operation.
@@ -43,10 +45,16 @@ export class OINOLog {
43
45
  * @param data structured data associated with the log event
44
46
  *
45
47
  */
46
- static _log(level, levelStr, message, data) {
47
- // console.log("_log: level=" + level + ", levelStr=" + levelStr + ", message=" + message + ", data=" + data)
48
- if ((OINOLog._instance) && (OINOLog._instance._logLevel <= level)) {
49
- OINOLog._instance?._writeLog(levelStr, message, data);
48
+ static _log(level, levelStr, domain, channel, method, message, data) {
49
+ const log_levels = OINOLog._instance._logLevels;
50
+ // console.log(log_levels)
51
+ const min_level = log_levels[domain + "|" + channel + "|" + method] ||
52
+ log_levels[domain + "|" + channel + "|*"] ||
53
+ log_levels[domain + "|*|*"] ||
54
+ log_levels["*|*|*"];
55
+ // console.log("_log: level=" + level + ", min_level=" + min_level + ", levelStr=" + levelStr + ", message=" + message, data)
56
+ if ((OINOLog._instance) && (level >= min_level)) {
57
+ OINOLog._instance?._writeLog(levelStr, domain, channel, method, message, data);
50
58
  }
51
59
  }
52
60
  /**
@@ -62,55 +70,89 @@ export class OINOLog {
62
70
  }
63
71
  }
64
72
  /**
65
- * Set log level.
73
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
74
+ * Multiple settings can be combined to set different logging accuracy specifically
75
+ *
76
+ * For example:
77
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
78
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
79
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
66
80
  *
67
81
  * @param logLevel log level to use
82
+ * @param domain domain of the log event (default: "*" for all)
83
+ * @param channel channel of the log event (default: "*" for all)
84
+ * @param method method of the log event (default: "*" for all)
68
85
  *
69
86
  */
70
- static setLogLevel(logLevel) {
87
+ static setLogLevel(logLevel, domain = "*", channel = "*", method = "*") {
71
88
  if (OINOLog._instance) {
72
- OINOLog._instance._logLevel = logLevel;
89
+ OINOLog._instance._logLevels[domain + "|" + channel + "|" + method] = logLevel;
73
90
  }
74
91
  }
75
92
  /**
76
- * Log error event.
93
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
77
94
  *
95
+ * @param domain domain of the log event
96
+ * @param channel channel of the log event
97
+ * @param method method of the log event
78
98
  * @param message message of the log event
79
99
  * @param data structured data associated with the log event
80
100
  *
81
101
  */
82
- static error(message, data) {
83
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data);
102
+ static exception(domain, channel, method, message, data) {
103
+ OINOLog._log(OINOLogLevel.exception, "EXCEPTION", domain, channel, method, message, data);
84
104
  }
85
105
  /**
86
- * Log warning event.
106
+ * Log error event. Error events are printed as a single line.
87
107
  *
108
+ * @param domain domain of the log event
109
+ * @param channel channel of the log event
110
+ * @param method method of the log event
88
111
  * @param message message of the log event
89
112
  * @param data structured data associated with the log event
90
113
  *
91
114
  */
92
- static warning(message, data) {
93
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data);
115
+ static error(domain, channel, method, message, data) {
116
+ OINOLog._log(OINOLogLevel.error, "ERROR", domain, channel, method, message, data);
94
117
  }
95
118
  /**
96
- * Log info event.
119
+ * Log warning event. Warning events are printed as a single line.
97
120
  *
121
+ * @param domain domain of the log event
122
+ * @param channel channel of the log event
123
+ * @param method method of the log event
98
124
  * @param message message of the log event
99
125
  * @param data structured data associated with the log event
100
126
  *
101
127
  */
102
- static info(message, data) {
103
- OINOLog._log(OINOLogLevel.info, "INFO", message, data);
128
+ static warning(domain, channel, method, message, data) {
129
+ OINOLog._log(OINOLogLevel.warning, "WARN", domain, channel, method, message, data);
104
130
  }
105
131
  /**
106
- * Log debug event.
132
+ * Log info event. Info events are printed as a single line.
107
133
  *
134
+ * @param domain domain of the log event
135
+ * @param channel channel of the log event
136
+ * @param method method of the log event
108
137
  * @param message message of the log event
109
138
  * @param data structured data associated with the log event
110
139
  *
111
140
  */
112
- static debug(message, data) {
113
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data);
141
+ static info(domain, channel, method, message, data) {
142
+ OINOLog._log(OINOLogLevel.info, "INFO", domain, channel, method, message, data);
143
+ }
144
+ /**
145
+ * Log debug event. Debug events are prettyprinted.
146
+ *
147
+ * @param domain domain of the log event
148
+ * @param channel channel of the log event
149
+ * @param method method of the log event
150
+ * @param message message of the log event
151
+ * @param data structured data associated with the log event
152
+ *
153
+ */
154
+ static debug(domain, channel, method, message, data) {
155
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", domain, channel, method, message, data);
114
156
  }
115
157
  }
116
158
  /**
@@ -122,25 +164,38 @@ export class OINOConsoleLog extends OINOLog {
122
164
  * Constructor of `OINOConsoleLog`
123
165
  * @param logLevel logging level
124
166
  */
125
- constructor(logLevel = OINOLogLevel.warn) {
167
+ constructor(logLevel = OINOLogLevel.warning) {
126
168
  super(logLevel);
127
169
  }
128
- _writeLog(level, message, data) {
129
- let log = "OINOLog " + level + ": " + message;
130
- if (data) {
131
- log += " " + JSON.stringify(data);
170
+ _writeLog(level, domain, channel, method, message, data) {
171
+ if (message === undefined) {
172
+ console.log("OINOLog missing message: " + (new Error()).stack);
132
173
  }
133
- if (level == "ERROR") {
134
- console.error(log);
174
+ let log = "OINOLog." + level + " | " + domain + " | " + channel + " | " + method + ": " + message;
175
+ let logger_func;
176
+ if ((level == "ERROR") || (level == "EXCEPTION")) {
177
+ logger_func = console.error;
135
178
  }
136
179
  else if (level == "WARN") {
137
- console.warn(log);
180
+ logger_func = console.warn;
138
181
  }
139
182
  else if (level == "INFO") {
140
- console.info(log);
183
+ logger_func = console.info;
184
+ }
185
+ else {
186
+ logger_func = console.log;
187
+ }
188
+ if (data && (level == "DEBUG")) {
189
+ logger_func(log, data);
190
+ }
191
+ else if (data && (level == "EXCEPTION")) {
192
+ logger_func(log + JSON.stringify(data, null, 2).replaceAll(/[^\\]\\n/g, "\n")); // preserve newlines for stack traces
193
+ }
194
+ else if (data) {
195
+ logger_func(log + " " + JSON.stringify(data));
141
196
  }
142
197
  else {
143
- console.log(log);
198
+ logger_func(log);
144
199
  }
145
200
  }
146
201
  }
@@ -1,13 +1,15 @@
1
1
  /** Logging levels */
2
2
  export declare enum OINOLogLevel {
3
3
  /** Debug messages */
4
- debug = 0,
4
+ debug = 1,
5
5
  /** Informational messages */
6
- info = 1,
6
+ info = 2,
7
7
  /** Warning messages */
8
- warn = 2,
8
+ warning = 3,
9
9
  /** Error messages */
10
- error = 3
10
+ error = 4,
11
+ /** Exception messages */
12
+ exception = 5
11
13
  }
12
14
  /**
13
15
  * Abstract base class for logging implementations supporting
@@ -17,11 +19,11 @@ export declare enum OINOLogLevel {
17
19
  */
18
20
  export declare abstract class OINOLog {
19
21
  protected static _instance: OINOLog;
20
- protected _logLevel: OINOLogLevel;
22
+ protected _logLevels: Record<string, OINOLogLevel>;
21
23
  /**
22
24
  * Abstract logging method to implement the actual logging operation.
23
25
  *
24
- * @param logLevel level of the log events
26
+ * @param logLevel default loglevel for all log events
25
27
  *
26
28
  */
27
29
  constructor(logLevel?: OINOLogLevel);
@@ -29,11 +31,14 @@ export declare abstract class OINOLog {
29
31
  * Abstract logging method to implement the actual logging operation.
30
32
  *
31
33
  * @param levelStr level string of the log event
34
+ * @param domain domain of the log event
35
+ * @param channel channel of the log event
36
+ * @param method method of the log event
32
37
  * @param message message of the log event
33
38
  * @param data structured data associated with the log event
34
39
  *
35
40
  */
36
- protected abstract _writeLog(levelStr: string, message: string, data?: any): void;
41
+ protected abstract _writeLog(levelStr: string, domain: string, channel: string, method: string, message: string, data?: any): void;
37
42
  /**
38
43
  * Abstract logging method to implement the actual logging operation.
39
44
  *
@@ -43,7 +48,7 @@ export declare abstract class OINOLog {
43
48
  * @param data structured data associated with the log event
44
49
  *
45
50
  */
46
- protected static _log(level: OINOLogLevel, levelStr: string, message: string, data?: any): void;
51
+ protected static _log(level: OINOLogLevel, levelStr: string, domain: string, channel: string, method: string, message: string, data?: any): void;
47
52
  /**
48
53
  * Set active logger and log level.
49
54
  *
@@ -52,44 +57,76 @@ export declare abstract class OINOLog {
52
57
  */
53
58
  static setLogger(logger: OINOLog): void;
54
59
  /**
55
- * Set log level.
60
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
61
+ * Multiple settings can be combined to set different logging accuracy specifically
62
+ *
63
+ * For example:
64
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
65
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
66
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
56
67
  *
57
68
  * @param logLevel log level to use
69
+ * @param domain domain of the log event (default: "*" for all)
70
+ * @param channel channel of the log event (default: "*" for all)
71
+ * @param method method of the log event (default: "*" for all)
72
+ *
73
+ */
74
+ static setLogLevel(logLevel: OINOLogLevel, domain?: string, channel?: string, method?: string): void;
75
+ /**
76
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
77
+ *
78
+ * @param domain domain of the log event
79
+ * @param channel channel of the log event
80
+ * @param method method of the log event
81
+ * @param message message of the log event
82
+ * @param data structured data associated with the log event
58
83
  *
59
84
  */
60
- static setLogLevel(logLevel: OINOLogLevel): void;
85
+ static exception(domain: string, channel: string, method: string, message: string, data?: any): void;
61
86
  /**
62
- * Log error event.
87
+ * Log error event. Error events are printed as a single line.
63
88
  *
89
+ * @param domain domain of the log event
90
+ * @param channel channel of the log event
91
+ * @param method method of the log event
64
92
  * @param message message of the log event
65
93
  * @param data structured data associated with the log event
66
94
  *
67
95
  */
68
- static error(message: string, data?: any): void;
96
+ static error(domain: string, channel: string, method: string, message: string, data?: any): void;
69
97
  /**
70
- * Log warning event.
98
+ * Log warning event. Warning events are printed as a single line.
71
99
  *
100
+ * @param domain domain of the log event
101
+ * @param channel channel of the log event
102
+ * @param method method of the log event
72
103
  * @param message message of the log event
73
104
  * @param data structured data associated with the log event
74
105
  *
75
106
  */
76
- static warning(message: string, data?: any): void;
107
+ static warning(domain: string, channel: string, method: string, message: string, data?: any): void;
77
108
  /**
78
- * Log info event.
109
+ * Log info event. Info events are printed as a single line.
79
110
  *
111
+ * @param domain domain of the log event
112
+ * @param channel channel of the log event
113
+ * @param method method of the log event
80
114
  * @param message message of the log event
81
115
  * @param data structured data associated with the log event
82
116
  *
83
117
  */
84
- static info(message: string, data?: any): void;
118
+ static info(domain: string, channel: string, method: string, message: string, data?: any): void;
85
119
  /**
86
- * Log debug event.
120
+ * Log debug event. Debug events are prettyprinted.
87
121
  *
122
+ * @param domain domain of the log event
123
+ * @param channel channel of the log event
124
+ * @param method method of the log event
88
125
  * @param message message of the log event
89
126
  * @param data structured data associated with the log event
90
127
  *
91
128
  */
92
- static debug(message: string, data?: any): void;
129
+ static debug(domain: string, channel: string, method: string, message: string, data?: any): void;
93
130
  }
94
131
  /**
95
132
  * Logging implementation based on console.log.
@@ -101,5 +138,5 @@ export declare class OINOConsoleLog extends OINOLog {
101
138
  * @param logLevel logging level
102
139
  */
103
140
  constructor(logLevel?: OINOLogLevel);
104
- protected _writeLog(level: string, message: string, data?: any): void;
141
+ protected _writeLog(level: string, domain: string, channel: string, method: string, message: string, data?: any): void;
105
142
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/common",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "description": "OINO TS package for common classes.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -19,7 +19,7 @@
19
19
  "dependencies": {
20
20
  },
21
21
  "devDependencies": {
22
- "@oino-ts/types": "0.7.2"
22
+ "@oino-ts/types": "0.8.0"
23
23
  },
24
24
  "files": [
25
25
  "src/*.ts",
package/src/OINOLog.ts CHANGED
@@ -7,13 +7,15 @@
7
7
  /** Logging levels */
8
8
  export enum OINOLogLevel {
9
9
  /** Debug messages */
10
- debug=0,
10
+ debug=1,
11
11
  /** Informational messages */
12
- info=1,
12
+ info=2,
13
13
  /** Warning messages */
14
- warn=2,
14
+ warning=3,
15
15
  /** Error messages */
16
- error=3
16
+ error=4,
17
+ /** Exception messages */
18
+ exception=5
17
19
  }
18
20
 
19
21
  /**
@@ -26,17 +28,17 @@ export enum OINOLogLevel {
26
28
  export abstract class OINOLog {
27
29
  protected static _instance:OINOLog
28
30
 
29
- protected _logLevel:OINOLogLevel = OINOLogLevel.warn
31
+ protected _logLevels:Record<string, OINOLogLevel> = { "*|*|*": OINOLogLevel.warning }
30
32
 
31
33
  /**
32
34
  * Abstract logging method to implement the actual logging operation.
33
35
  *
34
- * @param logLevel level of the log events
36
+ * @param logLevel default loglevel for all log events
35
37
  *
36
38
  */
37
- constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
39
+ constructor (logLevel:OINOLogLevel = OINOLogLevel.warning) {
38
40
  // console.log("OINOLog.constructor: logLevel=" + logLevel)
39
- this._logLevel = logLevel
41
+ this._logLevels["*|*|*"] = logLevel
40
42
  }
41
43
 
42
44
 
@@ -44,11 +46,14 @@ export abstract class OINOLog {
44
46
  * Abstract logging method to implement the actual logging operation.
45
47
  *
46
48
  * @param levelStr level string of the log event
49
+ * @param domain domain of the log event
50
+ * @param channel channel of the log event
51
+ * @param method method of the log event
47
52
  * @param message message of the log event
48
53
  * @param data structured data associated with the log event
49
54
  *
50
55
  */
51
- protected abstract _writeLog(levelStr:string, message:string, data?:any):void
56
+ protected abstract _writeLog(levelStr:string, domain:string, channel:string, method:string, message:string, data?:any):void
52
57
 
53
58
  /**
54
59
  * Abstract logging method to implement the actual logging operation.
@@ -59,10 +64,16 @@ export abstract class OINOLog {
59
64
  * @param data structured data associated with the log event
60
65
  *
61
66
  */
62
- protected static _log(level:OINOLogLevel, levelStr:string, message:string, data?:any):void {
63
- // console.log("_log: level=" + level + ", levelStr=" + levelStr + ", message=" + message + ", data=" + data)
64
- if ((OINOLog._instance) && (OINOLog._instance._logLevel <= level)) {
65
- OINOLog._instance?._writeLog(levelStr, message, data)
67
+ protected static _log(level:OINOLogLevel, levelStr:string, domain:string, channel:string, method:string, message:string, data?:any):void {
68
+ const log_levels = OINOLog._instance!._logLevels
69
+ // console.log(log_levels)
70
+ const min_level = log_levels[domain + "|" + channel + "|" + method] ||
71
+ log_levels[domain + "|" + channel + "|*"] ||
72
+ log_levels[domain + "|*|*"] ||
73
+ log_levels["*|*|*"]
74
+ // console.log("_log: level=" + level + ", min_level=" + min_level + ", levelStr=" + levelStr + ", message=" + message, data)
75
+ if ((OINOLog._instance) && (level >= min_level)) {
76
+ OINOLog._instance?._writeLog(levelStr, domain, channel, method, message, data)
66
77
  }
67
78
  }
68
79
 
@@ -80,59 +91,94 @@ export abstract class OINOLog {
80
91
  }
81
92
 
82
93
  /**
83
- * Set log level.
94
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
95
+ * Multiple settings can be combined to set different logging accuracy specifically
96
+ *
97
+ * For example:
98
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
99
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
100
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
84
101
  *
85
102
  * @param logLevel log level to use
103
+ * @param domain domain of the log event (default: "*" for all)
104
+ * @param channel channel of the log event (default: "*" for all)
105
+ * @param method method of the log event (default: "*" for all)
86
106
  *
87
107
  */
88
- static setLogLevel(logLevel:OINOLogLevel) {
108
+ static setLogLevel(logLevel:OINOLogLevel, domain:string = "*", channel:string = "*", method:string = "*") {
89
109
  if (OINOLog._instance) {
90
- OINOLog._instance._logLevel = logLevel
110
+ OINOLog._instance._logLevels[domain + "|" + channel + "|" + method] = logLevel
91
111
  }
92
112
  }
93
113
 
94
114
  /**
95
- * Log error event.
115
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
96
116
  *
117
+ * @param domain domain of the log event
118
+ * @param channel channel of the log event
119
+ * @param method method of the log event
97
120
  * @param message message of the log event
98
121
  * @param data structured data associated with the log event
99
122
  *
100
123
  */
101
- static error(message:string, data?:any) {
102
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data)
124
+ static exception(domain:string, channel:string, method:string, message:string, data?:any) {
125
+ OINOLog._log(OINOLogLevel.exception, "EXCEPTION", domain, channel, method, message, data)
103
126
  }
104
127
 
105
128
  /**
106
- * Log warning event.
129
+ * Log error event. Error events are printed as a single line.
107
130
  *
131
+ * @param domain domain of the log event
132
+ * @param channel channel of the log event
133
+ * @param method method of the log event
108
134
  * @param message message of the log event
109
135
  * @param data structured data associated with the log event
110
136
  *
111
137
  */
112
- static warning(message:string, data?:any) {
113
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data)
138
+ static error(domain:string, channel:string, method:string, message:string, data?:any) {
139
+ OINOLog._log(OINOLogLevel.error, "ERROR", domain, channel, method, message, data)
114
140
  }
115
141
 
116
142
  /**
117
- * Log info event.
143
+ * Log warning event. Warning events are printed as a single line.
118
144
  *
145
+ * @param domain domain of the log event
146
+ * @param channel channel of the log event
147
+ * @param method method of the log event
119
148
  * @param message message of the log event
120
149
  * @param data structured data associated with the log event
121
150
  *
122
151
  */
123
- static info(message:string, data?:any) {
124
- OINOLog._log(OINOLogLevel.info, "INFO", message, data)
152
+ static warning(domain:string, channel:string, method:string, message:string, data?:any) {
153
+ OINOLog._log(OINOLogLevel.warning, "WARN", domain, channel, method, message, data)
125
154
  }
126
155
 
127
156
  /**
128
- * Log debug event.
157
+ * Log info event. Info events are printed as a single line.
129
158
  *
159
+ * @param domain domain of the log event
160
+ * @param channel channel of the log event
161
+ * @param method method of the log event
130
162
  * @param message message of the log event
131
163
  * @param data structured data associated with the log event
132
164
  *
133
165
  */
134
- static debug(message:string, data?:any) {
135
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data)
166
+ static info(domain:string, channel:string, method:string, message:string, data?:any) {
167
+ OINOLog._log(OINOLogLevel.info, "INFO", domain, channel, method, message, data)
168
+ }
169
+
170
+ /**
171
+ * Log debug event. Debug events are prettyprinted.
172
+ *
173
+ * @param domain domain of the log event
174
+ * @param channel channel of the log event
175
+ * @param method method of the log event
176
+ * @param message message of the log event
177
+ * @param data structured data associated with the log event
178
+ *
179
+ */
180
+ static debug(domain:string, channel:string, method:string, message:string, data?:any) {
181
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", domain, channel, method, message, data)
136
182
  }
137
183
  }
138
184
 
@@ -146,23 +192,33 @@ export class OINOConsoleLog extends OINOLog {
146
192
  * Constructor of `OINOConsoleLog`
147
193
  * @param logLevel logging level
148
194
  */
149
- constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
195
+ constructor (logLevel:OINOLogLevel = OINOLogLevel.warning) {
150
196
  super(logLevel)
151
197
  }
152
198
 
153
- protected _writeLog(level:string, message:string, data?:any):void {
154
- let log:string = "OINOLog " + level + ": " + message
155
- if (data) {
156
- log += " " + JSON.stringify(data)
199
+ protected _writeLog(level:string, domain:string, channel:string, method:string, message:string, data?:any):void {
200
+ if (message === undefined) {
201
+ console.log("OINOLog missing message: " + (new Error()).stack)
157
202
  }
158
- if (level == "ERROR") {
159
- console.error(log)
203
+ let log:string = "OINOLog." + level + " | " + domain + " | " + channel + " | " + method + ": " + message
204
+ let logger_func
205
+ if ((level == "ERROR") || (level == "EXCEPTION")) {
206
+ logger_func = console.error
160
207
  } else if (level == "WARN") {
161
- console.warn(log)
208
+ logger_func = console.warn
162
209
  } else if (level == "INFO") {
163
- console.info(log)
210
+ logger_func = console.info
211
+ } else {
212
+ logger_func = console.log
213
+ }
214
+ if (data && (level == "DEBUG")) {
215
+ logger_func(log, data)
216
+ } else if (data && (level == "EXCEPTION")) {
217
+ logger_func(log + JSON.stringify(data, null, 2).replaceAll(/[^\\]\\n/g, "\n")) // preserve newlines for stack traces
218
+ } else if (data) {
219
+ logger_func(log + " " + JSON.stringify(data))
164
220
  } else {
165
- console.log(log)
221
+ logger_func(log)
166
222
  }
167
223
  }
168
224
  }