@creejs/commons-logging 1.0.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.
@@ -0,0 +1,90 @@
1
+ 'use strict'
2
+ // internal
3
+ const { TypeAssert: { assertString } } = require('@creejs/commons-lang')
4
+ // owned
5
+ // eslint-disable-next-line no-unused-vars
6
+ const Logger = require('../logger')
7
+ // eslint-disable-next-line no-unused-vars
8
+ const { Level, value2Name } = require('../log-level')
9
+ const LogFactory = require('../log-factory')
10
+
11
+ const Log4jsLogger = require('./log4js-logger')
12
+ const DefaultConfig = require('./log4js-6x-config')
13
+
14
+ /**
15
+ * Use log4js as the logging provider.
16
+ */
17
+ class Log4jsFactory extends LogFactory {
18
+ /**
19
+ * the log4js module instance.
20
+ * @returns {{}} The log4js module instance.
21
+ */
22
+ get log4js () {
23
+ return this._libraryModule
24
+ }
25
+
26
+ get setting () {
27
+ if (this._setting == null) {
28
+ this._setting = cloneDefaultConfig()
29
+ }
30
+ return this._setting
31
+ }
32
+
33
+ /**
34
+ * Initializes the log4js provider by configuring it with the provided settings.
35
+ * @override
36
+ */
37
+ init () {
38
+ // @ts-ignore
39
+ this.log4js.configure(this.setting)
40
+ }
41
+
42
+ /**
43
+ * Update factory's Log Level
44
+ * 1. Only Provider knows how to update
45
+ * @param {number} level - The log level to set, see {@link LogLevel.Level}
46
+ * @returns {void}
47
+ * @throws {Error} Throws an error as this method is Not Impled Yet.
48
+ * @abstract
49
+ */
50
+ setLevel (level) {
51
+ let levelName = value2Name(level)
52
+ if (levelName == null) {
53
+ throw new Error(`Invalid log level: ${level}`)
54
+ }
55
+ levelName = levelName.toLowerCase()
56
+ let updated = false
57
+ // @ts-ignore
58
+ const { categories } = this.setting
59
+ for (const categoryKey in categories) {
60
+ const category = categories[categoryKey]
61
+ const { level: categoryLevel } = category
62
+ if (categoryLevel !== levelName) {
63
+ category.level = levelName.toLowerCase()
64
+ updated = true
65
+ }
66
+ }
67
+ // init log4js again
68
+ updated && this.init()
69
+ }
70
+
71
+ /**
72
+ * Creates a new logger instance with the specified name and log level.
73
+ * @param {string} loggerName - The name of the logger to create.
74
+ * @returns {Logger} A new logger instance configured with the given name and level.
75
+ * @override
76
+ */
77
+ createLogger (loggerName) {
78
+ assertString(loggerName)
79
+ // @ts-ignore
80
+ const nativeLogger = this.log4js.getLogger(loggerName)
81
+ const logger = new Log4jsLogger(loggerName, nativeLogger)
82
+ return logger
83
+ }
84
+ }
85
+
86
+ function cloneDefaultConfig () {
87
+ return JSON.parse(JSON.stringify(DefaultConfig))
88
+ }
89
+
90
+ module.exports = Log4jsFactory
@@ -0,0 +1,111 @@
1
+ 'use strict'
2
+
3
+ // 3rd
4
+ // internal
5
+ // owned
6
+ const { assertLevel, value2Name, name2Value, Level } = require('../log-level')
7
+ const Logger = require('../logger')
8
+ // module vars
9
+
10
+ class Log4jsLogger extends Logger {
11
+ /**
12
+ * Creates a new ConsoleLogger instance.
13
+ * @constructor
14
+ * @param {string} name - The name of the logger.
15
+ * @param {*} nativeLogger - Log4js library
16
+ */
17
+ constructor (name, nativeLogger) {
18
+ super(name, nativeLogger)
19
+ // @ts-ignore
20
+ this._level = undefined // MUST NOT accept level from outside
21
+ this._logger = nativeLogger
22
+ }
23
+
24
+ /**
25
+ * 1. log4js may have many categories(loggerName) with diffirent level
26
+ * 2. return the underlying log4js native logger's level
27
+ * @returns {number} The current log level.
28
+ */
29
+ get level () {
30
+ if (this._level == null) {
31
+ const levelName = this._logger.level.levelStr
32
+ if (levelName == null) {
33
+ return Level.OFF
34
+ }
35
+ const level = name2Value(levelName)
36
+ if (level == null) {
37
+ throw new Error(`Unknown level name: ${levelName}`)
38
+ }
39
+ return level
40
+ }
41
+ return this._level
42
+ }
43
+
44
+ /**
45
+ * set log4js native logger's level
46
+ * @param {number} level
47
+ * @override
48
+ * @protectd
49
+ */
50
+ _setLevel (level) {
51
+ assertLevel(level)
52
+ const levelName = value2Name(level)
53
+ if (levelName == null) {
54
+ throw new Error(`Invalid log level: ${level}`)
55
+ }
56
+ // log4js use string level, eg. 'error, trace'
57
+ this._logger.level = levelName.toLowerCase()
58
+ }
59
+
60
+ /**
61
+ * Logs a fatal error message with timestamp and logger name.
62
+ * Only outputs if fatal logging is enabled for this logger instance.
63
+ * @param {...any} args - Arguments to log (will be space-separated)
64
+ */
65
+ _fatal (...args) {
66
+ this._logger.fatal(...args)
67
+ }
68
+
69
+ /**
70
+ * Logs an error message to the console with timestamp and logger name.
71
+ * Only logs if error logging is enabled for this logger instance.
72
+ * @param {...any} args - Arguments to be logged as error message
73
+ */
74
+ _error (...args) {
75
+ this._logger.error(...args)
76
+ }
77
+
78
+ /**
79
+ * Logs a warning message to the console if warn logging is enabled.
80
+ * @param {...any} args - The arguments to log as a warning message.
81
+ */
82
+ _warn (...args) {
83
+ this._logger.warn(...args)
84
+ }
85
+
86
+ /**
87
+ * Logs debug messages to console if debug mode is enabled.
88
+ * @param {...any} args - The data to be logged
89
+ */
90
+ _debug (...args) {
91
+ this._logger.debug(...args)
92
+ }
93
+
94
+ /**
95
+ * Logs an info message to the console with timestamp and logger name.
96
+ * @param {...any} args - The data to be logged. Accepts multiple arguments.
97
+ */
98
+ _info (...args) {
99
+ this._logger.info(...args)
100
+ }
101
+
102
+ /**
103
+ * Logs a trace message with timestamp and logger name if trace logging is enabled.
104
+ * @param {...any} args - Data to be logged as trace message.
105
+ */
106
+ _trace (...args) {
107
+ this._logger.trace(...args)
108
+ }
109
+ }
110
+
111
+ module.exports = Log4jsLogger
@@ -0,0 +1,35 @@
1
+ 'use strict'
2
+ // internal
3
+ const { TypeAssert: { assertNotNil } } = require('@creejs/commons-lang')
4
+
5
+ // owned
6
+ const ProviderType = require('../provider-type')
7
+ const Provider = require('../provider')
8
+ const Log4jsFactory = require('./log4js-factory')
9
+
10
+ class Log4jsProvider extends Provider {
11
+ /**
12
+ * Gets the provider type (Log4js).
13
+ * @returns {string} "LOG4JS"
14
+ * @override
15
+ */
16
+ getType () {
17
+ return ProviderType.Log4js
18
+ }
19
+
20
+ /**
21
+ * Creates a new Provider instance.
22
+ * @param {*} libraryModule - The library module to be used.
23
+ * @param {*} setting - Configuration settings for the provider.
24
+ * @returns {Log4jsFactory} A new instance of Log4jsFactory.
25
+ * @override
26
+ */
27
+ createLogFactory (libraryModule, setting) {
28
+ assertNotNil(libraryModule)
29
+ const factory = new Log4jsFactory(libraryModule, setting)
30
+ factory.init()
31
+ return factory
32
+ }
33
+ }
34
+
35
+ module.exports = Log4jsProvider
package/lib/logger.js ADDED
@@ -0,0 +1,285 @@
1
+ 'use strict'
2
+
3
+ const { assertString } = require('@creejs/commons-lang/lib/type-assert')
4
+ const LogLevel = require('./log-level')
5
+ const { Level, DefaultLevel, assertLevel } = require('./log-level')
6
+
7
+ /**
8
+ * Abstract Logger Class of All Logger
9
+ * @abstract
10
+ */
11
+ class Logger {
12
+ /**
13
+ * Creates a new Logger instance.
14
+ * @constructor
15
+ * @param {string} name - The name identifier for the logger
16
+ * @param {*} [nativeLogger]
17
+ * @param {number} [level] - The logging level, default is Level.ERROR=1
18
+ */
19
+ constructor (name, nativeLogger, level = DefaultLevel) {
20
+ assertString(name)
21
+ assertLevel(level)
22
+ this._name = name
23
+ this._nativeLogger = nativeLogger
24
+ this._level = level
25
+ }
26
+
27
+ get nativeLogger () {
28
+ return this._nativeLogger
29
+ }
30
+
31
+ get level () {
32
+ return this._level
33
+ }
34
+
35
+ /**
36
+ * Gets the name of the logger instance.
37
+ * @returns {string} The name of the logger.
38
+ */
39
+ get name () {
40
+ return this._name
41
+ }
42
+
43
+ /**
44
+ * Checks if FATAL level logging is enabled for this logger.
45
+ * @returns {boolean} True if FATAL level logging is enabled, false otherwise.
46
+ */
47
+ get fatalEnabled () {
48
+ return this.level >= Level.FATAL
49
+ }
50
+
51
+ /**
52
+ * Checks if ERROR level logging is enabled for this logger.
53
+ * @returns {boolean} True if ERROR level logging is enabled, false otherwise.
54
+ */
55
+ get errorEnabled () {
56
+ return this.level >= Level.ERROR
57
+ }
58
+
59
+ /**
60
+ * Checks if WARN level logging is enabled for this logger.
61
+ * @returns {boolean} True if WARN level logging is enabled, false otherwise.
62
+ */
63
+ get warnEnabled () {
64
+ return this.level >= Level.WARN
65
+ }
66
+
67
+ /**
68
+ * Checks if DEBUG level logging is enabled for this logger.
69
+ * @returns {boolean} True if DEBUG level logging is enabled, false otherwise.
70
+ */
71
+ get debugEnabled () {
72
+ return this.level >= Level.DEBUG
73
+ }
74
+
75
+ /**
76
+ * Checks if INFO level logging is enabled for this logger.
77
+ * @returns {boolean} True if INFO level logging is enabled, false otherwise.
78
+ */
79
+ get infoEnabled () {
80
+ return this.level >= Level.INFO
81
+ }
82
+
83
+ /**
84
+ * Checks if TRACE level logging is enabled for this logger.
85
+ * @returns {boolean} True if TRACE level logging is enabled, false otherwise.
86
+ */
87
+ get traceEnabled () {
88
+ return this.level >= Level.TRACE
89
+ }
90
+
91
+ /**
92
+ * change log level for current logger instance.
93
+ * @param {number} level - new log level to set
94
+ * @throws {Error} Currently throws an error as this method is Not Impled Yet.
95
+ * @abstract
96
+ */
97
+ setLevel (level) {
98
+ LogLevel.assertLevel(level)
99
+ this._setLevel(level)
100
+ this._level = level
101
+ }
102
+
103
+ /**
104
+ * Logs a fatal error message with timestamp and logger name.
105
+ * Only outputs if fatal logging is enabled for this logger instance.
106
+ * @param {...any} args - Arguments to log (will be space-separated)
107
+ */
108
+ fatal (...args) {
109
+ if (this.fatalEnabled) {
110
+ this._fatal(...args)
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Logs an error message to the console with timestamp and logger name.
116
+ * Only logs if error logging is enabled for this logger instance.
117
+ * @param {...any} args - Arguments to be logged as error message
118
+ */
119
+ error (...args) {
120
+ if (this.errorEnabled) {
121
+ this._error(...args)
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Logs a warning message to the console if warn logging is enabled.
127
+ * @param {...any} args - The arguments to log as a warning message.
128
+ */
129
+ warn (...args) {
130
+ if (this.warnEnabled) {
131
+ this._warn(...args)
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Logs debug messages to console if debug mode is enabled.
137
+ * @param {...any} args - The data to be logged
138
+ */
139
+ debug (...args) {
140
+ if (this.debugEnabled) {
141
+ this._debug(...args)
142
+ }
143
+ }
144
+
145
+ /**
146
+ * Logs an info message to the console with timestamp and logger name.
147
+ * @param {...any} args - The data to be logged. Accepts multiple arguments.
148
+ */
149
+ info (...args) {
150
+ if (this.infoEnabled) {
151
+ this._info(...args)
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Logs a trace message with timestamp and logger name if trace logging is enabled.
157
+ * @param {...any} args - Data to be logged as trace message.
158
+ */
159
+ trace (...args) {
160
+ if (this.traceEnabled) {
161
+ this._trace(...args)
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Checks if FATAL level logging is enabled for this logger.
167
+ * @returns {boolean} True if FATAL level logging is enabled, false otherwise.
168
+ */
169
+ isFatalEnabled () {
170
+ return this.fatalEnabled
171
+ }
172
+
173
+ /**
174
+ * Checks if ERROR level logging is enabled for this logger.
175
+ * @returns {boolean} True if ERROR level logging is enabled, false otherwise.
176
+ */
177
+ isErrorEnabled () {
178
+ return this.errorEnabled
179
+ }
180
+
181
+ /**
182
+ * Checks if WARN level logging is enabled for this logger.
183
+ * @returns {boolean} True if WARN level logging is enabled, false otherwise.
184
+ */
185
+ isWarnEnabled () {
186
+ return this.warnEnabled
187
+ }
188
+
189
+ /**
190
+ * Checks if DEBUG level logging is enabled for this logger.
191
+ * @returns {boolean} True if DEBUG level logging is enabled, false otherwise.
192
+ */
193
+ isDebugEnabled () {
194
+ return this.debugEnabled
195
+ }
196
+
197
+ /**
198
+ * Checks if INFO level logging is enabled for this logger.
199
+ * @returns {boolean} True if INFO level logging is enabled, false otherwise.
200
+ */
201
+ isInfoEnabled () {
202
+ return this.infoEnabled
203
+ }
204
+
205
+ /**
206
+ * Checks if TRACE level logging is enabled for this logger.
207
+ * @returns {boolean} True if TRACE level logging is enabled, false otherwise.
208
+ */
209
+ isTraceEnabled () {
210
+ return this.traceEnabled
211
+ }
212
+
213
+ /**
214
+ * Sets the logging level
215
+ * @param {number} level
216
+ * @throws {Error} Always throws "Not Impled Yet Yet" error.
217
+ * @protected
218
+ * @abstract
219
+ */
220
+ _setLevel (level) {
221
+ throw new Error('Not Impled Yet')
222
+ }
223
+
224
+ /**
225
+ * Override this method to implement fatal logging.
226
+ * @protected
227
+ * @param {...*} args - Variable arguments
228
+ * @throws {Error} Always throws "Not Impled Yet Yet" error
229
+ */
230
+ _fatal (...args) {
231
+ throw new Error('Not Impled Yet')
232
+ }
233
+
234
+ /**
235
+ * Override this method to implement error logging.
236
+ * @protected
237
+ * @param {...*} args - Variable arguments
238
+ * @throws {Error} Always throws "Not Impled Yet Yet" error
239
+ */
240
+ _error (...args) {
241
+ throw new Error('Not Impled Yet')
242
+ }
243
+
244
+ /**
245
+ * Override this method to implement warn logging.
246
+ * @protected
247
+ * @param {...*} args - Variable arguments
248
+ * @throws {Error} Always throws "Not Impled Yet Yet" error
249
+ */
250
+ _warn (...args) {
251
+ throw new Error('Not Impled Yet')
252
+ }
253
+
254
+ /**
255
+ * Override this method to implement debug logging.
256
+ * @protected
257
+ * @param {...*} args - Variable arguments
258
+ * @throws {Error} Always throws "Not Impled Yet Yet" error
259
+ */
260
+ _debug (...args) {
261
+ throw new Error('Not Impled Yet')
262
+ }
263
+
264
+ /**
265
+ * Override this method to implement info logging.
266
+ * @protected
267
+ * @param {...*} args - Variable arguments
268
+ * @throws {Error} Always throws "Not Impled Yet Yet" error
269
+ */
270
+ _info (...args) {
271
+ throw new Error('Not Impled Yet')
272
+ }
273
+
274
+ /**
275
+ * Override this method to implement trace logging.
276
+ * @protected
277
+ * @param {...*} args - Variable arguments
278
+ * @throws {Error} Always throws "Not Impled Yet Yet" error
279
+ */
280
+ _trace (...args) {
281
+ throw new Error('Not Impled Yet')
282
+ }
283
+ }
284
+
285
+ module.exports = Logger
@@ -0,0 +1,13 @@
1
+ 'use strict'
2
+ const Log4js = 'LOG4JS'
3
+ const Console = 'CONSOLE'
4
+ /**
5
+ * @namespace ProviderType
6
+ * @description Define the static types
7
+ */
8
+ const ProviderType = {
9
+ Log4js,
10
+ Console
11
+ }
12
+
13
+ module.exports = ProviderType
@@ -0,0 +1,53 @@
1
+ 'use strict'
2
+
3
+ // owned
4
+ // eslint-disable-next-line no-unused-vars
5
+ const LogFactory = require('./log-factory')
6
+
7
+ /**
8
+ * A interface that All Provider module must export
9
+ * @interface
10
+ */
11
+ class Provider {
12
+ /**
13
+ * Checks if a value resembles a logging provider by verifying it has required methods.
14
+ * @param {*} value - The value to check
15
+ * @returns {boolean}
16
+ */
17
+ static isProviderLike (value) {
18
+ if (value == null) {
19
+ return false
20
+ }
21
+ return typeof value === 'object' && typeof value.createLogFactory === 'function' && typeof value.getType === 'function'
22
+ }
23
+
24
+ /**
25
+ * Asserts that the given value is a valid provider-like object.
26
+ * @throws {Error} Throws an error if the value is not provider-like.
27
+ * @param {*} value - The value to check.
28
+ */
29
+ static assertProviderLike (value) {
30
+ if (!this.isProviderLike(value)) {
31
+ throw new Error('Not LogProvider')
32
+ }
33
+ }
34
+
35
+ /**
36
+ * The Type Name of current Provider
37
+ * @return {String} The type of the provider.
38
+ */
39
+ getType () {
40
+ throw new Error('Not Impled Yet')
41
+ }
42
+
43
+ /**
44
+ * Create a new LogFactory instance
45
+ * @param {*} [nativeLib] - The native library to use for logging.
46
+ * @param {*} [setting] - Configuration settings for the logging provider.
47
+ * @returns {LogFactory} A new instance of LogFactory.
48
+ */
49
+ createLogFactory (nativeLib, setting) {
50
+ throw new Error('Not Impled Yet')
51
+ }
52
+ }
53
+ module.exports = Provider
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@creejs/commons-logging",
3
+ "version": "1.0.0",
4
+ "description": "Common Utils About Logging",
5
+ "main": "index.js",
6
+ "private": false,
7
+ "files": [
8
+ "index.js",
9
+ "lib/",
10
+ "types/",
11
+ "README.md"
12
+ ],
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/frameworkee/commons.git"
19
+ },
20
+ "scripts": {
21
+ "dts": "tsc",
22
+ "generate-docs": "node_modules/.bin/jsdoc -c ./jsdoc.json"
23
+ },
24
+ "author": "rodney.vin@gmail.com",
25
+ "license": "Apache-2.0",
26
+ "dependencies": {
27
+ "@creejs/commons-lang": "1.x"
28
+ },
29
+ "devDependencies": {
30
+ "better-docs": "^2.7.3",
31
+ "jsdoc": "^4.0.4",
32
+ "log4js": "6.x"
33
+ }
34
+ }
@@ -0,0 +1,7 @@
1
+ export = ConsoleLogger;
2
+ /**
3
+ * A Simple Implementation of the Logger interface that logs to the console.
4
+ */
5
+ declare class ConsoleLogger extends Logger {
6
+ }
7
+ import Logger = require("./logger");
@@ -0,0 +1,22 @@
1
+ import Logger = require("./logger");
2
+ import LoggingLevel = require("./logging-level");
3
+ /**
4
+ * Adds a logging provider with the specified library and settings.
5
+ * @param {module} providerLib - The logging provider library to add.
6
+ * @param {{}} setting - Configuration settings for the provider.
7
+ */
8
+ export function addProvider(providerLib: any, setting: {}): void;
9
+ /**
10
+ * Gets or creates a named logger instance with the specified log level.
11
+ * If a logger with the given name already exists, updates its log level if different.
12
+ * @param {string} name - The name identifier for the logger
13
+ * @param {'TRACE'|'DEBUG'|'INFO'|'WARN'|'ERROR'|'FATAL'|'OFF'|'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'off'} [level='ERROR'] - The log level
14
+ * @returns {Logger} The logger instance for the given name
15
+ */
16
+ export function getLogger(name: string, level?: "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" | "FATAL" | "OFF" | "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "off"): Logger;
17
+ /**
18
+ * set Global logging level
19
+ * @param {'TRACE'|'DEBUG'|'INFO'|'WARN'|'ERROR'|'FATAL'|'OFF'|'trace'|'debug'|'info'|'warn'|'error'|'fatal'|'off'} level - The log level
20
+ */
21
+ export function setLevel(level: "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" | "FATAL" | "OFF" | "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "off"): void;
22
+ export { Logger, LoggingLevel };
@@ -0,0 +1,13 @@
1
+ export = Log4jsLogger;
2
+ declare class Log4jsLogger extends Logger {
3
+ /**
4
+ * Creates a new ConsoleLogger instance.
5
+ * @constructor
6
+ * @param {string} name - The name of the logger.
7
+ * @param {number} level - The logging level.
8
+ * @param {module} log4jsModule - Log4js library
9
+ */
10
+ constructor(name: string, level: number, log4jsModule: any);
11
+ _logger: any;
12
+ }
13
+ import Logger = require("./logger");