@oino-ts/common 0.7.2 → 0.8.1

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,18 @@ 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 + "||" + method] ||
56
+ log_levels[domain + "|" + channel + "|"] ||
57
+ log_levels["|" + channel + "|"] ||
58
+ log_levels[domain + "||"] ||
59
+ log_levels["||"];
60
+ // console.log("_log: level=" + level + ", min_level=" + min_level + ", levelStr=" + levelStr + ", message=" + message, data)
61
+ if ((OINOLog._instance) && (level >= min_level)) {
62
+ OINOLog._instance?._writeLog(levelStr, domain, channel, method, message, data);
53
63
  }
54
64
  }
55
65
  /**
@@ -65,55 +75,89 @@ class OINOLog {
65
75
  }
66
76
  }
67
77
  /**
68
- * Set log level.
78
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
79
+ * Multiple settings can be combined to set different logging accuracy specifically
80
+ *
81
+ * For example:
82
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
83
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
84
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
69
85
  *
70
86
  * @param logLevel log level to use
87
+ * @param domain domain of the log event (default: "*" for all)
88
+ * @param channel channel of the log event (default: "*" for all)
89
+ * @param method method of the log event (default: "*" for all)
71
90
  *
72
91
  */
73
- static setLogLevel(logLevel) {
92
+ static setLogLevel(logLevel, domain = "", channel = "", method = "") {
74
93
  if (OINOLog._instance) {
75
- OINOLog._instance._logLevel = logLevel;
94
+ OINOLog._instance._logLevels[domain + "|" + channel + "|" + method] = logLevel;
76
95
  }
77
96
  }
78
97
  /**
79
- * Log error event.
98
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
80
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
81
103
  * @param message message of the log event
82
104
  * @param data structured data associated with the log event
83
105
  *
84
106
  */
85
- static error(message, data) {
86
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data);
107
+ static exception(domain, channel, method, message, data) {
108
+ OINOLog._log(OINOLogLevel.exception, "EXCEPTION", domain, channel, method, message, data);
87
109
  }
88
110
  /**
89
- * Log warning event.
111
+ * Log error event. Error events are printed as a single line.
90
112
  *
113
+ * @param domain domain of the log event
114
+ * @param channel channel of the log event
115
+ * @param method method of the log event
91
116
  * @param message message of the log event
92
117
  * @param data structured data associated with the log event
93
118
  *
94
119
  */
95
- static warning(message, data) {
96
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data);
120
+ static error(domain, channel, method, message, data) {
121
+ OINOLog._log(OINOLogLevel.error, "ERROR", domain, channel, method, message, data);
97
122
  }
98
123
  /**
99
- * Log info event.
124
+ * Log warning event. Warning events are printed as a single line.
100
125
  *
126
+ * @param domain domain of the log event
127
+ * @param channel channel of the log event
128
+ * @param method method of the log event
101
129
  * @param message message of the log event
102
130
  * @param data structured data associated with the log event
103
131
  *
104
132
  */
105
- static info(message, data) {
106
- OINOLog._log(OINOLogLevel.info, "INFO", message, data);
133
+ static warning(domain, channel, method, message, data) {
134
+ OINOLog._log(OINOLogLevel.warning, "WARN", domain, channel, method, message, data);
107
135
  }
108
136
  /**
109
- * Log debug event.
137
+ * Log info event. Info events are printed as a single line.
110
138
  *
139
+ * @param domain domain of the log event
140
+ * @param channel channel of the log event
141
+ * @param method method of the log event
111
142
  * @param message message of the log event
112
143
  * @param data structured data associated with the log event
113
144
  *
114
145
  */
115
- static debug(message, data) {
116
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data);
146
+ static info(domain, channel, method, message, data) {
147
+ OINOLog._log(OINOLogLevel.info, "INFO", domain, channel, method, message, data);
148
+ }
149
+ /**
150
+ * Log debug event. Debug events are prettyprinted.
151
+ *
152
+ * @param domain domain of the log event
153
+ * @param channel channel of the log event
154
+ * @param method method of the log event
155
+ * @param message message of the log event
156
+ * @param data structured data associated with the log event
157
+ *
158
+ */
159
+ static debug(domain, channel, method, message, data) {
160
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", domain, channel, method, message, data);
117
161
  }
118
162
  }
119
163
  exports.OINOLog = OINOLog;
@@ -126,25 +170,38 @@ class OINOConsoleLog extends OINOLog {
126
170
  * Constructor of `OINOConsoleLog`
127
171
  * @param logLevel logging level
128
172
  */
129
- constructor(logLevel = OINOLogLevel.warn) {
173
+ constructor(logLevel = OINOLogLevel.warning) {
130
174
  super(logLevel);
131
175
  }
132
- _writeLog(level, message, data) {
133
- let log = "OINOLog " + level + ": " + message;
134
- if (data) {
135
- log += " " + JSON.stringify(data);
176
+ _writeLog(level, domain, channel, method, message, data) {
177
+ if (message === undefined) {
178
+ console.log("OINOLog missing message: " + (new Error()).stack);
136
179
  }
137
- if (level == "ERROR") {
138
- console.error(log);
180
+ let log = "OINOLog." + level + " | " + domain + " | " + channel + " | " + method + ": " + message;
181
+ let logger_func;
182
+ if ((level == "ERROR") || (level == "EXCEPTION")) {
183
+ logger_func = console.error;
139
184
  }
140
185
  else if (level == "WARN") {
141
- console.warn(log);
186
+ logger_func = console.warn;
142
187
  }
143
188
  else if (level == "INFO") {
144
- console.info(log);
189
+ logger_func = console.info;
190
+ }
191
+ else {
192
+ logger_func = console.log;
193
+ }
194
+ if (data && (level == "DEBUG")) {
195
+ logger_func(log, data);
196
+ }
197
+ else if (data && (level == "EXCEPTION")) {
198
+ logger_func(log + JSON.stringify(data, null, 2).replaceAll(/[^\\]\\n/g, "\n")); // preserve newlines for stack traces
199
+ }
200
+ else if (data) {
201
+ logger_func(log + " " + JSON.stringify(data));
145
202
  }
146
203
  else {
147
- console.log(log);
204
+ logger_func(log);
148
205
  }
149
206
  }
150
207
  }
@@ -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,18 @@ 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 + "||" + method] ||
53
+ log_levels[domain + "|" + channel + "|"] ||
54
+ log_levels["|" + channel + "|"] ||
55
+ log_levels[domain + "||"] ||
56
+ log_levels["||"];
57
+ // console.log("_log: level=" + level + ", min_level=" + min_level + ", levelStr=" + levelStr + ", message=" + message, data)
58
+ if ((OINOLog._instance) && (level >= min_level)) {
59
+ OINOLog._instance?._writeLog(levelStr, domain, channel, method, message, data);
50
60
  }
51
61
  }
52
62
  /**
@@ -62,55 +72,89 @@ export class OINOLog {
62
72
  }
63
73
  }
64
74
  /**
65
- * Set log level.
75
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
76
+ * Multiple settings can be combined to set different logging accuracy specifically
77
+ *
78
+ * For example:
79
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
80
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
81
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
66
82
  *
67
83
  * @param logLevel log level to use
84
+ * @param domain domain of the log event (default: "*" for all)
85
+ * @param channel channel of the log event (default: "*" for all)
86
+ * @param method method of the log event (default: "*" for all)
68
87
  *
69
88
  */
70
- static setLogLevel(logLevel) {
89
+ static setLogLevel(logLevel, domain = "", channel = "", method = "") {
71
90
  if (OINOLog._instance) {
72
- OINOLog._instance._logLevel = logLevel;
91
+ OINOLog._instance._logLevels[domain + "|" + channel + "|" + method] = logLevel;
73
92
  }
74
93
  }
75
94
  /**
76
- * Log error event.
95
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
77
96
  *
97
+ * @param domain domain of the log event
98
+ * @param channel channel of the log event
99
+ * @param method method of the log event
78
100
  * @param message message of the log event
79
101
  * @param data structured data associated with the log event
80
102
  *
81
103
  */
82
- static error(message, data) {
83
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data);
104
+ static exception(domain, channel, method, message, data) {
105
+ OINOLog._log(OINOLogLevel.exception, "EXCEPTION", domain, channel, method, message, data);
84
106
  }
85
107
  /**
86
- * Log warning event.
108
+ * Log error event. Error events are printed as a single line.
87
109
  *
110
+ * @param domain domain of the log event
111
+ * @param channel channel of the log event
112
+ * @param method method of the log event
88
113
  * @param message message of the log event
89
114
  * @param data structured data associated with the log event
90
115
  *
91
116
  */
92
- static warning(message, data) {
93
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data);
117
+ static error(domain, channel, method, message, data) {
118
+ OINOLog._log(OINOLogLevel.error, "ERROR", domain, channel, method, message, data);
94
119
  }
95
120
  /**
96
- * Log info event.
121
+ * Log warning event. Warning events are printed as a single line.
97
122
  *
123
+ * @param domain domain of the log event
124
+ * @param channel channel of the log event
125
+ * @param method method of the log event
98
126
  * @param message message of the log event
99
127
  * @param data structured data associated with the log event
100
128
  *
101
129
  */
102
- static info(message, data) {
103
- OINOLog._log(OINOLogLevel.info, "INFO", message, data);
130
+ static warning(domain, channel, method, message, data) {
131
+ OINOLog._log(OINOLogLevel.warning, "WARN", domain, channel, method, message, data);
104
132
  }
105
133
  /**
106
- * Log debug event.
134
+ * Log info event. Info events are printed as a single line.
107
135
  *
136
+ * @param domain domain of the log event
137
+ * @param channel channel of the log event
138
+ * @param method method of the log event
108
139
  * @param message message of the log event
109
140
  * @param data structured data associated with the log event
110
141
  *
111
142
  */
112
- static debug(message, data) {
113
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data);
143
+ static info(domain, channel, method, message, data) {
144
+ OINOLog._log(OINOLogLevel.info, "INFO", domain, channel, method, message, data);
145
+ }
146
+ /**
147
+ * Log debug event. Debug events are prettyprinted.
148
+ *
149
+ * @param domain domain of the log event
150
+ * @param channel channel of the log event
151
+ * @param method method of the log event
152
+ * @param message message of the log event
153
+ * @param data structured data associated with the log event
154
+ *
155
+ */
156
+ static debug(domain, channel, method, message, data) {
157
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", domain, channel, method, message, data);
114
158
  }
115
159
  }
116
160
  /**
@@ -122,25 +166,38 @@ export class OINOConsoleLog extends OINOLog {
122
166
  * Constructor of `OINOConsoleLog`
123
167
  * @param logLevel logging level
124
168
  */
125
- constructor(logLevel = OINOLogLevel.warn) {
169
+ constructor(logLevel = OINOLogLevel.warning) {
126
170
  super(logLevel);
127
171
  }
128
- _writeLog(level, message, data) {
129
- let log = "OINOLog " + level + ": " + message;
130
- if (data) {
131
- log += " " + JSON.stringify(data);
172
+ _writeLog(level, domain, channel, method, message, data) {
173
+ if (message === undefined) {
174
+ console.log("OINOLog missing message: " + (new Error()).stack);
132
175
  }
133
- if (level == "ERROR") {
134
- console.error(log);
176
+ let log = "OINOLog." + level + " | " + domain + " | " + channel + " | " + method + ": " + message;
177
+ let logger_func;
178
+ if ((level == "ERROR") || (level == "EXCEPTION")) {
179
+ logger_func = console.error;
135
180
  }
136
181
  else if (level == "WARN") {
137
- console.warn(log);
182
+ logger_func = console.warn;
138
183
  }
139
184
  else if (level == "INFO") {
140
- console.info(log);
185
+ logger_func = console.info;
186
+ }
187
+ else {
188
+ logger_func = console.log;
189
+ }
190
+ if (data && (level == "DEBUG")) {
191
+ logger_func(log, data);
192
+ }
193
+ else if (data && (level == "EXCEPTION")) {
194
+ logger_func(log + JSON.stringify(data, null, 2).replaceAll(/[^\\]\\n/g, "\n")); // preserve newlines for stack traces
195
+ }
196
+ else if (data) {
197
+ logger_func(log + " " + JSON.stringify(data));
141
198
  }
142
199
  else {
143
- console.log(log);
200
+ logger_func(log);
144
201
  }
145
202
  }
146
203
  }
@@ -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.1",
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.1"
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,18 @@ 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 + "||" + method] ||
72
+ log_levels[domain + "|" + channel + "|"] ||
73
+ log_levels["|" + channel + "|"] ||
74
+ log_levels[domain + "||"] ||
75
+ log_levels["||"]
76
+ // console.log("_log: level=" + level + ", min_level=" + min_level + ", levelStr=" + levelStr + ", message=" + message, data)
77
+ if ((OINOLog._instance) && (level >= min_level)) {
78
+ OINOLog._instance?._writeLog(levelStr, domain, channel, method, message, data)
66
79
  }
67
80
  }
68
81
 
@@ -80,59 +93,94 @@ export abstract class OINOLog {
80
93
  }
81
94
 
82
95
  /**
83
- * Set log level.
96
+ * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
97
+ * Multiple settings can be combined to set different logging accuracy specifically
98
+ *
99
+ * For example:
100
+ * logLevel: warning, domain: *, channel: *, method: * will only output error events.
101
+ * logLevel: debug, domain: d1, channel: c1, method: "*" will enable debug events for channel c1 of domain d1.
102
+ * logLevel: info, domain: d1, channel: c1, method: m1 will supress debug events for method m1.
84
103
  *
85
104
  * @param logLevel log level to use
105
+ * @param domain domain of the log event (default: "*" for all)
106
+ * @param channel channel of the log event (default: "*" for all)
107
+ * @param method method of the log event (default: "*" for all)
86
108
  *
87
109
  */
88
- static setLogLevel(logLevel:OINOLogLevel) {
110
+ static setLogLevel(logLevel:OINOLogLevel, domain:string = "", channel:string = "", method:string = "") {
89
111
  if (OINOLog._instance) {
90
- OINOLog._instance._logLevel = logLevel
112
+ OINOLog._instance._logLevels[domain + "|" + channel + "|" + method] = logLevel
91
113
  }
92
114
  }
93
115
 
94
116
  /**
95
- * Log error event.
117
+ * Log exception event. Exception events are prettyprinted and preserve newlines so that stack traces are readable.
96
118
  *
119
+ * @param domain domain of the log event
120
+ * @param channel channel of the log event
121
+ * @param method method of the log event
97
122
  * @param message message of the log event
98
123
  * @param data structured data associated with the log event
99
124
  *
100
125
  */
101
- static error(message:string, data?:any) {
102
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data)
126
+ static exception(domain:string, channel:string, method:string, message:string, data?:any) {
127
+ OINOLog._log(OINOLogLevel.exception, "EXCEPTION", domain, channel, method, message, data)
103
128
  }
104
129
 
105
130
  /**
106
- * Log warning event.
131
+ * Log error event. Error events are printed as a single line.
107
132
  *
133
+ * @param domain domain of the log event
134
+ * @param channel channel of the log event
135
+ * @param method method of the log event
108
136
  * @param message message of the log event
109
137
  * @param data structured data associated with the log event
110
138
  *
111
139
  */
112
- static warning(message:string, data?:any) {
113
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data)
140
+ static error(domain:string, channel:string, method:string, message:string, data?:any) {
141
+ OINOLog._log(OINOLogLevel.error, "ERROR", domain, channel, method, message, data)
114
142
  }
115
143
 
116
144
  /**
117
- * Log info event.
145
+ * Log warning event. Warning events are printed as a single line.
118
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
119
150
  * @param message message of the log event
120
151
  * @param data structured data associated with the log event
121
152
  *
122
153
  */
123
- static info(message:string, data?:any) {
124
- OINOLog._log(OINOLogLevel.info, "INFO", message, data)
154
+ static warning(domain:string, channel:string, method:string, message:string, data?:any) {
155
+ OINOLog._log(OINOLogLevel.warning, "WARN", domain, channel, method, message, data)
125
156
  }
126
157
 
127
158
  /**
128
- * Log debug event.
159
+ * Log info event. Info events are printed as a single line.
129
160
  *
161
+ * @param domain domain of the log event
162
+ * @param channel channel of the log event
163
+ * @param method method of the log event
130
164
  * @param message message of the log event
131
165
  * @param data structured data associated with the log event
132
166
  *
133
167
  */
134
- static debug(message:string, data?:any) {
135
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data)
168
+ static info(domain:string, channel:string, method:string, message:string, data?:any) {
169
+ OINOLog._log(OINOLogLevel.info, "INFO", domain, channel, method, message, data)
170
+ }
171
+
172
+ /**
173
+ * Log debug event. Debug events are prettyprinted.
174
+ *
175
+ * @param domain domain of the log event
176
+ * @param channel channel of the log event
177
+ * @param method method of the log event
178
+ * @param message message of the log event
179
+ * @param data structured data associated with the log event
180
+ *
181
+ */
182
+ static debug(domain:string, channel:string, method:string, message:string, data?:any) {
183
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", domain, channel, method, message, data)
136
184
  }
137
185
  }
138
186
 
@@ -146,23 +194,33 @@ export class OINOConsoleLog extends OINOLog {
146
194
  * Constructor of `OINOConsoleLog`
147
195
  * @param logLevel logging level
148
196
  */
149
- constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
197
+ constructor (logLevel:OINOLogLevel = OINOLogLevel.warning) {
150
198
  super(logLevel)
151
199
  }
152
200
 
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)
201
+ protected _writeLog(level:string, domain:string, channel:string, method:string, message:string, data?:any):void {
202
+ if (message === undefined) {
203
+ console.log("OINOLog missing message: " + (new Error()).stack)
157
204
  }
158
- if (level == "ERROR") {
159
- console.error(log)
205
+ let log:string = "OINOLog." + level + " | " + domain + " | " + channel + " | " + method + ": " + message
206
+ let logger_func
207
+ if ((level == "ERROR") || (level == "EXCEPTION")) {
208
+ logger_func = console.error
160
209
  } else if (level == "WARN") {
161
- console.warn(log)
210
+ logger_func = console.warn
162
211
  } else if (level == "INFO") {
163
- console.info(log)
212
+ logger_func = console.info
213
+ } else {
214
+ logger_func = console.log
215
+ }
216
+ if (data && (level == "DEBUG")) {
217
+ logger_func(log, data)
218
+ } else if (data && (level == "EXCEPTION")) {
219
+ logger_func(log + JSON.stringify(data, null, 2).replaceAll(/[^\\]\\n/g, "\n")) // preserve newlines for stack traces
220
+ } else if (data) {
221
+ logger_func(log + " " + JSON.stringify(data))
164
222
  } else {
165
- console.log(log)
223
+ logger_func(log)
166
224
  }
167
225
  }
168
226
  }