Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
296 lines (267 loc) · 8.5 KB
/
Copy pathutils.js
File metadata and controls
296 lines (267 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict';
const os = require('os');
const util = require('util');
const { performance } = require('perf_hooks');
const chalk = require('chalk');
const utility = require('utility');
const iconv = require('iconv-lite');
const circularJSON = require('circular-json-for-egg');
const { FrameworkBaseError, FrameworkErrorFormater } = require('egg-errors');
const levels = require('./level');
const hostname = os.hostname();
const durationRegexp = /([0-9]+ms)/g;
// eslint-disable-next-line no-useless-escape
const categoryRegexp = /(\[[\w\-_.:]+\])/g;
const httpMethodRegexp = /(GET|POST|PUT|PATH|HEAD|DELETE) /g;
/**
* @class LoggerUtils
*/
module.exports = {
normalizeLevel(level) {
if (typeof level === 'number') {
return level;
}
// 'WARN' => level.warn
if (typeof level === 'string' && level) {
return levels[level.toUpperCase()];
}
},
defaultContextPaddingMessage(ctx) {
// Auto record necessary request context infomation, e.g.: user id, request spend time
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
const userId = ctx.userId || '-';
const traceId = ctx.tracer?.traceId || '-';
let use = 0;
if (ctx.performanceStarttime) {
use = Math.floor((performance.now() - ctx.performanceStarttime) * 1000) / 1000;
} else if (ctx.starttime) {
use = Date.now() - ctx.starttime;
}
return '[' +
userId + '/' +
ctx.ip + '/' +
traceId + '/' +
use + 'ms ' +
ctx.method + ' ' +
ctx.url +
']';
},
// format with ctx and ctx.tracer
// Auto record necessary request context infomation, e.g.: user id, request spend time
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
defaultFormatter(meta) {
let paddingMessage = ' ';
// try to use the exists paddingMessage first
if (meta.paddingMessage) {
paddingMessage = ` ${meta.paddingMessage} `;
} else {
// gen the default ctx paddingMessage
const ctx = meta.ctx;
if (ctx) {
paddingMessage = ` ${module.exports.defaultContextPaddingMessage(ctx)} `;
}
}
return meta.date + ' ' + meta.level + ' ' + meta.pid + paddingMessage + meta.message;
},
// output to Terminal format
consoleFormatter(meta) {
let paddingMessage = ' ';
// try to use the exists paddingMessage first
if (meta.paddingMessage) {
paddingMessage = ` ${meta.paddingMessage} `;
}
let msg = meta.date + ' ' + meta.level + ' ' + meta.pid + paddingMessage + meta.message;
if (!chalk.supportsColor) {
return msg;
}
if (meta.level === 'ERROR') {
return chalk.red(msg);
} else if (meta.level === 'WARN') {
return chalk.yellow(msg);
}
msg = msg.replace(durationRegexp, chalk.green('$1'));
msg = msg.replace(categoryRegexp, chalk.blue('$1'));
msg = msg.replace(httpMethodRegexp, chalk.cyan('$1 '));
return msg;
},
/**
* Get final formated log string buffer
*
* Invoke link: {@Link Logger#log} -> {@link Transport#log} -> LoggerUtils.format
* @function LoggerUtils#format
* @param {String} level - log level
* @param {Array} args - format arguments
* @param {Object} meta - loging behaviour meta infomation
* - {String} level
* - {Boolean} raw
* - {Function} formatter
* - {Error} error
* - {String} message
* - {Number} pid
* - {String} hostname
* - {String} date
* @param {Object} options - {@link Transport}'s options
* - {String} encoding
* - {Boolean} json
* - {Boolean} dateISOFormat
* - {Function} [formatter]
* - {Function} [contextFormatter]
* - {Function} [paddingMessageFormatter]
* - {number} [maxCauseChainLength]
* @return {String|Buffer} formatted log string buffer
*/
format(level, args, meta, options) {
meta = meta || {};
let message;
let output;
let formatter = meta.formatter || options.formatter;
if (meta.ctx) {
if (options.contextFormatter) {
formatter = options.contextFormatter;
if (!meta.paddingMessage) {
// should add paddingMessage to meta on custom contextFormatter
meta.paddingMessage = options.paddingMessageFormatter ?
options.paddingMessageFormatter(meta.ctx) : module.exports.defaultContextPaddingMessage(meta.ctx);
}
} else if (options.paddingMessageFormatter && !meta.paddingMessage) {
// use custom paddingMessageFormatter to format paddingMessage
meta.paddingMessage = options.paddingMessageFormatter(meta.ctx);
}
}
if (args[0] instanceof Error) {
message = formatError(args[0], options);
} else {
message = util.format.apply(util, args);
}
if (meta.raw === true) {
output = message;
} else if (options.json === true || formatter) {
meta.level = level;
meta.date = options.dateISOFormat ? new Date().toISOString() : utility.logDate(',');
meta.pid = process.pid;
meta.hostname = hostname;
meta.message = message;
if (options.json === true) {
const outputMeta = { ...meta };
outputMeta.ctx = undefined;
output = JSON.stringify(outputMeta);
} else {
output = formatter(meta);
}
} else {
output = message;
}
if (!output) return Buffer.from('');
output += options.eol;
// convert string to buffer when encoding is not utf8
return options.encoding === 'utf8' ? output : iconv.encode(output, options.encoding);
},
// Like `Object.assign`, but don't copy `undefined` value
assign(target) {
if (!target) {
return {};
}
const sources = Array.prototype.slice.call(arguments, 1);
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
if (!source) continue;
const keys = Object.keys(source);
for (let j = 0; j < keys.length; j++) {
const key = keys[j];
if (source[key] !== undefined && source[key] !== null) {
target[key] = source[key];
}
}
}
return target;
},
formatError,
};
function errorToString(err, options, causeLength) {
options = options || {};
const maxCauseChainLength = options.maxCauseChainLength || 10;
causeLength = causeLength || 0;
if (causeLength > maxCauseChainLength) {
return 'too long cause chain';
}
let errName = err.name || 'no_name';
if (err.name === 'Error' && typeof err.code === 'string') {
errName = err.code + errName;
}
let errMessage = err.message || 'no_message';
if (err.host) {
errMessage += ` (${err.host})`;
}
// name and stack could not be change on node 0.11+
const errStack = err.stack || 'no_stack';
const errProperties = Object.keys(err).map(key => inspect(key, err[key])).join('\n');
let errorString = util.format('nodejs.%s: %s\n%s\n%s',
errName,
errMessage,
errStack.substring(errStack.indexOf('\n') + 1),
errProperties
);
if (err.name === 'AggregateError' && err.errors) {
for (let i = 0; i < err.errors.length; i++) {
const subError = err.errors[i];
const subErrorMsg = errorToString(subError, options, causeLength + 1);
errorString = util.format('%s\n[error-%d]:\n\n%s',
errorString,
i,
subErrorMsg
);
}
}
if (err.cause) {
const causeMsg = errorToString(err.cause, options, causeLength + 1);
errorString = util.format('%s\ncause:\n\n%s',
errorString,
causeMsg
);
}
return errorString;
}
function formatError(err, options, causeLength) {
if (FrameworkBaseError.isFrameworkError(err)) {
return FrameworkErrorFormater.format(err);
}
const msg = errorToString(err, options, causeLength);
return util.format('%s\npid: %s\nhostname: %s\n',
msg,
process.pid,
hostname
);
}
function inspect(key, value) {
return `${key}: ${formatObject(value)}`;
}
function formatString(str) {
if (str.length > 10000) {
return `${str.substr(0, 10000)}...(${str.length})`;
}
return str;
}
function formatBuffer(buf) {
const tail = buf.data.length > 50 ? ` ...(${buf.data.length}) ` : '';
const bufStr = buf.data.slice(0, 50).map(i => {
i = i.toString(16);
if (i.length === 1) i = `0${i}`;
return i;
}).join(' ');
return `<Buffer ${bufStr}${tail}>`;
}
function formatObject(obj) {
try {
return circularJSON.stringify(obj, (key, v) => {
if (typeof v === 'string') return formatString(v);
if (v && v.type === 'Buffer' && Array.isArray(v.data)) {
return formatBuffer(v);
}
if (v instanceof RegExp) return util.inspect(v);
return v;
});
} catch (_) {
/* istanbul ignore next */
return String(obj);
}
}