zeddemore-logger 1.1.0 → 1.2.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/lib/color.js +25 -21
- package/lib/enums.js +12 -12
- package/lib/mappings/color_mappings.js +36 -28
- package/lib/zeddemore-logger.js +96 -12
- package/package.json +1 -1
package/lib/color.js
CHANGED
|
@@ -6,7 +6,7 @@ const chalk = require('chalk');
|
|
|
6
6
|
const colorMappings = require('./mappings/color_mappings');
|
|
7
7
|
const enums = require('./enums');
|
|
8
8
|
|
|
9
|
-
const { chalkLayers: cl, chalkFormats:
|
|
9
|
+
const { chalkLayers: cl, chalkFormats: cf, hexColors: hc } = enums;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @param ansiColor {number}
|
|
@@ -24,22 +24,6 @@ function chalkAnsi(ansiColor, layer = cl.FOREGROUND, string) {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
/**
|
|
28
|
-
* @param ansiColor {number}
|
|
29
|
-
* @param layer {string}
|
|
30
|
-
* @param string {string}
|
|
31
|
-
* @returns {string}
|
|
32
|
-
*/
|
|
33
|
-
function chalkAnsi256(ansiColor, layer = cl.FOREGROUND, string) {
|
|
34
|
-
if (!string) return '';
|
|
35
|
-
if (!ansiColor) return string;
|
|
36
|
-
switch (layer) {
|
|
37
|
-
case cl.BACKGROUND: return chalk.bgAnsi256(ansiColor)(string);
|
|
38
|
-
case cl.FOREGROUND: return chalk.ansi256(ansiColor)(string);
|
|
39
|
-
default: return string;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
27
|
/**
|
|
44
28
|
* @param hexColor {string}
|
|
45
29
|
* @param layer {string}
|
|
@@ -66,11 +50,29 @@ function chalkHttpStatuses(statusCode, string = null) {
|
|
|
66
50
|
return chalkedMatch;
|
|
67
51
|
}
|
|
68
52
|
|
|
53
|
+
function chalkLogLevel(logLevel, string = null) {
|
|
54
|
+
return chalkMatch(logLevel, string, colorMappings.logLevelColors);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function chalkMatch(match, string, colorsMap) {
|
|
58
|
+
if (!string) string = match;
|
|
59
|
+
const colorMap = _.find(colorsMap, { match });
|
|
60
|
+
if (!colorMap || !colorMap.style) return string;
|
|
61
|
+
return chalkTarget(string, colorMap.style);
|
|
62
|
+
}
|
|
63
|
+
|
|
69
64
|
function chalkHttpVerbs(verb, string = null, replaceGlobal = false) {
|
|
70
65
|
if (!string) string = verb;
|
|
71
66
|
return chalkViaColorMap(verb, string, colorMappings.httpVerbColors, replaceGlobal);
|
|
72
67
|
}
|
|
73
68
|
|
|
69
|
+
function chalkPackage(packageName, string = null) {
|
|
70
|
+
if (!string) string = packageName;
|
|
71
|
+
const colorMap = _.find(colorMappings.packageColors, { match: packageName });
|
|
72
|
+
if (!colorMap || !colorMap.style) return chalkTarget(string, { format: cf.HEX, fore: hc.BEARDON_RED, back: null });
|
|
73
|
+
return chalkTarget(string, colorMap.style);
|
|
74
|
+
}
|
|
75
|
+
|
|
74
76
|
/**
|
|
75
77
|
* @param redColor {number}
|
|
76
78
|
* @param greenColor {number}
|
|
@@ -94,10 +96,9 @@ function chalkTarget(target, style) {
|
|
|
94
96
|
const fore = style.fore;
|
|
95
97
|
const back = style.back;
|
|
96
98
|
switch (style.format) {
|
|
97
|
-
case
|
|
98
|
-
case
|
|
99
|
-
case
|
|
100
|
-
case cs.RGB: return chalkRgb(back.r, back.g, back.b, cl.BACKGROUND, chalkRgb(fore.r, fore.g, fore.b, cl.FOREGROUND, target));
|
|
99
|
+
case cf.ANSI: return chalkAnsi(back, cl.BACKGROUND, chalkAnsi(fore, cl.FOREGROUND, target));
|
|
100
|
+
case cf.HEX: return chalkHex(back, cl.BACKGROUND, chalkHex(fore, cl.FOREGROUND, target));
|
|
101
|
+
case cf.RGB: return chalkRgb(back.r, back.g, back.b, cl.BACKGROUND, chalkRgb(fore.r, fore.g, fore.b, cl.FOREGROUND, target));
|
|
101
102
|
default: return target;
|
|
102
103
|
}
|
|
103
104
|
}
|
|
@@ -178,5 +179,8 @@ function stringToIdempotentHslValues(str, hslOptions) {
|
|
|
178
179
|
module.exports = {
|
|
179
180
|
chalkHttpStatuses,
|
|
180
181
|
chalkHttpVerbs,
|
|
182
|
+
chalkLogLevel,
|
|
183
|
+
chalkPackage,
|
|
184
|
+
chalkTarget,
|
|
181
185
|
stringToIdempotentHexColor,
|
|
182
186
|
};
|
package/lib/enums.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
// Copyright (c) 2025 by Beardon Services, Inc.
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const ansiColors = {
|
|
4
|
+
HTTP_CLIENT_ERROR: 33, // red
|
|
5
|
+
HTTP_INFORMATIONAL: 0, // white
|
|
6
|
+
HTTP_REDIRECTION: 36, // blue
|
|
7
|
+
HTTP_SERVER_ERROR: 31, // red
|
|
8
|
+
HTTP_SUCCESSFUL: 32, // green
|
|
4
9
|
LOG_LEVEL_DEBUG: 34, // blue
|
|
5
10
|
LOG_LEVEL_ERROR: 31, // red
|
|
6
11
|
LOG_LEVEL_HTTP: 32, // green
|
|
@@ -10,14 +15,6 @@ const ansi256Colors = {
|
|
|
10
15
|
LOG_LEVEL_WARNING: 33, // yellow
|
|
11
16
|
};
|
|
12
17
|
|
|
13
|
-
const ansiColors = {
|
|
14
|
-
HTTP_CLIENT_ERROR: 33, // red
|
|
15
|
-
HTTP_INFORMATIONAL: 0,
|
|
16
|
-
HTTP_REDIRECTION: 36, // blue
|
|
17
|
-
HTTP_SERVER_ERROR: 31, // red
|
|
18
|
-
HTTP_SUCCESSFUL: 32, // green
|
|
19
|
-
};
|
|
20
|
-
|
|
21
18
|
const apiLogLevelCaptions = {
|
|
22
19
|
DEFAULT: 'Default',
|
|
23
20
|
ERROR: 'Error',
|
|
@@ -42,7 +39,6 @@ const apiLogLevels = {
|
|
|
42
39
|
|
|
43
40
|
const chalkFormats = {
|
|
44
41
|
ANSI: 'ansi',
|
|
45
|
-
ANSI256: 'ansi256',
|
|
46
42
|
HEX: 'hex',
|
|
47
43
|
RGB: 'rgb',
|
|
48
44
|
};
|
|
@@ -61,6 +57,7 @@ const databaseOperations = {
|
|
|
61
57
|
};
|
|
62
58
|
|
|
63
59
|
const hexColors = {
|
|
60
|
+
BEARDON_RED: '#951A1D',
|
|
64
61
|
BLACK: '#000000',
|
|
65
62
|
CONSOLE_WHITE: '#ECECEC',
|
|
66
63
|
DB_DELETE: '#F22613',
|
|
@@ -77,7 +74,11 @@ const hexColors = {
|
|
|
77
74
|
HTTP_POST: '#F4DA7A',
|
|
78
75
|
HTTP_PUT: '#74AEF6',
|
|
79
76
|
HTTP_TRACE: '#FFFFFF',
|
|
80
|
-
|
|
77
|
+
PACKAGE_EXPRESS: '#F7DF1E',
|
|
78
|
+
PACKAGE_MYSQL: '#00618A',
|
|
79
|
+
PACKAGE_NODE_JS: '#689F63',
|
|
80
|
+
PACKAGE_POSTGRESQL: '#336791',
|
|
81
|
+
PACKAGE_SEQUELIZE: '#00AFEF',
|
|
81
82
|
};
|
|
82
83
|
|
|
83
84
|
const httpStatusCodeGroups = {
|
|
@@ -145,7 +146,6 @@ const winstonLogLevels = {
|
|
|
145
146
|
};
|
|
146
147
|
|
|
147
148
|
module.exports = {
|
|
148
|
-
ansi256Colors,
|
|
149
149
|
ansiColors,
|
|
150
150
|
apiLogLevelCaptions,
|
|
151
151
|
apiLogLevels,
|
|
@@ -3,45 +3,52 @@
|
|
|
3
3
|
const _ = require('lodash');
|
|
4
4
|
const enums = require('../enums');
|
|
5
5
|
|
|
6
|
-
const {
|
|
7
|
-
httpMethods: http, winstonLogLevels: wll } = enums;
|
|
6
|
+
const { ansiColors: ac, databaseOperations: dbo, chalkFormats: cf, hexColors: hc, httpMethods: http, winstonLogLevels: wll } = enums;
|
|
8
7
|
|
|
9
8
|
const databaseOperationColors = [
|
|
10
|
-
{ match: dbo.DELETE, style: { format:
|
|
11
|
-
{ match: dbo.INSERT, style: { format:
|
|
12
|
-
{ match: dbo.SELECT, style: { format:
|
|
13
|
-
{ match: dbo.TRUNCATE, style: { format:
|
|
14
|
-
{ match: dbo.UPDATE, style: { format:
|
|
9
|
+
{ match: dbo.DELETE, style: { format: cf.HEX, fore: hc.DB_DELETE, back: null } },
|
|
10
|
+
{ match: dbo.INSERT, style: { format: cf.HEX, fore: hc.DB_INSERT, back: null } },
|
|
11
|
+
{ match: dbo.SELECT, style: { format: cf.HEX, fore: hc.DB_SELECT, back: null } },
|
|
12
|
+
{ match: dbo.TRUNCATE, style: { format: cf.HEX, fore: hc.DB_TRUNCATE, back: null } },
|
|
13
|
+
{ match: dbo.UPDATE, style: { format: cf.HEX, fore: hc.DB_UPDATE, back: null } },
|
|
15
14
|
];
|
|
16
15
|
|
|
17
16
|
const httpStatusColors = [
|
|
18
|
-
{ range: _.range(500, 600), style: { format:
|
|
19
|
-
{ range: _.range(400, 500), style: { format:
|
|
20
|
-
{ range: _.range(300, 400), style: { format:
|
|
21
|
-
{ range: _.range(200, 300), style: { format:
|
|
22
|
-
{ range: _.range(100, 200), style: { format:
|
|
17
|
+
{ range: _.range(500, 600), style: { format: cf.ANSI, fore: ac.HTTP_SERVER_ERROR, back: null } },
|
|
18
|
+
{ range: _.range(400, 500), style: { format: cf.ANSI, fore: ac.HTTP_CLIENT_ERROR, back: null } },
|
|
19
|
+
{ range: _.range(300, 400), style: { format: cf.ANSI, fore: ac.HTTP_REDIRECTION, back: null } },
|
|
20
|
+
{ range: _.range(200, 300), style: { format: cf.ANSI, fore: ac.HTTP_SUCCESSFUL, back: null } },
|
|
21
|
+
{ range: _.range(100, 200), style: { format: cf.ANSI, fore: ac.HTTP_INFORMATIONAL, back: null } },
|
|
23
22
|
];
|
|
24
23
|
|
|
25
24
|
const httpVerbColors = [
|
|
26
|
-
{ match: http.CONNECT, style: { format:
|
|
27
|
-
{ match: http.DELETE, style: { format:
|
|
28
|
-
{ match: http.GET, style: { format:
|
|
29
|
-
{ match: http.HEAD, style: { format:
|
|
30
|
-
{ match: http.OPTIONS, style: { format:
|
|
31
|
-
{ match: http.PATCH, style: { format:
|
|
32
|
-
{ match: http.POST, style: { format:
|
|
33
|
-
{ match: http.PUT, style: { format:
|
|
34
|
-
{ match: http.TRACE, style: { format:
|
|
25
|
+
{ match: http.CONNECT, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_CONNECT } },
|
|
26
|
+
{ match: http.DELETE, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_DELETE } },
|
|
27
|
+
{ match: http.GET, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_GET } },
|
|
28
|
+
{ match: http.HEAD, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_HEAD } },
|
|
29
|
+
{ match: http.OPTIONS, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_OPTIONS } },
|
|
30
|
+
{ match: http.PATCH, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_PATCH } },
|
|
31
|
+
{ match: http.POST, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_POST } },
|
|
32
|
+
{ match: http.PUT, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_PUT } },
|
|
33
|
+
{ match: http.TRACE, style: { format: cf.HEX, fore: hc.BLACK, back: hc.HTTP_TRACE } },
|
|
35
34
|
];
|
|
36
35
|
|
|
37
36
|
const logLevelColors = [
|
|
38
|
-
{ match: wll.ERROR, style: { format:
|
|
39
|
-
{ match: wll.WARNING, style: { format:
|
|
40
|
-
{ match: wll.INFO, style: { format:
|
|
41
|
-
{ match: wll.HTTP, style: { format:
|
|
42
|
-
{ match: wll.VERBOSE, style: { format:
|
|
43
|
-
{ match: wll.DEBUG, style: { format:
|
|
44
|
-
{ match: wll.SILLY, style: { format:
|
|
37
|
+
{ match: wll.ERROR, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_ERROR, back: null } },
|
|
38
|
+
{ match: wll.WARNING, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_WARNING, back: null } },
|
|
39
|
+
{ match: wll.INFO, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_INFO, back: null } },
|
|
40
|
+
{ match: wll.HTTP, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_HTTP, back: null } },
|
|
41
|
+
{ match: wll.VERBOSE, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_VERBOSE, back: null } },
|
|
42
|
+
{ match: wll.DEBUG, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_DEBUG, back: null } },
|
|
43
|
+
{ match: wll.SILLY, style: { format: cf.ANSI, fore: ac.LOG_LEVEL_SILLY, back: null } },
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const packageColors = [
|
|
47
|
+
{ match: 'express', style: { format: cf.HEX, fore: hc.PACKAGE_EXPRESS, back: null } },
|
|
48
|
+
{ match: 'MySQL', style: { format: cf.HEX, fore: hc.PACKAGE_MYSQL, back: null } },
|
|
49
|
+
{ match: 'Node.js', style: { format: cf.HEX, fore: hc.PACKAGE_NODE_JS, back: null } },
|
|
50
|
+
{ match: 'PostgreSQL', style: { format: cf.HEX, fore: hc.PACKAGE_POSTGRESQL, back: null } },
|
|
51
|
+
{ match: 'sequelize', style: { format: cf.HEX, fore: hc.PACKAGE_SEQUELIZE, back: null } },
|
|
45
52
|
];
|
|
46
53
|
|
|
47
54
|
module.exports = {
|
|
@@ -49,4 +56,5 @@ module.exports = {
|
|
|
49
56
|
httpStatusColors,
|
|
50
57
|
httpVerbColors,
|
|
51
58
|
logLevelColors,
|
|
59
|
+
packageColors,
|
|
52
60
|
};
|
package/lib/zeddemore-logger.js
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const _ = require('lodash');
|
|
4
4
|
const chalk = require('chalk');
|
|
5
|
-
const { createLogger: createWinstonLogger, format, transports } = require('winston');
|
|
5
|
+
const { createLogger: createWinstonLogger, format, Logger, transports } = require('winston');
|
|
6
6
|
const morgan = require('morgan');
|
|
7
7
|
const sizeof = require('object-sizeof');
|
|
8
8
|
const { v4: uuid } = require('uuid');
|
|
9
9
|
|
|
10
10
|
const { areHeadersSent, isResponseSuccessful } = require('./response');
|
|
11
|
-
const { chalkHttpStatuses, chalkHttpVerbs, stringToIdempotentHexColor } = require('./color');
|
|
11
|
+
const { chalkHttpStatuses, chalkHttpVerbs, chalkLogLevel, chalkPackage, chalkTarget, stringToIdempotentHexColor } = require('./color');
|
|
12
12
|
const { convertBytes } = require('./math');
|
|
13
13
|
const { convertMilliseconds } = require('./date');
|
|
14
14
|
const enums = require('./enums');
|
|
@@ -24,12 +24,22 @@ const DEFAULT_REQUEST_ID_ATTRIBUTE = 'requestId';
|
|
|
24
24
|
const DEFAULT_USER_LOG_LEVEL = -1;
|
|
25
25
|
const WINSTON_CONSOLE_TRANSPORT_NAME = 'console';
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Convert a log level to a Winston log level name
|
|
29
|
+
* @param logLevel {number|string}
|
|
30
|
+
* @returns {string}
|
|
31
|
+
*/
|
|
27
32
|
function massageLogLevel(logLevel) {
|
|
28
33
|
if (!logLevel) return wlln.VERBOSE;
|
|
29
34
|
if (!_.isInteger(logLevel)) return logLevel;
|
|
30
35
|
return winstonLogLevelIdToName(logLevel);
|
|
31
36
|
}
|
|
32
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Convert a log level ID to a Winston log level name
|
|
40
|
+
* @param levelId {number}
|
|
41
|
+
* @returns {string}
|
|
42
|
+
*/
|
|
33
43
|
function winstonLogLevelIdToName(levelId) {
|
|
34
44
|
const logLevelEntry = _.find(logLevelMap, { winstonLevel: levelId });
|
|
35
45
|
return logLevelEntry ? logLevelEntry.winstonName : wlln.HTTP;
|
|
@@ -38,11 +48,14 @@ function winstonLogLevelIdToName(levelId) {
|
|
|
38
48
|
class ZeddemoreLogger {
|
|
39
49
|
|
|
40
50
|
#forceLogLevel = false;
|
|
51
|
+
/** @type {Logger} */
|
|
52
|
+
#logger = null;
|
|
41
53
|
#logLevel = wll.INFO;
|
|
42
54
|
|
|
43
55
|
constructor(options) {
|
|
44
56
|
options = options || { };
|
|
45
57
|
this.apiCallModel = options.apiCallModel || null;
|
|
58
|
+
this.apiRouteCategories = options.apiRouteCategories || [ ];
|
|
46
59
|
this.apiRoutes = options.apiRoutes || [ ];
|
|
47
60
|
this.apiVersion = options.apiVersion || null;
|
|
48
61
|
this.defaultLabel = options.defaultLabel || 'zeddemore-logger';
|
|
@@ -73,6 +86,11 @@ class ZeddemoreLogger {
|
|
|
73
86
|
return this.#forceLogLevel;
|
|
74
87
|
}
|
|
75
88
|
|
|
89
|
+
get logger() {
|
|
90
|
+
if (!this.#logger) this.#logger = this.createLogger();
|
|
91
|
+
return this.#logger;
|
|
92
|
+
}
|
|
93
|
+
|
|
76
94
|
get logLevel() {
|
|
77
95
|
return this.#logLevel;
|
|
78
96
|
}
|
|
@@ -199,6 +217,14 @@ class ZeddemoreLogger {
|
|
|
199
217
|
return uuid(null, null, null);
|
|
200
218
|
}
|
|
201
219
|
|
|
220
|
+
static getLogger(options) {
|
|
221
|
+
options = options || { };
|
|
222
|
+
const logger = options.logger || (options.user ? options.user.logger : null) || ((app && app.locals) ? app.locals.logger : null);
|
|
223
|
+
if (logger) return logger;
|
|
224
|
+
const zeddemoreLogger = new ZeddemoreLogger();
|
|
225
|
+
return zeddemoreLogger.createLogger(options);
|
|
226
|
+
}
|
|
227
|
+
|
|
202
228
|
#getRequestId = (req) => {
|
|
203
229
|
if (!req) return null;
|
|
204
230
|
return req[ this.requestIdAttribute ];
|
|
@@ -223,12 +249,70 @@ class ZeddemoreLogger {
|
|
|
223
249
|
morgan.token('status-colored', (req, res) => { return buildStatusColoredToken(res); });
|
|
224
250
|
}
|
|
225
251
|
|
|
226
|
-
|
|
252
|
+
log = (logLevel) => {
|
|
253
|
+
logLevel = logLevel || this.#logLevel;
|
|
254
|
+
const logLevelName = massageLogLevel(logLevel);
|
|
255
|
+
return this.logger[ logLevelName ];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#logServerInfo = (options, logLevel = wll.INFO) => {
|
|
227
259
|
options = options || { };
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
const
|
|
231
|
-
|
|
260
|
+
const showLogLevel = options.hasOwnProperty('showLogLevel') ? options.showLogLevel : true;
|
|
261
|
+
const logLevelLabel = chalkLogLevel(this.#logLevel, this.logLevelName.toUpperCase());
|
|
262
|
+
const msgParts = [ `Server is ${ chalk.bold(chalk.yellowBright('⚡live⚡')) }` ];
|
|
263
|
+
if (showLogLevel) msgParts.push(`with log level ${ logLevelLabel }${ this.#forceLogLevel ? ` (${ chalk.italic('forced') })` : '' }`);
|
|
264
|
+
if (options.url) msgParts.push(`at ${ chalk.underline(options.url) }`);
|
|
265
|
+
this.log(logLevel)(msgParts.join(' '));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
#logServerListening = (options, logLevel = wll.INFO) => {
|
|
269
|
+
const serverEnvironment = options.environment || process.env.NODE_ENV || 'development';
|
|
270
|
+
const serverInstance = options.instance || (process.env.NODE_APP_INSTANCE && (process.env.NODE_APP_INSTANCE.length > 1)) ? process.env.NODE_APP_INSTANCE : null; // handles goofy PM2 NODE_APP_INSTANCE of '0'
|
|
271
|
+
const serverName = options.name || this.defaultLabel;
|
|
272
|
+
const serverPort = options.port || process.env.PORT || 3000;
|
|
273
|
+
const msgParts = [ ];
|
|
274
|
+
const serverParts = [ serverName ];
|
|
275
|
+
if (serverEnvironment) serverParts.push(serverEnvironment);
|
|
276
|
+
if (serverInstance) serverParts.push(serverInstance);
|
|
277
|
+
msgParts.push(`[${ serverParts.join(':') }]`);
|
|
278
|
+
msgParts.push(`Node.js Express server listening on port ${ chalk.bold(serverPort) }`);
|
|
279
|
+
this.log(logLevel)(msgParts.join(' '));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
#logServerPackagesInfo = (options, packages, logLevel = wll.INFO) => {
|
|
283
|
+
function buildVersionLabel(_package) {
|
|
284
|
+
if (!_package || !_package.name || !_package.version) return null;
|
|
285
|
+
const packageName = _package.style ? chalkTarget(_package.name, _package.style) : chalkPackage(_package.name);
|
|
286
|
+
return `${ packageName }@${ _package.version }`;
|
|
287
|
+
}
|
|
288
|
+
const versionParts = _.compact(_.sortBy(packages, (_package) => _package.name.toUpperCase()).map(buildVersionLabel));
|
|
289
|
+
this.log(logLevel)(`Packages: ${ versionParts.join(' | ') }`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#logServerRoutesAdded = (options, logLevel = wll.INFO) => {
|
|
293
|
+
options = options || { };
|
|
294
|
+
const showCategories = options.hasOwnProperty('showCategories') ? options.showCategories : true;
|
|
295
|
+
const routes = options.routes || this.apiRoutes || [ ];
|
|
296
|
+
const routesLabel = (routes.length === 1) ? 'route' : 'routes';
|
|
297
|
+
const addedLabel = chalk.green('added');
|
|
298
|
+
const msgParts = [ `${ routes.length } ${ routesLabel } ${ addedLabel }` ];
|
|
299
|
+
if (showCategories) {
|
|
300
|
+
const categories = options.categories || this.apiRouteCategories || [];
|
|
301
|
+
msgParts.push(`in: ${ categories.sort().join('/') }`);
|
|
302
|
+
}
|
|
303
|
+
this.log(logLevel)(msgParts.join(', '));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
logServerStartup = (options, logLevel = wll.INFO) => {
|
|
307
|
+
options = options || { };
|
|
308
|
+
const showListening = options.hasOwnProperty('showListening') ? options.showListening : true;
|
|
309
|
+
const showPackages = options.hasOwnProperty('showPackages') ? options.showPackages : true;
|
|
310
|
+
const showRoutes = options.hasOwnProperty('showRoutes') ? options.showRoutes : true;
|
|
311
|
+
const showInfo = options.hasOwnProperty('showInfo') ? options.showInfo : true;
|
|
312
|
+
if (showListening) this.#logServerListening(options, logLevel);
|
|
313
|
+
if (showPackages && options.packages) this.#logServerPackagesInfo(options, options.packages, logLevel);
|
|
314
|
+
if (showRoutes) this.#logServerRoutesAdded(options, logLevel);
|
|
315
|
+
if (showInfo) this.#logServerInfo(options, logLevel);
|
|
232
316
|
}
|
|
233
317
|
|
|
234
318
|
#matchPathToRoute = (path) => {
|
|
@@ -258,7 +342,7 @@ class ZeddemoreLogger {
|
|
|
258
342
|
const logOptions = { };
|
|
259
343
|
const user = userGetFn ? userGetFn(req, res) : null;
|
|
260
344
|
if (user) logOptions.user = user;
|
|
261
|
-
ZeddemoreLogger.
|
|
345
|
+
ZeddemoreLogger.getLogger(logOptions).http(this.#colorizeResponse(message.trim()));
|
|
262
346
|
},
|
|
263
347
|
},
|
|
264
348
|
})(req, res, next);
|
|
@@ -347,7 +431,7 @@ class ZeddemoreLogger {
|
|
|
347
431
|
await this.apiCallModel.create(appCallValues);
|
|
348
432
|
} catch (e) {
|
|
349
433
|
console.error(e);
|
|
350
|
-
ZeddemoreLogger.
|
|
434
|
+
ZeddemoreLogger.getLogger({ user }).error('Api call logging failed, moving on');
|
|
351
435
|
}
|
|
352
436
|
}
|
|
353
437
|
}
|
|
@@ -384,7 +468,7 @@ class ZeddemoreLogger {
|
|
|
384
468
|
const _response = axiosError.response;
|
|
385
469
|
if (!_response.request) return;
|
|
386
470
|
const _request = _response.request;
|
|
387
|
-
const logger = options.logger || ZeddemoreLogger.
|
|
471
|
+
const logger = options.logger || ZeddemoreLogger.getLogger(options);
|
|
388
472
|
if (!logger) return;
|
|
389
473
|
const _method = _request.method || null;
|
|
390
474
|
const method = _method ? chalkHttpVerbs(_method) : null;
|
|
@@ -413,7 +497,7 @@ class ZeddemoreLogger {
|
|
|
413
497
|
const axiosConfig = logOptions.axios || { };
|
|
414
498
|
const configData = (logOptions.data && _.isObject(axiosConfig.data)) ? JSON.stringify(axiosConfig.data) : null;
|
|
415
499
|
const logLevel = massageLogLevel(logOptions.logLevel);
|
|
416
|
-
const logger = options.logger || ZeddemoreLogger.
|
|
500
|
+
const logger = options.logger || ZeddemoreLogger.getLogger(options);
|
|
417
501
|
if (!logger) return;
|
|
418
502
|
const _method = _request.method || null;
|
|
419
503
|
const method = _method ? chalkHttpVerbs(_method) : null;
|
|
@@ -436,7 +520,7 @@ class ZeddemoreLogger {
|
|
|
436
520
|
}
|
|
437
521
|
|
|
438
522
|
module.exports = {
|
|
439
|
-
log: ZeddemoreLogger.
|
|
523
|
+
log: ZeddemoreLogger.getLogger,
|
|
440
524
|
winstonLogLevelIdToName,
|
|
441
525
|
writeAxiosErrorLog: ZeddemoreLogger.writeAxiosErrorLog,
|
|
442
526
|
writeAxiosLog: ZeddemoreLogger.writeAxiosResponseLog, // deprecated
|