@oino-ts/common 0.3.3 → 0.3.4

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.
package/src/OINOLog.ts CHANGED
@@ -1,168 +1,168 @@
1
- /*
2
- * This Source Code Form is subject to the terms of the Mozilla Public
3
- * License, v. 2.0. If a copy of the MPL was not distributed with this
4
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
- */
6
-
7
- /** Logging levels */
8
- export enum OINOLogLevel {
9
- /** Debug messages */
10
- debug=0,
11
- /** Informational messages */
12
- info=1,
13
- /** Warning messages */
14
- warn=2,
15
- /** Error messages */
16
- error=3
17
- }
18
-
19
- /**
20
- * Abstract base class for logging implementations supporting
21
- * - error, warning, info and debug channels
22
- * - setting level of logs outputted
23
- *
24
- */
25
-
26
- export abstract class OINOLog {
27
- protected static _instance:OINOLog
28
-
29
- protected _logLevel:OINOLogLevel = OINOLogLevel.warn
30
-
31
- /**
32
- * Abstract logging method to implement the actual logging operation.
33
- *
34
- * @param logLevel level of the log events
35
- *
36
- */
37
- constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
38
- // console.log("OINOLog.constructor: logLevel=" + logLevel)
39
- this._logLevel = logLevel
40
- }
41
-
42
-
43
- /**
44
- * Abstract logging method to implement the actual logging operation.
45
- *
46
- * @param levelStr level string of the log event
47
- * @param message message of the log event
48
- * @param data structured data associated with the log event
49
- *
50
- */
51
- protected abstract _writeLog(levelStr:string, message:string, data?:any):void
52
-
53
- /**
54
- * Abstract logging method to implement the actual logging operation.
55
- *
56
- * @param level level of the log event
57
- * @param levelStr level string of the log event
58
- * @param message message of the log event
59
- * @param data structured data associated with the log event
60
- *
61
- */
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)
66
- }
67
- }
68
-
69
- /**
70
- * Set active logger and log level.
71
- *
72
- * @param logger logger instance
73
- *
74
- */
75
- static setLogger(logger: OINOLog) {
76
- // console.log("setLogger: " + log)
77
- if (logger) {
78
- OINOLog._instance = logger
79
- }
80
- }
81
-
82
- /**
83
- * Set log level.
84
- *
85
- * @param logLevel log level to use
86
- *
87
- */
88
- static setLogLevel(logLevel:OINOLogLevel) {
89
- if (OINOLog._instance) {
90
- OINOLog._instance._logLevel = logLevel
91
- }
92
- }
93
-
94
- /**
95
- * Log error event.
96
- *
97
- * @param message message of the log event
98
- * @param data structured data associated with the log event
99
- *
100
- */
101
- static error(message:string, data?:any) {
102
- OINOLog._log(OINOLogLevel.error, "ERROR", message, data)
103
- }
104
-
105
- /**
106
- * Log warning event.
107
- *
108
- * @param message message of the log event
109
- * @param data structured data associated with the log event
110
- *
111
- */
112
- static warning(message:string, data?:any) {
113
- OINOLog._log(OINOLogLevel.warn, "WARN", message, data)
114
- }
115
-
116
- /**
117
- * Log info event.
118
- *
119
- * @param message message of the log event
120
- * @param data structured data associated with the log event
121
- *
122
- */
123
- static info(message:string, data?:any) {
124
- OINOLog._log(OINOLogLevel.info, "INFO", message, data)
125
- }
126
-
127
- /**
128
- * Log debug event.
129
- *
130
- * @param message message of the log event
131
- * @param data structured data associated with the log event
132
- *
133
- */
134
- static debug(message:string, data?:any) {
135
- OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data)
136
- }
137
- }
138
-
139
- /**
140
- * Logging implementation based on console.log.
141
- *
142
- */
143
- export class OINOConsoleLog extends OINOLog {
144
-
145
- /**
146
- * Constructor of `OINOConsoleLog`
147
- * @param logLevel logging level
148
- */
149
- constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
150
- super(logLevel)
151
- }
152
-
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)
157
- }
158
- if (level == "ERROR") {
159
- console.error(log)
160
- } else if (level == "WARN") {
161
- console.warn(log)
162
- } else if (level == "INFO") {
163
- console.info(log)
164
- } else {
165
- console.log(log)
166
- }
167
- }
168
- }
1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+
7
+ /** Logging levels */
8
+ export enum OINOLogLevel {
9
+ /** Debug messages */
10
+ debug=0,
11
+ /** Informational messages */
12
+ info=1,
13
+ /** Warning messages */
14
+ warn=2,
15
+ /** Error messages */
16
+ error=3
17
+ }
18
+
19
+ /**
20
+ * Abstract base class for logging implementations supporting
21
+ * - error, warning, info and debug channels
22
+ * - setting level of logs outputted
23
+ *
24
+ */
25
+
26
+ export abstract class OINOLog {
27
+ protected static _instance:OINOLog
28
+
29
+ protected _logLevel:OINOLogLevel = OINOLogLevel.warn
30
+
31
+ /**
32
+ * Abstract logging method to implement the actual logging operation.
33
+ *
34
+ * @param logLevel level of the log events
35
+ *
36
+ */
37
+ constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
38
+ // console.log("OINOLog.constructor: logLevel=" + logLevel)
39
+ this._logLevel = logLevel
40
+ }
41
+
42
+
43
+ /**
44
+ * Abstract logging method to implement the actual logging operation.
45
+ *
46
+ * @param levelStr level string of the log event
47
+ * @param message message of the log event
48
+ * @param data structured data associated with the log event
49
+ *
50
+ */
51
+ protected abstract _writeLog(levelStr:string, message:string, data?:any):void
52
+
53
+ /**
54
+ * Abstract logging method to implement the actual logging operation.
55
+ *
56
+ * @param level level of the log event
57
+ * @param levelStr level string of the log event
58
+ * @param message message of the log event
59
+ * @param data structured data associated with the log event
60
+ *
61
+ */
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)
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Set active logger and log level.
71
+ *
72
+ * @param logger logger instance
73
+ *
74
+ */
75
+ static setLogger(logger: OINOLog) {
76
+ // console.log("setLogger: " + log)
77
+ if (logger) {
78
+ OINOLog._instance = logger
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Set log level.
84
+ *
85
+ * @param logLevel log level to use
86
+ *
87
+ */
88
+ static setLogLevel(logLevel:OINOLogLevel) {
89
+ if (OINOLog._instance) {
90
+ OINOLog._instance._logLevel = logLevel
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Log error event.
96
+ *
97
+ * @param message message of the log event
98
+ * @param data structured data associated with the log event
99
+ *
100
+ */
101
+ static error(message:string, data?:any) {
102
+ OINOLog._log(OINOLogLevel.error, "ERROR", message, data)
103
+ }
104
+
105
+ /**
106
+ * Log warning event.
107
+ *
108
+ * @param message message of the log event
109
+ * @param data structured data associated with the log event
110
+ *
111
+ */
112
+ static warning(message:string, data?:any) {
113
+ OINOLog._log(OINOLogLevel.warn, "WARN", message, data)
114
+ }
115
+
116
+ /**
117
+ * Log info event.
118
+ *
119
+ * @param message message of the log event
120
+ * @param data structured data associated with the log event
121
+ *
122
+ */
123
+ static info(message:string, data?:any) {
124
+ OINOLog._log(OINOLogLevel.info, "INFO", message, data)
125
+ }
126
+
127
+ /**
128
+ * Log debug event.
129
+ *
130
+ * @param message message of the log event
131
+ * @param data structured data associated with the log event
132
+ *
133
+ */
134
+ static debug(message:string, data?:any) {
135
+ OINOLog._log(OINOLogLevel.debug, "DEBUG", message, data)
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Logging implementation based on console.log.
141
+ *
142
+ */
143
+ export class OINOConsoleLog extends OINOLog {
144
+
145
+ /**
146
+ * Constructor of `OINOConsoleLog`
147
+ * @param logLevel logging level
148
+ */
149
+ constructor (logLevel:OINOLogLevel = OINOLogLevel.warn) {
150
+ super(logLevel)
151
+ }
152
+
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)
157
+ }
158
+ if (level == "ERROR") {
159
+ console.error(log)
160
+ } else if (level == "WARN") {
161
+ console.warn(log)
162
+ } else if (level == "INFO") {
163
+ console.info(log)
164
+ } else {
165
+ console.log(log)
166
+ }
167
+ }
168
+ }