zeddemore-logger 2.2.4 → 2.2.6
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/ZeddemoreLogger.js +7 -1
- package/lib/defaults.js +3 -1
- package/lib/enums.js +208 -207
- package/lib/request.js +69 -67
- package/package.json +34 -34
package/lib/ZeddemoreLogger.js
CHANGED
|
@@ -748,6 +748,7 @@ class ZeddemoreLogger extends ZeddemoreBase {
|
|
|
748
748
|
* Log server listening information
|
|
749
749
|
* @param {Object} options
|
|
750
750
|
* @param {String} [options.environment]
|
|
751
|
+
* @param {String} [options.listenHost]
|
|
751
752
|
* @param {String} [options.instance]
|
|
752
753
|
* @param {String} [options.name]
|
|
753
754
|
* @param {number} [options.port]
|
|
@@ -756,15 +757,20 @@ class ZeddemoreLogger extends ZeddemoreBase {
|
|
|
756
757
|
*/
|
|
757
758
|
#logServerListening = (options, logLevel = wll.INFO) => {
|
|
758
759
|
const serverEnvironment = options.environment || this.serverEnvironment;
|
|
760
|
+
const serverHost = options.listenHost || defaults.listenHost;
|
|
759
761
|
const serverInstance = options.instance || this.serverInstance;
|
|
760
762
|
const serverName = options.name || this.serverName;
|
|
761
763
|
const serverPort = options.port || this.serverPort;
|
|
764
|
+
const usingIPv4 = true;
|
|
765
|
+
const usingIPv6 = _.isString(serverHost) && serverHost.includes(defaults.listenHostIPv6Sentinel);
|
|
766
|
+
const ipVs = _.compact([ (usingIPv4 ? 'IPv4' : null), (usingIPv6 ? chalk.red('IPv6') : null) ]).join('/');
|
|
762
767
|
const msgParts = [ ];
|
|
763
768
|
const serverParts = [ serverName ];
|
|
764
769
|
if (serverEnvironment) serverParts.push(serverEnvironment);
|
|
765
770
|
if (serverInstance) serverParts.push(serverInstance);
|
|
766
771
|
msgParts.push(`[${ serverParts.join(':') }]`);
|
|
767
|
-
msgParts.push(`Node.js Express server listening on
|
|
772
|
+
msgParts.push(`Node.js Express server listening on ${ serverHost }:${ chalk.bold(serverPort) }`);
|
|
773
|
+
msgParts.push(`(${ ipVs })`);
|
|
768
774
|
this.log(logLevel)(msgParts.join(' '));
|
|
769
775
|
}
|
|
770
776
|
|
package/lib/defaults.js
CHANGED
|
@@ -12,7 +12,9 @@ module.exports = {
|
|
|
12
12
|
consoleDirExtensionDepth: null,
|
|
13
13
|
consoleDirExtensionShowHidden: true,
|
|
14
14
|
contentLengthDigits: 2,
|
|
15
|
-
|
|
15
|
+
listenHost: '::',
|
|
16
|
+
listenHostIPv6Sentinel: ':',
|
|
17
|
+
liveMessage: 'live',
|
|
16
18
|
logLevelType: llt.CONSOLE,
|
|
17
19
|
morganFormat: mf.ZEDDEMORE,
|
|
18
20
|
morganFormatTokens: [ mft.METHOD, mft.DECODED_URL, mft.STATUS, mft.RESPONSE_TIME_FORMAT, mft.CONTENT_LENGTH_FORMAT ],
|
package/lib/enums.js
CHANGED
|
@@ -1,207 +1,208 @@
|
|
|
1
|
-
// Copyright (c) 2025-2026 by Beardon Services, Inc.
|
|
2
|
-
|
|
3
|
-
function doesEnumInclude(set, value) {
|
|
4
|
-
for (const key in set) {
|
|
5
|
-
if (set[ key ] === value) return true;
|
|
6
|
-
}
|
|
7
|
-
return false;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const ansiColors = {
|
|
11
|
-
HTTP_CLIENT_ERROR: 33, // red
|
|
12
|
-
HTTP_INFORMATIONAL: 0, // white
|
|
13
|
-
HTTP_REDIRECTION: 36, // blue
|
|
14
|
-
HTTP_SERVER_ERROR: 31, // red
|
|
15
|
-
HTTP_SUCCESSFUL: 32, // green
|
|
16
|
-
LOG_LEVEL_DEBUG: 34, // blue
|
|
17
|
-
LOG_LEVEL_ERROR: 31, // red
|
|
18
|
-
LOG_LEVEL_HTTP: 32, // green
|
|
19
|
-
LOG_LEVEL_INFO: 32, // green
|
|
20
|
-
LOG_LEVEL_SILLY: 35, // magenta
|
|
21
|
-
LOG_LEVEL_VERBOSE: 36, // cyan
|
|
22
|
-
LOG_LEVEL_WARNING: 33, // yellow
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
const chalkFormats = {
|
|
26
|
-
ANSI: 'ansi',
|
|
27
|
-
HEX: 'hex',
|
|
28
|
-
RGB: 'rgb',
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const chalkLayers = {
|
|
32
|
-
BACKGROUND: 'bg',
|
|
33
|
-
FOREGROUND: 'fg',
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const hexColors = {
|
|
37
|
-
BEARDON_RED: '#951A1D',
|
|
38
|
-
BLACK: '#000000',
|
|
39
|
-
CONSOLE_WHITE: '#ECECEC',
|
|
40
|
-
DB_DELETE: '#F22613',
|
|
41
|
-
DB_INSERT: '#00B16A',
|
|
42
|
-
DB_SELECT: '#1E90FF',
|
|
43
|
-
DB_TRUNCATE: '#750505',
|
|
44
|
-
DB_UPDATE: '#F9690E',
|
|
45
|
-
HTTP_CONNECT: '#FFFFFF',
|
|
46
|
-
HTTP_DELETE: '#EF968A',
|
|
47
|
-
HTTP_GET: '#67D193',
|
|
48
|
-
HTTP_HEAD: '#68D696',
|
|
49
|
-
HTTP_OPTIONS: '#E55AA8',
|
|
50
|
-
HTTP_PATCH: '#C0A8E1',
|
|
51
|
-
HTTP_POST: '#F4DA7A',
|
|
52
|
-
HTTP_PUT: '#74AEF6',
|
|
53
|
-
HTTP_TRACE: '#FFFFFF',
|
|
54
|
-
PACKAGE_ANGULARJS: '#C04737',
|
|
55
|
-
PACKAGE_AXIOS: '#6200e1',
|
|
56
|
-
PACKAGE_EXPRESS: '#F7DF1E',
|
|
57
|
-
PACKAGE_HANDLEBARS: '#F0772B',
|
|
58
|
-
PACKAGE_JIMP: '#FF0000',
|
|
59
|
-
PACKAGE_MYSQL: '#00618A',
|
|
60
|
-
PACKAGE_NODE_JS: '#689F63',
|
|
61
|
-
PACKAGE_NODEMAILER: '#29ABE2',
|
|
62
|
-
PACKAGE_PASSPORT: '#51E492',
|
|
63
|
-
PACKAGE_PDFKIT: '#DB000D',
|
|
64
|
-
PACKAGE_POSTGRESQL: '#336791',
|
|
65
|
-
PACKAGE_REDIS: '#C23936',
|
|
66
|
-
PACKAGE_SEQUELIZE: '#00AFEF',
|
|
67
|
-
PACKAGE_ZEDDEMORE: '#E2B68F',
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const httpStatusCodeGroups = {
|
|
71
|
-
INFORMATIONAL: 1,
|
|
72
|
-
SUCCESSFUL: 2,
|
|
73
|
-
REDIRECTION: 3,
|
|
74
|
-
CLIENT_ERROR: 4,
|
|
75
|
-
SERVER_ERROR: 5,
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const httpMethods = {
|
|
79
|
-
GET: 'GET',
|
|
80
|
-
HEAD: 'HEAD',
|
|
81
|
-
POST: 'POST',
|
|
82
|
-
PUT: 'PUT',
|
|
83
|
-
DELETE: 'DELETE',
|
|
84
|
-
CONNECT: 'CONNECT',
|
|
85
|
-
OPTIONS: 'OPTIONS',
|
|
86
|
-
TRACE: 'TRACE',
|
|
87
|
-
PATCH: 'PATCH',
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const httpRequestHeaders = {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
1
|
+
// Copyright (c) 2025-2026 by Beardon Services, Inc.
|
|
2
|
+
|
|
3
|
+
function doesEnumInclude(set, value) {
|
|
4
|
+
for (const key in set) {
|
|
5
|
+
if (set[ key ] === value) return true;
|
|
6
|
+
}
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const ansiColors = {
|
|
11
|
+
HTTP_CLIENT_ERROR: 33, // red
|
|
12
|
+
HTTP_INFORMATIONAL: 0, // white
|
|
13
|
+
HTTP_REDIRECTION: 36, // blue
|
|
14
|
+
HTTP_SERVER_ERROR: 31, // red
|
|
15
|
+
HTTP_SUCCESSFUL: 32, // green
|
|
16
|
+
LOG_LEVEL_DEBUG: 34, // blue
|
|
17
|
+
LOG_LEVEL_ERROR: 31, // red
|
|
18
|
+
LOG_LEVEL_HTTP: 32, // green
|
|
19
|
+
LOG_LEVEL_INFO: 32, // green
|
|
20
|
+
LOG_LEVEL_SILLY: 35, // magenta
|
|
21
|
+
LOG_LEVEL_VERBOSE: 36, // cyan
|
|
22
|
+
LOG_LEVEL_WARNING: 33, // yellow
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const chalkFormats = {
|
|
26
|
+
ANSI: 'ansi',
|
|
27
|
+
HEX: 'hex',
|
|
28
|
+
RGB: 'rgb',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const chalkLayers = {
|
|
32
|
+
BACKGROUND: 'bg',
|
|
33
|
+
FOREGROUND: 'fg',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const hexColors = {
|
|
37
|
+
BEARDON_RED: '#951A1D',
|
|
38
|
+
BLACK: '#000000',
|
|
39
|
+
CONSOLE_WHITE: '#ECECEC',
|
|
40
|
+
DB_DELETE: '#F22613',
|
|
41
|
+
DB_INSERT: '#00B16A',
|
|
42
|
+
DB_SELECT: '#1E90FF',
|
|
43
|
+
DB_TRUNCATE: '#750505',
|
|
44
|
+
DB_UPDATE: '#F9690E',
|
|
45
|
+
HTTP_CONNECT: '#FFFFFF',
|
|
46
|
+
HTTP_DELETE: '#EF968A',
|
|
47
|
+
HTTP_GET: '#67D193',
|
|
48
|
+
HTTP_HEAD: '#68D696',
|
|
49
|
+
HTTP_OPTIONS: '#E55AA8',
|
|
50
|
+
HTTP_PATCH: '#C0A8E1',
|
|
51
|
+
HTTP_POST: '#F4DA7A',
|
|
52
|
+
HTTP_PUT: '#74AEF6',
|
|
53
|
+
HTTP_TRACE: '#FFFFFF',
|
|
54
|
+
PACKAGE_ANGULARJS: '#C04737',
|
|
55
|
+
PACKAGE_AXIOS: '#6200e1',
|
|
56
|
+
PACKAGE_EXPRESS: '#F7DF1E',
|
|
57
|
+
PACKAGE_HANDLEBARS: '#F0772B',
|
|
58
|
+
PACKAGE_JIMP: '#FF0000',
|
|
59
|
+
PACKAGE_MYSQL: '#00618A',
|
|
60
|
+
PACKAGE_NODE_JS: '#689F63',
|
|
61
|
+
PACKAGE_NODEMAILER: '#29ABE2',
|
|
62
|
+
PACKAGE_PASSPORT: '#51E492',
|
|
63
|
+
PACKAGE_PDFKIT: '#DB000D',
|
|
64
|
+
PACKAGE_POSTGRESQL: '#336791',
|
|
65
|
+
PACKAGE_REDIS: '#C23936',
|
|
66
|
+
PACKAGE_SEQUELIZE: '#00AFEF',
|
|
67
|
+
PACKAGE_ZEDDEMORE: '#E2B68F',
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const httpStatusCodeGroups = {
|
|
71
|
+
INFORMATIONAL: 1,
|
|
72
|
+
SUCCESSFUL: 2,
|
|
73
|
+
REDIRECTION: 3,
|
|
74
|
+
CLIENT_ERROR: 4,
|
|
75
|
+
SERVER_ERROR: 5,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const httpMethods = {
|
|
79
|
+
GET: 'GET',
|
|
80
|
+
HEAD: 'HEAD',
|
|
81
|
+
POST: 'POST',
|
|
82
|
+
PUT: 'PUT',
|
|
83
|
+
DELETE: 'DELETE',
|
|
84
|
+
CONNECT: 'CONNECT',
|
|
85
|
+
OPTIONS: 'OPTIONS',
|
|
86
|
+
TRACE: 'TRACE',
|
|
87
|
+
PATCH: 'PATCH',
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const httpRequestHeaders = {
|
|
91
|
+
ACCEPT_LANGUAGE: 'Accept-Language',
|
|
92
|
+
ALLOW: 'Allow',
|
|
93
|
+
APP_VERSION: 'App-Version',
|
|
94
|
+
AUTHORIZATION: 'Authorization',
|
|
95
|
+
BEARER_PREFIX: 'Bearer',
|
|
96
|
+
FORWARDED_FOR: 'X-Forwarded-For',
|
|
97
|
+
REQUEST_ID: 'X-Request-Id',
|
|
98
|
+
USER_AGENT: 'User-Agent',
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const httpResponseHeaders = {
|
|
102
|
+
CONTENT_LENGTH: 'Content-Length',
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const logLevelTypes = {
|
|
106
|
+
CONSOLE: 'console',
|
|
107
|
+
EMAIL: 'email',
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const morganFormats = {
|
|
111
|
+
COMBINED: 'combined',
|
|
112
|
+
COMMON: 'common',
|
|
113
|
+
DEV: 'dev',
|
|
114
|
+
SHORT: 'short',
|
|
115
|
+
TINY: 'tiny',
|
|
116
|
+
ZEDDEMORE: 'zeddemore', // added for `zeddemore-logger`
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const morganFormatTokens = {
|
|
120
|
+
CONTENT_LENGTH_FORMAT: 'content-length-format', // added for `zeddemore-logger`
|
|
121
|
+
DATE: 'date', // :date[format]
|
|
122
|
+
DECODED_URL: 'decoded-url', // added for `zeddemore-logger`
|
|
123
|
+
HTTP_VERSION: 'http-version',
|
|
124
|
+
METHOD: 'method',
|
|
125
|
+
REFERRER: 'referrer',
|
|
126
|
+
REMOTE_ADDR: 'remote-addr',
|
|
127
|
+
REMOTE_USER: 'remote-user',
|
|
128
|
+
REQ: 'req', // :req[header]
|
|
129
|
+
RES: 'res', // :res[header]
|
|
130
|
+
RESPONSE_TIME: 'response-time', // :response-time[digits]
|
|
131
|
+
RESPONSE_TIME_FORMAT: 'response-time-format', // added for `zeddemore-logger`
|
|
132
|
+
STATUS: 'status',
|
|
133
|
+
STATUS_COLORED: 'status-colored', // added for `zeddemore-logger`
|
|
134
|
+
TOTAL_TIME: 'total-time', // :total-time[digits]
|
|
135
|
+
URL: 'url',
|
|
136
|
+
USER_AGENT: 'user-agent',
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const winstonFormats = {
|
|
140
|
+
COLORIZE: 'colorize',
|
|
141
|
+
JSON: 'json',
|
|
142
|
+
METADATA: 'metadata',
|
|
143
|
+
PRETTY_PRINT: 'pretty_print',
|
|
144
|
+
SIMPLE: 'simple',
|
|
145
|
+
SPLAT: 'splat',
|
|
146
|
+
TIMESTAMP: 'timestamp',
|
|
147
|
+
ZEDDEMORE_INFO_MUTATION: 'zeddemore_info_mutation',
|
|
148
|
+
ZEDDEMORE_LABEL: 'zeddemore_label',
|
|
149
|
+
ZEDDEMORE_PRINTF: 'zeddemore_printf',
|
|
150
|
+
ZEDDEMORE_TIMESTAMP: 'zeddemore_timestamp',
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const winstonLogLevelNames = {
|
|
154
|
+
ERROR: 'error',
|
|
155
|
+
WARNING: 'warn',
|
|
156
|
+
INFO: 'info',
|
|
157
|
+
HTTP: 'http',
|
|
158
|
+
VERBOSE: 'verbose',
|
|
159
|
+
DEBUG: 'debug',
|
|
160
|
+
SILLY: 'silly',
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// using NPM logging levels
|
|
164
|
+
const winstonLogLevels = {
|
|
165
|
+
ERROR: 0,
|
|
166
|
+
WARNING: 1,
|
|
167
|
+
INFO: 2,
|
|
168
|
+
HTTP: 3,
|
|
169
|
+
VERBOSE: 4,
|
|
170
|
+
DEBUG: 5,
|
|
171
|
+
SILLY: 6,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const winstonTransports = {
|
|
175
|
+
CONSOLE: 'console',
|
|
176
|
+
EMAIL: 'email',
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const zeddemoreLogLevelCaptions = {
|
|
180
|
+
DEFAULT: 'Default',
|
|
181
|
+
ERROR: 'Error',
|
|
182
|
+
WARNING: 'Warning',
|
|
183
|
+
INFO: 'Info',
|
|
184
|
+
HTTP: 'HTTP',
|
|
185
|
+
VERBOSE: 'Verbose',
|
|
186
|
+
DEBUG: 'Debug',
|
|
187
|
+
SILLY: 'Silly',
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
module.exports = {
|
|
191
|
+
doesEnumInclude,
|
|
192
|
+
ansiColors,
|
|
193
|
+
chalkFormats,
|
|
194
|
+
chalkLayers,
|
|
195
|
+
hexColors,
|
|
196
|
+
httpStatusCodeGroups,
|
|
197
|
+
httpMethods,
|
|
198
|
+
httpRequestHeaders,
|
|
199
|
+
httpResponseHeaders,
|
|
200
|
+
logLevelTypes,
|
|
201
|
+
morganFormats,
|
|
202
|
+
morganFormatTokens,
|
|
203
|
+
winstonFormats,
|
|
204
|
+
winstonLogLevelNames,
|
|
205
|
+
winstonLogLevels,
|
|
206
|
+
winstonTransports,
|
|
207
|
+
zeddemoreLogLevelCaptions,
|
|
208
|
+
};
|
package/lib/request.js
CHANGED
|
@@ -1,67 +1,69 @@
|
|
|
1
|
-
// Copyright (c) 2025 by Beardon Services, Inc.
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash');
|
|
4
|
-
|
|
5
|
-
const enums = require('./enums');
|
|
6
|
-
|
|
7
|
-
const { httpRequestHeaders: rqh } = enums;
|
|
8
|
-
|
|
9
|
-
function extractToken(req) {
|
|
10
|
-
let token;
|
|
11
|
-
const authHeader = req.header(rqh.AUTHORIZATION);
|
|
12
|
-
if (authHeader && authHeader.startsWith(`${ rqh.BEARER_PREFIX } `)) {
|
|
13
|
-
token = req.header(rqh.AUTHORIZATION).split(' ')[ 1 ];
|
|
14
|
-
} else if (req.query && req.query.token) {
|
|
15
|
-
token = req.query.token;
|
|
16
|
-
}
|
|
17
|
-
return token;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function getRequestResponseTime(req, res) {
|
|
21
|
-
if (!req._startAt || !res._startAt) return null;
|
|
22
|
-
return (res._startAt[ 0 ] - req._startAt[ 0 ]) * 1e3 + (res._startAt[ 1 ] - req._startAt[ 1 ]) * 1e-6;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function isRequestOfMethod(req, method) {
|
|
26
|
-
const methods = Array.isArray(method) ? method : [ method ];
|
|
27
|
-
if (!req || !req.method || _.isEmpty(methods)) return false;
|
|
28
|
-
return methods.includes(req.method);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function parseRequest(req) {
|
|
32
|
-
if (!req) return { };
|
|
33
|
-
const body = req.body || { };
|
|
34
|
-
return {
|
|
35
|
-
acceptLanguage: parseRequestAcceptLanguage(req),
|
|
36
|
-
client: body.client || null,
|
|
37
|
-
clientVersion: body.applicationVersion || null,
|
|
38
|
-
ipAddress: parseRequestIpAddress(req),
|
|
39
|
-
sessionAge: body.uptime || 0,
|
|
40
|
-
sessionId: body.sessionId || null,
|
|
41
|
-
userAgent: parseRequestUserAgent(req),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function parseRequestAcceptLanguage(req) {
|
|
46
|
-
const body = req.body || { };
|
|
47
|
-
return body.language || req.header(
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function parseRequestIpAddress(req) {
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
1
|
+
// Copyright (c) 2025-2026 by Beardon Services, Inc.
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
const enums = require('./enums');
|
|
6
|
+
|
|
7
|
+
const { httpRequestHeaders: rqh } = enums;
|
|
8
|
+
|
|
9
|
+
function extractToken(req) {
|
|
10
|
+
let token;
|
|
11
|
+
const authHeader = req.header(rqh.AUTHORIZATION);
|
|
12
|
+
if (authHeader && authHeader.startsWith(`${ rqh.BEARER_PREFIX } `)) {
|
|
13
|
+
token = req.header(rqh.AUTHORIZATION).split(' ')[ 1 ];
|
|
14
|
+
} else if (req.query && req.query.token) {
|
|
15
|
+
token = req.query.token;
|
|
16
|
+
}
|
|
17
|
+
return token;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getRequestResponseTime(req, res) {
|
|
21
|
+
if (!req._startAt || !res._startAt) return null;
|
|
22
|
+
return (res._startAt[ 0 ] - req._startAt[ 0 ]) * 1e3 + (res._startAt[ 1 ] - req._startAt[ 1 ]) * 1e-6;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isRequestOfMethod(req, method) {
|
|
26
|
+
const methods = Array.isArray(method) ? method : [ method ];
|
|
27
|
+
if (!req || !req.method || _.isEmpty(methods)) return false;
|
|
28
|
+
return methods.includes(req.method);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseRequest(req) {
|
|
32
|
+
if (!req) return { };
|
|
33
|
+
const body = req.body || { };
|
|
34
|
+
return {
|
|
35
|
+
acceptLanguage: parseRequestAcceptLanguage(req),
|
|
36
|
+
client: body.client || null,
|
|
37
|
+
clientVersion: body.applicationVersion || null,
|
|
38
|
+
ipAddress: parseRequestIpAddress(req),
|
|
39
|
+
sessionAge: body.uptime || 0,
|
|
40
|
+
sessionId: body.sessionId || null,
|
|
41
|
+
userAgent: parseRequestUserAgent(req),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseRequestAcceptLanguage(req) {
|
|
46
|
+
const body = req.body || { };
|
|
47
|
+
return body.language || req.header(rqh.ACCEPT_LANGUAGE);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function parseRequestIpAddress(req) {
|
|
51
|
+
const LOCALHOST_IPS = [ '127.0.0.1', '::1' ];
|
|
52
|
+
const body = req.body || { };
|
|
53
|
+
const xForwardedFors = _.isString(req.get(rqh.FORWARDED_FOR)) ? req.get(rqh.FORWARDED_FOR).split(',').map((ip) => ip.trim()) : [ ];
|
|
54
|
+
const xForwardedForClient = !!xForwardedFors.length ? xForwardedFors[ 0 ] : null;
|
|
55
|
+
const ipAddress = body.ipAddress || xForwardedForClient || req.socket.remoteAddress;
|
|
56
|
+
return LOCALHOST_IPS.includes(ipAddress) ? 'localhost' : ipAddress;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function parseRequestUserAgent(req) {
|
|
60
|
+
const body = req.body || { };
|
|
61
|
+
return body.agent || body.osVersion || req.get(rqh.USER_AGENT);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = {
|
|
65
|
+
extractToken,
|
|
66
|
+
getRequestResponseTime,
|
|
67
|
+
isRequestOfMethod,
|
|
68
|
+
parseRequest,
|
|
69
|
+
};
|
package/package.json
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "zeddemore-logger",
|
|
3
|
-
"version": "2.2.
|
|
4
|
-
"description": "Node.js Express logger using Morgan and Winston for thread and caller info tracking",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "git+https://github.com/beardon/zeddemore-logger.git"
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"express",
|
|
11
|
-
"logger",
|
|
12
|
-
"morgan",
|
|
13
|
-
"winston"
|
|
14
|
-
],
|
|
15
|
-
"author": "Aaron Bean <aaron.bean@beardon.com> (https://beardon.com)",
|
|
16
|
-
"license": "UNLICENSED",
|
|
17
|
-
"bugs": {
|
|
18
|
-
"url": "https://github.com/beardon/zeddemore-logger/issues"
|
|
19
|
-
},
|
|
20
|
-
"homepage": "https://github.com/beardon/zeddemore-logger#readme",
|
|
21
|
-
"main": "lib/zeddemore-logger.js",
|
|
22
|
-
"dependencies": {
|
|
23
|
-
"ansi-html": "^0.0.9",
|
|
24
|
-
"chalk": "^4.1.2",
|
|
25
|
-
"lodash": "^4.18.1",
|
|
26
|
-
"luxon": "^3.7.1",
|
|
27
|
-
"morgan": "^1.10.1",
|
|
28
|
-
"nodemailer": "^8.0.5",
|
|
29
|
-
"object-sizeof": "^2.6.5",
|
|
30
|
-
"uuid": "^10.0.0",
|
|
31
|
-
"winston": "^3.17.0",
|
|
32
|
-
"winston-transport": "^4.9.0"
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "zeddemore-logger",
|
|
3
|
+
"version": "2.2.6",
|
|
4
|
+
"description": "Node.js Express logger using Morgan and Winston for thread and caller info tracking",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/beardon/zeddemore-logger.git"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"express",
|
|
11
|
+
"logger",
|
|
12
|
+
"morgan",
|
|
13
|
+
"winston"
|
|
14
|
+
],
|
|
15
|
+
"author": "Aaron Bean <aaron.bean@beardon.com> (https://beardon.com)",
|
|
16
|
+
"license": "UNLICENSED",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/beardon/zeddemore-logger/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/beardon/zeddemore-logger#readme",
|
|
21
|
+
"main": "lib/zeddemore-logger.js",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"ansi-html": "^0.0.9",
|
|
24
|
+
"chalk": "^4.1.2",
|
|
25
|
+
"lodash": "^4.18.1",
|
|
26
|
+
"luxon": "^3.7.1",
|
|
27
|
+
"morgan": "^1.10.1",
|
|
28
|
+
"nodemailer": "^8.0.5",
|
|
29
|
+
"object-sizeof": "^2.6.5",
|
|
30
|
+
"uuid": "^10.0.0",
|
|
31
|
+
"winston": "^3.17.0",
|
|
32
|
+
"winston-transport": "^4.9.0"
|
|
33
|
+
}
|
|
34
|
+
}
|