@villedemontreal/logger 6.5.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.
- package/LICENSE +22 -0
- package/README.md +164 -0
- package/dist/src/config/configs.d.ts +118 -0
- package/dist/src/config/configs.js +167 -0
- package/dist/src/config/configs.js.map +1 -0
- package/dist/src/config/constants.d.ts +60 -0
- package/dist/src/config/constants.js +60 -0
- package/dist/src/config/constants.js.map +1 -0
- package/dist/src/consoleStream.d.ts +8 -0
- package/dist/src/consoleStream.js +29 -0
- package/dist/src/consoleStream.js.map +1 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lazyLogger.d.ts +21 -0
- package/dist/src/lazyLogger.js +46 -0
- package/dist/src/lazyLogger.js.map +1 -0
- package/dist/src/logger.d.ts +169 -0
- package/dist/src/logger.js +501 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/logger.test.d.ts +8 -0
- package/dist/src/logger.test.js +564 -0
- package/dist/src/logger.test.js.map +1 -0
- package/package.json +67 -0
- package/src/config/configs.ts +195 -0
- package/src/config/constants.ts +78 -0
- package/src/consoleStream.ts +29 -0
- package/src/index.ts +3 -0
- package/src/lazyLogger.ts +50 -0
- package/src/logger.test.ts +673 -0
- package/src/logger.ts +551 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ILogger, LogLevel } from './logger';
|
|
2
|
+
/**
|
|
3
|
+
* A Logger wrapper that allows to lazyly create a
|
|
4
|
+
* real Logger.
|
|
5
|
+
*
|
|
6
|
+
* The first time a log is perform, the
|
|
7
|
+
* "loggerCreator" will be called to create
|
|
8
|
+
* the actual Logger.
|
|
9
|
+
*/
|
|
10
|
+
export declare class LazyLogger implements ILogger {
|
|
11
|
+
private realLogger;
|
|
12
|
+
private name;
|
|
13
|
+
private loggerCreator;
|
|
14
|
+
constructor(name: string, loggerCreator: (name: string) => ILogger);
|
|
15
|
+
debug(messageObj: any, txtMsg?: string): void;
|
|
16
|
+
info(messageObj: any, txtMsg?: string): void;
|
|
17
|
+
warning(messageObj: any, txtMsg?: string): void;
|
|
18
|
+
error(messageObj: any, txtMsg?: string): void;
|
|
19
|
+
log(level: LogLevel, messageObj: any, txtMsg?: string): void;
|
|
20
|
+
protected getRealLogger(): ILogger;
|
|
21
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LazyLogger = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A Logger wrapper that allows to lazyly create a
|
|
6
|
+
* real Logger.
|
|
7
|
+
*
|
|
8
|
+
* The first time a log is perform, the
|
|
9
|
+
* "loggerCreator" will be called to create
|
|
10
|
+
* the actual Logger.
|
|
11
|
+
*/
|
|
12
|
+
class LazyLogger {
|
|
13
|
+
constructor(name, loggerCreator) {
|
|
14
|
+
this.name = name;
|
|
15
|
+
if (!loggerCreator) {
|
|
16
|
+
throw new Error(`The Logger Creator is required!`);
|
|
17
|
+
}
|
|
18
|
+
this.loggerCreator = loggerCreator;
|
|
19
|
+
}
|
|
20
|
+
debug(messageObj, txtMsg) {
|
|
21
|
+
return this.getRealLogger().debug(messageObj, txtMsg);
|
|
22
|
+
}
|
|
23
|
+
info(messageObj, txtMsg) {
|
|
24
|
+
return this.getRealLogger().info(messageObj, txtMsg);
|
|
25
|
+
}
|
|
26
|
+
warning(messageObj, txtMsg) {
|
|
27
|
+
return this.getRealLogger().warning(messageObj, txtMsg);
|
|
28
|
+
}
|
|
29
|
+
error(messageObj, txtMsg) {
|
|
30
|
+
return this.getRealLogger().error(messageObj, txtMsg);
|
|
31
|
+
}
|
|
32
|
+
log(level, messageObj, txtMsg) {
|
|
33
|
+
return this.getRealLogger().log(level, messageObj, txtMsg);
|
|
34
|
+
}
|
|
35
|
+
getRealLogger() {
|
|
36
|
+
if (!this.realLogger) {
|
|
37
|
+
this.realLogger = this.loggerCreator(this.name);
|
|
38
|
+
if (!this.realLogger) {
|
|
39
|
+
throw new Error(`The Logger Creator must create a valid Logger!`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return this.realLogger;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.LazyLogger = LazyLogger;
|
|
46
|
+
//# sourceMappingURL=lazyLogger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazyLogger.js","sourceRoot":"","sources":["../../src/lazyLogger.ts"],"names":[],"mappings":";;;AAEA;;;;;;;GAOG;AACH,MAAa,UAAU;IAKrB,YAAY,IAAY,EAAE,aAAwC;QAChE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,CAAC,aAAa,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;SACpD;QACD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAEM,KAAK,CAAC,UAAe,EAAE,MAAe;QAC3C,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACxD,CAAC;IACM,IAAI,CAAC,UAAe,EAAE,MAAe;QAC1C,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACvD,CAAC;IACM,OAAO,CAAC,UAAe,EAAE,MAAe;QAC7C,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IACM,KAAK,CAAC,UAAe,EAAE,MAAe;QAC3C,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACxD,CAAC;IACM,GAAG,CAAC,KAAe,EAAE,UAAe,EAAE,MAAe;QAC1D,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;IAC7D,CAAC;IAES,aAAa;QACrB,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;YACpB,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAChD,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;gBACpB,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;aACnE;SACF;QACD,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;CACF;AAvCD,gCAuCC"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { LogLevel } from '@villedemontreal/general-utils';
|
|
2
|
+
import * as pino from 'pino';
|
|
3
|
+
import { LoggerConfigs } from './config/configs';
|
|
4
|
+
export { LogLevel } from '@villedemontreal/general-utils';
|
|
5
|
+
/**
|
|
6
|
+
* A Logger.
|
|
7
|
+
*/
|
|
8
|
+
export interface ILogger {
|
|
9
|
+
debug(messageObj: any, txtMsg?: string): void;
|
|
10
|
+
info(messageObj: any, txtMsg?: string): void;
|
|
11
|
+
warning(messageObj: any, txtMsg?: string): void;
|
|
12
|
+
error(messageObj: any, txtMsg?: string): void;
|
|
13
|
+
log(level: LogLevel, messageObj: any, txtMsg?: string): void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts a Pino level to its number value.
|
|
17
|
+
*/
|
|
18
|
+
export declare const convertPinoLevelToNumber: (pinoLogLevel: pino.Level) => number;
|
|
19
|
+
/**
|
|
20
|
+
* Converts a local LogLevel to a Pino label level.
|
|
21
|
+
*/
|
|
22
|
+
export declare const convertLogLevelToPinoLabelLevel: (logLevel: LogLevel) => pino.Level;
|
|
23
|
+
/**
|
|
24
|
+
* Converts a local LogLevel to a Pino number level.
|
|
25
|
+
*/
|
|
26
|
+
export declare const convertLogLevelToPinoNumberLevel: (logLevel: LogLevel) => number;
|
|
27
|
+
/**
|
|
28
|
+
* Initialize the logger with the config given in parameter
|
|
29
|
+
* This function must be used before using createLogger or Logger Class
|
|
30
|
+
* @param {LoggerConfigs} loggerConfig
|
|
31
|
+
* @param {string} [name='default']
|
|
32
|
+
* @param force if `true`, the logger will be initialized
|
|
33
|
+
* again even if it already is.
|
|
34
|
+
*/
|
|
35
|
+
export declare const initLogger: (loggerConfig: LoggerConfigs, name?: string, force?: boolean) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Change the global log level of the application. Useful to change dynamically
|
|
38
|
+
* the log level of something that is already started.
|
|
39
|
+
* @param level The log level to set for the application
|
|
40
|
+
*/
|
|
41
|
+
export declare const setGlobalLogLevel: (level: LogLevel) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Shorthands function that return a new logger instance
|
|
44
|
+
* Internally, we use the same logger instance but with different context like the name given in parameter
|
|
45
|
+
* and this context is kept in this new instance returned.
|
|
46
|
+
* @export
|
|
47
|
+
* @param {string} name
|
|
48
|
+
* @returns {ILogger}
|
|
49
|
+
*/
|
|
50
|
+
export declare function createLogger(name: string): ILogger;
|
|
51
|
+
export declare function isInited(): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Logger implementation.
|
|
54
|
+
*/
|
|
55
|
+
export declare class Logger implements ILogger {
|
|
56
|
+
private readonly pino;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a logger.
|
|
59
|
+
*
|
|
60
|
+
* @param the logger name. This name should be related
|
|
61
|
+
* to the file the logger is created in. On a production
|
|
62
|
+
* environment, it's possible that only this name will
|
|
63
|
+
* be available to locate the source of the log.
|
|
64
|
+
* Streams will be created after the first call to the logger
|
|
65
|
+
*/
|
|
66
|
+
constructor(name: string);
|
|
67
|
+
/**
|
|
68
|
+
* Logs a DEBUG level message object.
|
|
69
|
+
*
|
|
70
|
+
* If the extra "txtMsg" parameter is set, it is
|
|
71
|
+
* going to be added to messageObj as a ".msg"
|
|
72
|
+
* property (if messageObj is an object) or
|
|
73
|
+
* concatenated to messageObj (if it's not an
|
|
74
|
+
* object).
|
|
75
|
+
*
|
|
76
|
+
* Those types of logs are possible :
|
|
77
|
+
*
|
|
78
|
+
* - log.debug("a simple text message");
|
|
79
|
+
* - log.debug({"name": "an object"});
|
|
80
|
+
* - log.debug({"name": "an object..."}, "... and an extra text message");
|
|
81
|
+
* - log.debug(err, "a catched error and an explanation message");
|
|
82
|
+
*/
|
|
83
|
+
debug(messageObj: any, txtMsg?: string): void;
|
|
84
|
+
/**
|
|
85
|
+
* Logs an INFO level message.
|
|
86
|
+
*
|
|
87
|
+
* If the extra "txtMsg" parameter is set, it is
|
|
88
|
+
* going to be added to messageObj as a ".msg"
|
|
89
|
+
* property (if messageObj is an object) or
|
|
90
|
+
* concatenated to messageObj (if it's not an
|
|
91
|
+
* object).
|
|
92
|
+
*
|
|
93
|
+
* Those types of logs are possible :
|
|
94
|
+
*
|
|
95
|
+
* - log.info("a simple text message");
|
|
96
|
+
* - log.info({"name": "an object"});
|
|
97
|
+
* - log.info({"name": "an object..."}, "... and an extra text message");
|
|
98
|
+
* - log.info(err, "a catched error and an explanation message");public
|
|
99
|
+
*/
|
|
100
|
+
info(messageObj: any, txtMsg?: string): void;
|
|
101
|
+
/**
|
|
102
|
+
* Logs a WARNING level message.
|
|
103
|
+
*
|
|
104
|
+
* If the extra "txtMsg" parameter is set, it is
|
|
105
|
+
* going to be added to messageObj as a ".msg"
|
|
106
|
+
* property (if messageObj is an object) or
|
|
107
|
+
* concatenated to messageObj (if it's not an
|
|
108
|
+
* object).
|
|
109
|
+
*
|
|
110
|
+
* Those types of logs are possible :
|
|
111
|
+
*
|
|
112
|
+
* - log.warning("a simple text message");
|
|
113
|
+
* - log.warning({"name": "an object"});
|
|
114
|
+
* - log.warning({"name": "an object..."}, "... and an extra text message");
|
|
115
|
+
* - log.warning(err, "a catched error and an explanation mespublic sage");
|
|
116
|
+
*/
|
|
117
|
+
warning(messageObj: any, txtMsg?: string): void;
|
|
118
|
+
/**
|
|
119
|
+
* Logs an ERROR level message.
|
|
120
|
+
*
|
|
121
|
+
* If the extra "txtMsg" parameter is set, it is
|
|
122
|
+
* going to be added to messageObj as a ".msg"
|
|
123
|
+
* property (if messageObj is an object) or
|
|
124
|
+
* concatenated to messageObj (if it's not an
|
|
125
|
+
* object).
|
|
126
|
+
*
|
|
127
|
+
* Those types of logs are possible :
|
|
128
|
+
*
|
|
129
|
+
* - log.error("a simple text message");
|
|
130
|
+
* - log.error({"name": "an object"});
|
|
131
|
+
* - log.error({"name": "an object..."}, "... and an extra text message");
|
|
132
|
+
* - log.error(err, "a catched error and an explanatpublic ion message");
|
|
133
|
+
*/
|
|
134
|
+
error(messageObj: any, txtMsg?: string): void;
|
|
135
|
+
/**
|
|
136
|
+
* Logs a level specific message.
|
|
137
|
+
*
|
|
138
|
+
* If the extra "txtMsg" parameter is set, it is
|
|
139
|
+
* going to be added to messageObj as a ".msg"
|
|
140
|
+
* property (if messageObj is an object) or
|
|
141
|
+
* concatenated to messageObj (if it's not an
|
|
142
|
+
* object).
|
|
143
|
+
*
|
|
144
|
+
* Those types of logs are possible :
|
|
145
|
+
*
|
|
146
|
+
* - log(LogLevel.XXXXX, "a simple text message");
|
|
147
|
+
* - log({"name": "an object"});
|
|
148
|
+
* - log({"name": "an object..."}, "... and an extra text message");
|
|
149
|
+
* - log(err, "a catched error and an epublic xplanation message");
|
|
150
|
+
*/
|
|
151
|
+
log(level: LogLevel, messageObj: any, txtMsg?: string): void;
|
|
152
|
+
/**
|
|
153
|
+
* Update the logger based on the parent changes.
|
|
154
|
+
* Could use something more precise to handle specific event but
|
|
155
|
+
* people could use it to update the child independently from the parent,
|
|
156
|
+
* which is not what is intended.
|
|
157
|
+
*/
|
|
158
|
+
update(): void;
|
|
159
|
+
/**
|
|
160
|
+
* Adds the file and line number where the log occures.
|
|
161
|
+
* This particular code is required since our custom Logger
|
|
162
|
+
* is a layer over Pino and therefore adds an extra level
|
|
163
|
+
* to the error stack. Without this code, the file and line number
|
|
164
|
+
* are not the right ones.
|
|
165
|
+
*
|
|
166
|
+
* Based by http://stackoverflow.com/a/38197778/843699
|
|
167
|
+
*/
|
|
168
|
+
private enhanceLog;
|
|
169
|
+
}
|