@creejs/commons-logging 1.0.8 → 2.0.2

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/lib/log-level.js DELETED
@@ -1,181 +0,0 @@
1
- import { TypeUtils } from '@creejs/commons-lang'
2
-
3
- const { isNumber, isString } = TypeUtils
4
- /**
5
- * @namespace LogLevel
6
- * @description Defines the logging levels and provides utilities for working with them.
7
- */
8
- // module vars
9
- const TRACE = 'TRACE'
10
- const DEBUG = 'DEBUG'
11
- const INFO = 'INFO'
12
- const WARN = 'WARN'
13
- const ERROR = 'ERROR'
14
- const FATAL = 'FATAL'
15
- const OFF = 'OFF'
16
- const ALL = 'ALL'
17
-
18
- const Names = new Set([TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF, ALL])
19
- /**
20
- * The logging level numbers, where OFF=-1, FATAL=0, ERROR=1, WARN=2, INFO=3, DEBUG=4, TRACE=5, ALL=6
21
- * @enum {number}
22
- * @memberof LogLevel
23
- * @static
24
- */
25
- const Level = {
26
- OFF: -1,
27
- FATAL: 0,
28
- ERROR: 1,
29
- WARN: 2,
30
- INFO: 3,
31
- DEBUG: 4,
32
- TRACE: 5,
33
- ALL: 6
34
- }
35
-
36
- /**
37
- * Standard logging level names
38
- * @enum {string}
39
- * @memberof LogLevel
40
- * @static
41
- * @constant
42
- */
43
- const Name = {
44
- TRACE,
45
- DEBUG,
46
- INFO,
47
- WARN,
48
- ERROR,
49
- FATAL,
50
- OFF
51
- }
52
-
53
- /**
54
- * Internal map for quick lookup of level names by their numeric values
55
- * @type {Map<number, string>}
56
- * @private
57
- */
58
- const Value2Name = new Map()
59
- for (const [name, value] of Object.entries(Level)) {
60
- Value2Name.set(value, name)
61
- }
62
-
63
- /**
64
- * The default logging level (ERROR)
65
- * @type {number}
66
- * @memberof LogLevel
67
- * @static
68
- */
69
- const DefaultLevel = Level.ERROR
70
-
71
- /**
72
- * Converts a numeric logging level value to its corresponding name.
73
- * @param {number} value - The numeric logging level value to convert.
74
- * @returns {string|undefined} The name associated with the given logging level value, from {@link LogLevel.Name}.
75
- * @memberof LogLevel
76
- * @static
77
- */
78
- function value2Name (value) {
79
- if (!isNumber(value)) {
80
- return undefined
81
- }
82
- return Value2Name.get(value)
83
- }
84
-
85
- /**
86
- * Converts a logging level name to its corresponding numeric value.
87
- * @param {string} name - The name of the logging level.
88
- * @returns {number|undefined} The numeric value of the logging level from {@link LogLevel.Level}, or undefined if name is not supported.
89
- * @memberof LogLevel
90
- * @static
91
- */
92
- function name2Value (name) {
93
- if (!isString(name)) {
94
- return undefined
95
- }
96
- // @ts-ignore
97
- return Level[name.toUpperCase()]
98
- }
99
-
100
- /**
101
- * Checks if the given level is a valid log level.
102
- * @param {number} level - The log level to check.
103
- * @returns {boolean} True if the level is valid, false otherwise.
104
- * @memberof LogLevel
105
- * @static
106
- */
107
- function hasLevel (level) {
108
- return Value2Name.has(level)
109
- }
110
-
111
- /**
112
- * Checks if the given level name exists in the Name enum/object.
113
- * @param {string} levelName - The name of the log level to check.
114
- * @returns {boolean} True if the level name exists, false otherwise.
115
- * @memberof LogLevel
116
- * @static
117
- */
118
- function hasName (levelName) {
119
- if (!isString(levelName)) {
120
- return false
121
- }
122
- return Names.has(levelName.toUpperCase())
123
- }
124
-
125
- /**
126
- * Validates that a given level is a number and falls within the valid range.
127
- * @param {*} level - The log level to validate
128
- * @throws {Error} If level is not a number or outside valid range (Level.OFF to Level.ALL)
129
- * @memberof LogLevel
130
- * @static
131
- */
132
- function assertLevel (level) {
133
- if (!isNumber(level)) {
134
- throw new Error(`Level Not Number: type=${typeof level} value=${level}`)
135
- }
136
- if (level < Level.OFF || level > Level.ALL) {
137
- throw new Error(`Not Valid Level: ${level}, Expect between ${Level.OFF} and ${Level.ALL}`)
138
- }
139
- }
140
-
141
- const LogLevel = {
142
- Name,
143
- Level,
144
- DefaultLevel,
145
- hasLevel,
146
- hasName,
147
- assertLevel,
148
- /**
149
- * Same with [Name]{@link LogLevel.Name}
150
- * @type {Object}
151
- * @memberof LogLevel
152
- * @static
153
- */
154
- LevelName: Name,
155
- /**
156
- * Same with [Level]{@link LogLevel.Level}
157
- * @type {Object}
158
- * @memberof LogLevel
159
- * @static
160
- */
161
- LevelValue: Level,
162
-
163
- value2Name,
164
-
165
- name2Value
166
- }
167
-
168
- export default LogLevel
169
-
170
- export {
171
- Name,
172
- Level,
173
- DefaultLevel,
174
- hasLevel,
175
- hasName,
176
- assertLevel,
177
- Name as LevelName,
178
- Level as LevelValue,
179
- value2Name,
180
- name2Value
181
- }
@@ -1,8 +0,0 @@
1
- // internal
2
-
3
- // owned
4
- import Log4jsProvider from './log4js-provider.js'
5
-
6
- // module vars
7
- const log4jsProvider = new Log4jsProvider()
8
- export default log4jsProvider
@@ -1,12 +0,0 @@
1
- const DefaultConfig = { // 6x fromat
2
- appenders: {
3
- out: { type: 'console' }
4
- },
5
- categories: {
6
- default: { appenders: ['out'], level: 'error' }
7
- }
8
- }
9
-
10
- export default DefaultConfig
11
-
12
- export { DefaultConfig }
@@ -1,89 +0,0 @@
1
- // internal
2
- import { TypeAssert } from '@creejs/commons-lang'
3
- // owned
4
- // eslint-disable-next-line no-unused-vars
5
- import Logger from '../logger.js'
6
- // eslint-disable-next-line no-unused-vars
7
- import { Level, value2Name } from '../log-level.js'
8
- import LogFactory from '../log-factory.js'
9
-
10
- import Log4jsLogger from './log4js-logger.js'
11
- import DefaultConfig from './log4js-6x-config.js'
12
-
13
- const { assertString } = TypeAssert
14
-
15
- /**
16
- * Use log4js as the logging provider.
17
- */
18
- export default class Log4jsFactory extends LogFactory {
19
- /**
20
- * the log4js module instance.
21
- * @returns {{}} The log4js module instance.
22
- */
23
- get log4js () {
24
- return this._libraryModule
25
- }
26
-
27
- get setting () {
28
- if (this._setting == null) {
29
- this._setting = cloneDefaultConfig()
30
- }
31
- return this._setting
32
- }
33
-
34
- /**
35
- * Initializes the log4js provider by configuring it with the provided settings.
36
- * @override
37
- */
38
- init () {
39
- // @ts-ignore
40
- this.log4js.configure(this.setting)
41
- }
42
-
43
- /**
44
- * Update factory's Log Level
45
- * 1. Only Provider knows how to update
46
- * @param {number} level - The log level to set, see {@link LogLevel.Level}
47
- * @returns {void}
48
- * @throws {Error} Throws an error as this method is Not Impled Yet.
49
- * @abstract
50
- */
51
- setLevel (level) {
52
- let levelName = value2Name(level)
53
- if (levelName == null) {
54
- throw new Error(`Invalid log level: ${level}`)
55
- }
56
- levelName = levelName.toLowerCase()
57
- let updated = false
58
- // @ts-ignore
59
- const { categories } = this.setting
60
- for (const categoryKey in categories) {
61
- const category = categories[categoryKey]
62
- const { level: categoryLevel } = category
63
- if (categoryLevel !== levelName) {
64
- category.level = levelName.toLowerCase()
65
- updated = true
66
- }
67
- }
68
- // init log4js again
69
- updated && this.init()
70
- }
71
-
72
- /**
73
- * Creates a new logger instance with the specified name and log level.
74
- * @param {string} loggerName - The name of the logger to create.
75
- * @returns {Logger} A new logger instance configured with the given name and level.
76
- * @override
77
- */
78
- createLogger (loggerName) {
79
- assertString(loggerName)
80
- // @ts-ignore
81
- const nativeLogger = this.log4js.getLogger(loggerName)
82
- const logger = new Log4jsLogger(loggerName, nativeLogger)
83
- return logger
84
- }
85
- }
86
-
87
- function cloneDefaultConfig () {
88
- return JSON.parse(JSON.stringify(DefaultConfig))
89
- }
@@ -1,107 +0,0 @@
1
- // 3rd
2
- // internal
3
- // owned
4
- import { assertLevel, value2Name, name2Value, Level } from '../log-level.js'
5
- import Logger from '../logger.js'
6
- // module vars
7
-
8
- export default class Log4jsLogger extends Logger {
9
- /**
10
- * Creates a new ConsoleLogger instance.
11
- * @constructor
12
- * @param {string} name - The name of the logger.
13
- * @param {*} nativeLogger - Log4js library
14
- */
15
- constructor (name, nativeLogger) {
16
- super(name, nativeLogger)
17
- // @ts-ignore
18
- this._level = undefined // MUST NOT accept level from outside
19
- this._logger = nativeLogger
20
- }
21
-
22
- /**
23
- * 1. log4js may have many categories(loggerName) with diffirent level
24
- * 2. return the underlying log4js native logger's level
25
- * @returns {number} The current log level.
26
- */
27
- get level () {
28
- if (this._level == null) {
29
- const levelName = this._logger.level.levelStr
30
- if (levelName == null) {
31
- return Level.OFF
32
- }
33
- const level = name2Value(levelName)
34
- if (level == null) {
35
- throw new Error(`Unknown level name: ${levelName}`)
36
- }
37
- return level
38
- }
39
- return this._level
40
- }
41
-
42
- /**
43
- * set log4js native logger's level
44
- * @param {number} level
45
- * @override
46
- * @protectd
47
- */
48
- _setLevel (level) {
49
- assertLevel(level)
50
- const levelName = value2Name(level)
51
- if (levelName == null) {
52
- throw new Error(`Invalid log level: ${level}`)
53
- }
54
- // log4js use string level, eg. 'error, trace'
55
- this._logger.level = levelName.toLowerCase()
56
- }
57
-
58
- /**
59
- * Logs a fatal error message with timestamp and logger name.
60
- * Only outputs if fatal logging is enabled for this logger instance.
61
- * @param {...any} args - Arguments to log (will be space-separated)
62
- */
63
- _fatal (...args) {
64
- this._logger.fatal(...args)
65
- }
66
-
67
- /**
68
- * Logs an error message to the console with timestamp and logger name.
69
- * Only logs if error logging is enabled for this logger instance.
70
- * @param {...any} args - Arguments to be logged as error message
71
- */
72
- _error (...args) {
73
- this._logger.error(...args)
74
- }
75
-
76
- /**
77
- * Logs a warning message to the console if warn logging is enabled.
78
- * @param {...any} args - The arguments to log as a warning message.
79
- */
80
- _warn (...args) {
81
- this._logger.warn(...args)
82
- }
83
-
84
- /**
85
- * Logs debug messages to console if debug mode is enabled.
86
- * @param {...any} args - The data to be logged
87
- */
88
- _debug (...args) {
89
- this._logger.debug(...args)
90
- }
91
-
92
- /**
93
- * Logs an info message to the console with timestamp and logger name.
94
- * @param {...any} args - The data to be logged. Accepts multiple arguments.
95
- */
96
- _info (...args) {
97
- this._logger.info(...args)
98
- }
99
-
100
- /**
101
- * Logs a trace message with timestamp and logger name if trace logging is enabled.
102
- * @param {...any} args - Data to be logged as trace message.
103
- */
104
- _trace (...args) {
105
- this._logger.trace(...args)
106
- }
107
- }
@@ -1,34 +0,0 @@
1
- // internal
2
- import { TypeAssert } from '@creejs/commons-lang'
3
-
4
- // owned
5
- import ProviderType from '../provider-type.js'
6
- import Provider from '../provider.js'
7
- import Log4jsFactory from './log4js-factory.js'
8
-
9
- const { assertNotNil } = TypeAssert
10
-
11
- export default class Log4jsProvider extends Provider {
12
- /**
13
- * Gets the provider type (Log4js).
14
- * @returns {string} "LOG4JS"
15
- * @override
16
- */
17
- getType () {
18
- return ProviderType.Log4js
19
- }
20
-
21
- /**
22
- * Creates a new Provider instance.
23
- * @param {*} libraryModule - The library module to be used.
24
- * @param {*} setting - Configuration settings for the provider.
25
- * @returns {Log4jsFactory} A new instance of Log4jsFactory.
26
- * @override
27
- */
28
- createLogFactory (libraryModule, setting) {
29
- assertNotNil(libraryModule)
30
- const factory = new Log4jsFactory(libraryModule, setting)
31
- factory.init()
32
- return factory
33
- }
34
- }
package/lib/logger.js DELETED
@@ -1,284 +0,0 @@
1
- // internal
2
- import { TypeAssert } from '@creejs/commons-lang'
3
- // owned
4
- import LogLevel from './log-level.js'
5
-
6
- const { Level, DefaultLevel, assertLevel } = LogLevel
7
- const { assertString } = TypeAssert
8
- /**
9
- * Abstract Logger Class of All Logger
10
- * @abstract
11
- */
12
- export default class Logger {
13
- /**
14
- * Creates a new Logger instance.
15
- * @constructor
16
- * @param {string} name - The name identifier for the logger
17
- * @param {*} [nativeLogger]
18
- * @param {number} [level] - The logging level, default is Level.ERROR=1
19
- */
20
- constructor (name, nativeLogger, level = DefaultLevel) {
21
- assertString(name)
22
- assertLevel(level)
23
- this._name = name
24
- this._nativeLogger = nativeLogger
25
- this._level = level
26
- }
27
-
28
- get nativeLogger () {
29
- return this._nativeLogger
30
- }
31
-
32
- get level () {
33
- return this._level
34
- }
35
-
36
- /**
37
- * Gets the name of the logger instance.
38
- * @returns {string} The name of the logger.
39
- */
40
- get name () {
41
- return this._name
42
- }
43
-
44
- /**
45
- * Checks if FATAL level logging is enabled for this logger.
46
- * @returns {boolean} True if FATAL level logging is enabled, false otherwise.
47
- */
48
- get fatalEnabled () {
49
- return this.level >= Level.FATAL
50
- }
51
-
52
- /**
53
- * Checks if ERROR level logging is enabled for this logger.
54
- * @returns {boolean} True if ERROR level logging is enabled, false otherwise.
55
- */
56
- get errorEnabled () {
57
- return this.level >= Level.ERROR
58
- }
59
-
60
- /**
61
- * Checks if WARN level logging is enabled for this logger.
62
- * @returns {boolean} True if WARN level logging is enabled, false otherwise.
63
- */
64
- get warnEnabled () {
65
- return this.level >= Level.WARN
66
- }
67
-
68
- /**
69
- * Checks if DEBUG level logging is enabled for this logger.
70
- * @returns {boolean} True if DEBUG level logging is enabled, false otherwise.
71
- */
72
- get debugEnabled () {
73
- return this.level >= Level.DEBUG
74
- }
75
-
76
- /**
77
- * Checks if INFO level logging is enabled for this logger.
78
- * @returns {boolean} True if INFO level logging is enabled, false otherwise.
79
- */
80
- get infoEnabled () {
81
- return this.level >= Level.INFO
82
- }
83
-
84
- /**
85
- * Checks if TRACE level logging is enabled for this logger.
86
- * @returns {boolean} True if TRACE level logging is enabled, false otherwise.
87
- */
88
- get traceEnabled () {
89
- return this.level >= Level.TRACE
90
- }
91
-
92
- /**
93
- * change log level for current logger instance.
94
- * @param {number} level - new log level to set
95
- * @throws {Error} Currently throws an error as this method is Not Impled Yet.
96
- * @abstract
97
- */
98
- setLevel (level) {
99
- LogLevel.assertLevel(level)
100
- this._setLevel(level)
101
- this._level = level
102
- }
103
-
104
- /**
105
- * Logs a fatal error message with timestamp and logger name.
106
- * Only outputs if fatal logging is enabled for this logger instance.
107
- * @param {...any} args - Arguments to log (will be space-separated)
108
- */
109
- fatal (...args) {
110
- if (this.fatalEnabled) {
111
- this._fatal(...args)
112
- }
113
- }
114
-
115
- /**
116
- * Logs an error message to the console with timestamp and logger name.
117
- * Only logs if error logging is enabled for this logger instance.
118
- * @param {...any} args - Arguments to be logged as error message
119
- */
120
- error (...args) {
121
- if (this.errorEnabled) {
122
- this._error(...args)
123
- }
124
- }
125
-
126
- /**
127
- * Logs a warning message to the console if warn logging is enabled.
128
- * @param {...any} args - The arguments to log as a warning message.
129
- */
130
- warn (...args) {
131
- if (this.warnEnabled) {
132
- this._warn(...args)
133
- }
134
- }
135
-
136
- /**
137
- * Logs debug messages to console if debug mode is enabled.
138
- * @param {...any} args - The data to be logged
139
- */
140
- debug (...args) {
141
- if (this.debugEnabled) {
142
- this._debug(...args)
143
- }
144
- }
145
-
146
- /**
147
- * Logs an info message to the console with timestamp and logger name.
148
- * @param {...any} args - The data to be logged. Accepts multiple arguments.
149
- */
150
- info (...args) {
151
- if (this.infoEnabled) {
152
- this._info(...args)
153
- }
154
- }
155
-
156
- /**
157
- * Logs a trace message with timestamp and logger name if trace logging is enabled.
158
- * @param {...any} args - Data to be logged as trace message.
159
- */
160
- trace (...args) {
161
- if (this.traceEnabled) {
162
- this._trace(...args)
163
- }
164
- }
165
-
166
- /**
167
- * Checks if FATAL level logging is enabled for this logger.
168
- * @returns {boolean} True if FATAL level logging is enabled, false otherwise.
169
- */
170
- isFatalEnabled () {
171
- return this.fatalEnabled
172
- }
173
-
174
- /**
175
- * Checks if ERROR level logging is enabled for this logger.
176
- * @returns {boolean} True if ERROR level logging is enabled, false otherwise.
177
- */
178
- isErrorEnabled () {
179
- return this.errorEnabled
180
- }
181
-
182
- /**
183
- * Checks if WARN level logging is enabled for this logger.
184
- * @returns {boolean} True if WARN level logging is enabled, false otherwise.
185
- */
186
- isWarnEnabled () {
187
- return this.warnEnabled
188
- }
189
-
190
- /**
191
- * Checks if DEBUG level logging is enabled for this logger.
192
- * @returns {boolean} True if DEBUG level logging is enabled, false otherwise.
193
- */
194
- isDebugEnabled () {
195
- return this.debugEnabled
196
- }
197
-
198
- /**
199
- * Checks if INFO level logging is enabled for this logger.
200
- * @returns {boolean} True if INFO level logging is enabled, false otherwise.
201
- */
202
- isInfoEnabled () {
203
- return this.infoEnabled
204
- }
205
-
206
- /**
207
- * Checks if TRACE level logging is enabled for this logger.
208
- * @returns {boolean} True if TRACE level logging is enabled, false otherwise.
209
- */
210
- isTraceEnabled () {
211
- return this.traceEnabled
212
- }
213
-
214
- /**
215
- * Sets the logging level
216
- * @param {number} level
217
- * @throws {Error} Always throws "Not Impled Yet Yet" error.
218
- * @protected
219
- * @abstract
220
- */
221
- _setLevel (level) {
222
- throw new Error('Not Impled Yet')
223
- }
224
-
225
- /**
226
- * Override this method to implement fatal logging.
227
- * @protected
228
- * @param {...*} args - Variable arguments
229
- * @throws {Error} Always throws "Not Impled Yet Yet" error
230
- */
231
- _fatal (...args) {
232
- throw new Error('Not Impled Yet')
233
- }
234
-
235
- /**
236
- * Override this method to implement error logging.
237
- * @protected
238
- * @param {...*} args - Variable arguments
239
- * @throws {Error} Always throws "Not Impled Yet Yet" error
240
- */
241
- _error (...args) {
242
- throw new Error('Not Impled Yet')
243
- }
244
-
245
- /**
246
- * Override this method to implement warn logging.
247
- * @protected
248
- * @param {...*} args - Variable arguments
249
- * @throws {Error} Always throws "Not Impled Yet Yet" error
250
- */
251
- _warn (...args) {
252
- throw new Error('Not Impled Yet')
253
- }
254
-
255
- /**
256
- * Override this method to implement debug logging.
257
- * @protected
258
- * @param {...*} args - Variable arguments
259
- * @throws {Error} Always throws "Not Impled Yet Yet" error
260
- */
261
- _debug (...args) {
262
- throw new Error('Not Impled Yet')
263
- }
264
-
265
- /**
266
- * Override this method to implement info logging.
267
- * @protected
268
- * @param {...*} args - Variable arguments
269
- * @throws {Error} Always throws "Not Impled Yet Yet" error
270
- */
271
- _info (...args) {
272
- throw new Error('Not Impled Yet')
273
- }
274
-
275
- /**
276
- * Override this method to implement trace logging.
277
- * @protected
278
- * @param {...*} args - Variable arguments
279
- * @throws {Error} Always throws "Not Impled Yet Yet" error
280
- */
281
- _trace (...args) {
282
- throw new Error('Not Impled Yet')
283
- }
284
- }