@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
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@villedemontreal/logger",
|
|
3
|
+
"version": "6.5.1",
|
|
4
|
+
"description": "Logger and logging utilities",
|
|
5
|
+
"main": "dist/src/index.js",
|
|
6
|
+
"typings": "dist/src",
|
|
7
|
+
"files": [
|
|
8
|
+
"src",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"start": "node run test",
|
|
13
|
+
"test": "node run test",
|
|
14
|
+
"compile": "node run compile",
|
|
15
|
+
"lint": "node run lint",
|
|
16
|
+
"lint-fix": "node run lint-fix",
|
|
17
|
+
"tslint": "node run tslint",
|
|
18
|
+
"tslint-fix": "node run tslint-fix",
|
|
19
|
+
"prettier": "node run prettier",
|
|
20
|
+
"prettier-fix": "node run prettier-fix",
|
|
21
|
+
"watch": "node run watch"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/villedemontreal/node-logger.git"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/villedemontreal/node-logger",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"montreal",
|
|
30
|
+
"utils",
|
|
31
|
+
"utilities",
|
|
32
|
+
"logging",
|
|
33
|
+
"log",
|
|
34
|
+
"logger"
|
|
35
|
+
],
|
|
36
|
+
"author": "Ville de Montréal",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@types/app-root-path": "1.2.4",
|
|
40
|
+
"@types/lodash": "4.14.178",
|
|
41
|
+
"@types/pino": "6.3.5",
|
|
42
|
+
"@types/pino-multi-stream": "5.1.0",
|
|
43
|
+
"@types/source-map-support": "0.5.4",
|
|
44
|
+
"@villedemontreal/general-utils": "5.16.0",
|
|
45
|
+
"app-root-path": "3.0.0",
|
|
46
|
+
"lodash": "4.17.21",
|
|
47
|
+
"pino": "6.11.0",
|
|
48
|
+
"pino-multi-stream": "5.2.0",
|
|
49
|
+
"pino-pretty": "4.5.0",
|
|
50
|
+
"rotating-file-stream": "3.0.2",
|
|
51
|
+
"source-map-support": "0.5.21",
|
|
52
|
+
"uuid": "8.3.2"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/chai": "4.3.0",
|
|
56
|
+
"@types/fs-extra": "9.0.13",
|
|
57
|
+
"@types/mocha": "9.0.0",
|
|
58
|
+
"@villedemontreal/lint-config": "1.7.6",
|
|
59
|
+
"@villedemontreal/scripting": "1.5.0",
|
|
60
|
+
"chai": "4.3.4",
|
|
61
|
+
"fs-extra": "9.1.0",
|
|
62
|
+
"mocha": "9.1.3",
|
|
63
|
+
"mocha-jenkins-reporter": "0.4.7",
|
|
64
|
+
"tslint": "6.1.2",
|
|
65
|
+
"typescript": "3.9.5"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { globalConstants } from '@villedemontreal/general-utils';
|
|
2
|
+
import { LogLevel } from '../logger';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Logger Config
|
|
6
|
+
*/
|
|
7
|
+
export class LoggerConfigs {
|
|
8
|
+
/**
|
|
9
|
+
* The correlation id provider.
|
|
10
|
+
* To set this is required.
|
|
11
|
+
*/
|
|
12
|
+
private correlationIdProvider: () => string = null;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Enable logging to a file.
|
|
16
|
+
* Default to false.
|
|
17
|
+
*/
|
|
18
|
+
private logToFile: boolean = false;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The directory where the potential
|
|
22
|
+
* log file will be written.
|
|
23
|
+
* Default to "./log".
|
|
24
|
+
*/
|
|
25
|
+
private logDir: string = './log';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Log rotation file nb
|
|
29
|
+
*/
|
|
30
|
+
private logRotateFilesNbr: number = 30;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Log rotation threshhold in MB
|
|
34
|
+
*/
|
|
35
|
+
private logRotateThresholdMB: number = 100;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Log maximum total size in MB
|
|
39
|
+
*/
|
|
40
|
+
private logRotateMaxTotalSizeMB: number = 1000;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Logging level (info, debug, warning, error)
|
|
44
|
+
*
|
|
45
|
+
* Default value : DEBUG in DEV or WARNING otherwise.
|
|
46
|
+
*/
|
|
47
|
+
private logLevel: LogLevel =
|
|
48
|
+
process.env.NODE_ENV === globalConstants.Environments.DEV ? LogLevel.DEBUG : LogLevel.WARNING;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Log in human readable form in the console.
|
|
52
|
+
*/
|
|
53
|
+
private logHumanReadableinConsole: boolean = false;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Add the stack trace to the error log in development
|
|
57
|
+
*/
|
|
58
|
+
private addStackTraceToErrorMessagesInDev: boolean =
|
|
59
|
+
process.env.NODE_ENV === globalConstants.Environments.DEV ? true : false;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Log the source of the error in the log message
|
|
63
|
+
*/
|
|
64
|
+
private logSource: boolean = true;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The Correlation Id provider is required.
|
|
68
|
+
*/
|
|
69
|
+
constructor(correlationIdProvider: () => string) {
|
|
70
|
+
if (!correlationIdProvider) {
|
|
71
|
+
throw new Error(`The Correlation Id provider is required.`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.correlationIdProvider = correlationIdProvider;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The current Correlation Id.
|
|
79
|
+
*/
|
|
80
|
+
get correlationId() {
|
|
81
|
+
return this.correlationIdProvider();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Logging to a file?
|
|
86
|
+
*/
|
|
87
|
+
public isLogToFile() {
|
|
88
|
+
return this.logToFile;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get the directory where the log files should be written
|
|
93
|
+
*/
|
|
94
|
+
public getLogDirectory() {
|
|
95
|
+
return this.logDir;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get the current logging level
|
|
100
|
+
*/
|
|
101
|
+
public getLogLevel() {
|
|
102
|
+
return this.logLevel;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public isLogHumanReadableinConsole() {
|
|
106
|
+
return this.logHumanReadableinConsole;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public isAddStackTraceToErrorMessagesInDev() {
|
|
110
|
+
return this.addStackTraceToErrorMessagesInDev;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public isLogSource() {
|
|
114
|
+
return this.logSource;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public getLogRotateFilesNbr() {
|
|
118
|
+
return this.logRotateFilesNbr;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public getLogRotateThresholdMB() {
|
|
122
|
+
return this.logRotateThresholdMB;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public getLogRotateMaxTotalSizeMB() {
|
|
126
|
+
return this.logRotateMaxTotalSizeMB;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Enable logging to a file in addition to
|
|
131
|
+
* logging to the standard output.
|
|
132
|
+
* This should probably be let to FALSE in our
|
|
133
|
+
* current network where Graylog is used : no
|
|
134
|
+
* log files are indeed required!
|
|
135
|
+
*/
|
|
136
|
+
public setSlowerLogToFileToo(logToFile: boolean) {
|
|
137
|
+
this.logToFile = logToFile;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Set the directory where the log files should be written
|
|
142
|
+
*/
|
|
143
|
+
public setLogDirectory(logDir: string) {
|
|
144
|
+
this.logDir = logDir;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Set the logging level
|
|
149
|
+
*/
|
|
150
|
+
public setLogLevel(loglevel: LogLevel) {
|
|
151
|
+
this.logLevel = loglevel;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Set the logs in the console to be human readable
|
|
156
|
+
*/
|
|
157
|
+
public setLogHumanReadableinConsole(logHumanReadableinConsole: boolean) {
|
|
158
|
+
this.logHumanReadableinConsole = logHumanReadableinConsole;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Set the stack trace to be logged in development environment
|
|
163
|
+
*/
|
|
164
|
+
public setAddStackTraceToErrorMessagesInDev(addStackTraceToErrorMessagesInDev: boolean) {
|
|
165
|
+
this.addStackTraceToErrorMessagesInDev = addStackTraceToErrorMessagesInDev;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Set if the source of the error should be logged
|
|
170
|
+
*/
|
|
171
|
+
public setLogSource(logSource: boolean) {
|
|
172
|
+
this.logSource = logSource;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Set the number of log files to rotate
|
|
177
|
+
*/
|
|
178
|
+
public setLogRotateFilesNbr(logRotateFilesNb: number) {
|
|
179
|
+
this.logRotateFilesNbr = logRotateFilesNb;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Set the log rotation threshhold.
|
|
184
|
+
*/
|
|
185
|
+
public setLogRotateThresholdMB(logRotateThresholdMB: number) {
|
|
186
|
+
this.logRotateThresholdMB = logRotateThresholdMB;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Set the maximum total size of the logfile
|
|
191
|
+
*/
|
|
192
|
+
public setLogRotateMaxTotalSizeMB(logRotateMaxTotalSizeMB: number) {
|
|
193
|
+
this.logRotateMaxTotalSizeMB = logRotateMaxTotalSizeMB;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { path as appRoot } from 'app-root-path';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Library constants
|
|
6
|
+
*/
|
|
7
|
+
export class Constants {
|
|
8
|
+
/**
|
|
9
|
+
* The library root. When this library is used
|
|
10
|
+
* as a dependency in a project, the "libRoot"
|
|
11
|
+
* will be the path to the dependency folder,
|
|
12
|
+
* inside the "node_modules".
|
|
13
|
+
*/
|
|
14
|
+
public libRoot: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The app root. When this library is used
|
|
18
|
+
* as a dependency in a project, the "appRoot"
|
|
19
|
+
* will be the path to the root project!
|
|
20
|
+
*/
|
|
21
|
+
public appRoot: string;
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
// From the "dist/src/config" folder
|
|
25
|
+
this.libRoot = path.normalize(__dirname + '/../../..');
|
|
26
|
+
this.appRoot = appRoot;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Logging constants
|
|
31
|
+
*/
|
|
32
|
+
get logging() {
|
|
33
|
+
return {
|
|
34
|
+
/**
|
|
35
|
+
* The properties that can be added to a log entry.
|
|
36
|
+
*/
|
|
37
|
+
properties: {
|
|
38
|
+
/**
|
|
39
|
+
* The type of log. Those types are specified in
|
|
40
|
+
* the following "logType" section.
|
|
41
|
+
*/
|
|
42
|
+
LOG_TYPE: 'logType',
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The version of the log type.
|
|
46
|
+
*/
|
|
47
|
+
LOG_TYPE_VERSION: 'logTypeVersion',
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* "Nom du composant logiciel"
|
|
51
|
+
*/
|
|
52
|
+
APP_NAME: 'app',
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* "Version du composant logiciel"
|
|
56
|
+
*/
|
|
57
|
+
APP_VERSION: 'version',
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Correlation id
|
|
61
|
+
*/
|
|
62
|
+
CORRELATION_ID: 'cid'
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The types of logs
|
|
67
|
+
*/
|
|
68
|
+
logType: {
|
|
69
|
+
/**
|
|
70
|
+
* The type for our Ville de Montréal logs.
|
|
71
|
+
*/
|
|
72
|
+
MONTREAL: 'mtl'
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export let constants: Constants = new Constants();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Stream } from 'stream';
|
|
2
|
+
const prettyStream = require('bunyan-prettystream-circularsafe');
|
|
3
|
+
|
|
4
|
+
export class ConsoleStream extends Stream {
|
|
5
|
+
private prettyStdOut: any;
|
|
6
|
+
private isLogHumanReadableinConsole: boolean;
|
|
7
|
+
|
|
8
|
+
constructor(isLogHumanReadableinConsole: boolean) {
|
|
9
|
+
super();
|
|
10
|
+
this.isLogHumanReadableinConsole = isLogHumanReadableinConsole;
|
|
11
|
+
this.prettyStdOut = new prettyStream();
|
|
12
|
+
this.prettyStdOut.pipe(process.stdout);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public write(data: any): void {
|
|
16
|
+
// Using human readable format?
|
|
17
|
+
if (this.isLogHumanReadableinConsole) {
|
|
18
|
+
this.prettyStdOut.write(data);
|
|
19
|
+
} else {
|
|
20
|
+
let dataClean = data;
|
|
21
|
+
if (typeof dataClean !== 'string') {
|
|
22
|
+
dataClean = JSON.stringify(dataClean);
|
|
23
|
+
}
|
|
24
|
+
dataClean += '\n';
|
|
25
|
+
|
|
26
|
+
process.stdout.write(dataClean);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ILogger, LogLevel } from './logger';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A Logger wrapper that allows to lazyly create a
|
|
5
|
+
* real Logger.
|
|
6
|
+
*
|
|
7
|
+
* The first time a log is perform, the
|
|
8
|
+
* "loggerCreator" will be called to create
|
|
9
|
+
* the actual Logger.
|
|
10
|
+
*/
|
|
11
|
+
export class LazyLogger implements ILogger {
|
|
12
|
+
private realLogger: ILogger;
|
|
13
|
+
private name: string;
|
|
14
|
+
private loggerCreator: (name: string) => ILogger;
|
|
15
|
+
|
|
16
|
+
constructor(name: string, loggerCreator: (name: string) => ILogger) {
|
|
17
|
+
this.name = name;
|
|
18
|
+
|
|
19
|
+
if (!loggerCreator) {
|
|
20
|
+
throw new Error(`The Logger Creator is required!`);
|
|
21
|
+
}
|
|
22
|
+
this.loggerCreator = loggerCreator;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public debug(messageObj: any, txtMsg?: string): void {
|
|
26
|
+
return this.getRealLogger().debug(messageObj, txtMsg);
|
|
27
|
+
}
|
|
28
|
+
public info(messageObj: any, txtMsg?: string): void {
|
|
29
|
+
return this.getRealLogger().info(messageObj, txtMsg);
|
|
30
|
+
}
|
|
31
|
+
public warning(messageObj: any, txtMsg?: string): void {
|
|
32
|
+
return this.getRealLogger().warning(messageObj, txtMsg);
|
|
33
|
+
}
|
|
34
|
+
public error(messageObj: any, txtMsg?: string): void {
|
|
35
|
+
return this.getRealLogger().error(messageObj, txtMsg);
|
|
36
|
+
}
|
|
37
|
+
public log(level: LogLevel, messageObj: any, txtMsg?: string): void {
|
|
38
|
+
return this.getRealLogger().log(level, messageObj, txtMsg);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected getRealLogger(): ILogger {
|
|
42
|
+
if (!this.realLogger) {
|
|
43
|
+
this.realLogger = this.loggerCreator(this.name);
|
|
44
|
+
if (!this.realLogger) {
|
|
45
|
+
throw new Error(`The Logger Creator must create a valid Logger!`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return this.realLogger;
|
|
49
|
+
}
|
|
50
|
+
}
|