-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (137 loc) · 3.87 KB
/
index.js
File metadata and controls
155 lines (137 loc) · 3.87 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
'use strict';
const util = require('node:util');
const onFinished = require('on-finished');
const Counter = require('passthrough-counter');
const humanize = require('humanize-number');
const bytes = require('bytes');
const chalk = require('chalk');
// color map.
const colorCodes = {
7: 'magenta',
5: 'red',
4: 'yellow',
3: 'cyan',
2: 'green',
1: 'green',
0: 'yellow'
};
// Log helper.
function log(print, ctx, start, length, err, event) {
// get the status code of the response
const status = err
? err.isBoom
? err.output.statusCode
: err.status || 500
: ctx.status || 404;
// set the color of the status code;
const s = (status / 100) | 0;
const color = colorCodes[s > 7 ? 0 : s];
// get the human readable response length
const formattedLength = [204, 205, 304].includes(status)
? ''
: length
? bytes(length).toLowerCase()
: '-';
const upstream = err
? chalk.red('xxx')
: event === 'close'
? chalk.yellow('-x-')
: chalk.gray('-->');
print(
' ' +
upstream +
' ' +
chalk.bold('%s') +
' ' +
chalk.gray('%s') +
' ' +
chalk[color]('%s') +
' ' +
chalk.gray('%s') +
' ' +
chalk.gray('%s'),
ctx.method,
ctx.originalUrl,
status,
time(start),
formattedLength
);
}
/**
* Show the response time in a human readable format.
* In milliseconds if less than 10 seconds,
* in seconds otherwise.
*/
function time(start) {
const delta = Date.now() - start;
return humanize(
delta < 10000 ? delta + 'ms' : Math.round(delta / 1000) + 's'
);
}
function koaLogger(options) {
// print to console helper.
const print = (function () {
let transporter;
if (typeof options === 'function') {
transporter = options;
} else if (options && options.transporter) {
transporter = options.transporter;
}
// eslint-disable-next-line func-names
return function printFunc(...args) {
const string = util.format(...args);
if (transporter) transporter(string, args);
else console.log(...args);
};
})();
// eslint-disable-next-line func-names
return async function logger(ctx, next) {
// request
const start = (() => {
// see: https://github.com/koajs/logger/pull/79
const injectedRequestRecivedStartTime =
ctx[Symbol.for('request-received.startTime')];
if (!injectedRequestRecivedStartTime) return Date.now();
// TOD-O: remove cabinjs/request-received on our next major release (ts)
console.warn(
'[DEPRECATION WARNING]: Support for `request-received` will be removed in the next major release.'
);
// support request-received v0.0.1
if ('getTime' in injectedRequestRecivedStartTime)
return injectedRequestRecivedStartTime.getTime();
// support request-received v0.2.0 + v0.3.0
return injectedRequestRecivedStartTime;
})();
print(
' ' +
chalk.gray('<--') +
' ' +
chalk.bold('%s') +
' ' +
chalk.gray('%s'),
ctx.method,
ctx.originalUrl
);
try {
await next();
} catch (err) {
// log uncaught downstream errors
log(print, ctx, start, null, err);
throw err;
}
// calculate the length of a streaming response
// by intercepting the stream with a counter.
// only necessary if a content-length header is currently not set.
let counter;
if (ctx.response.length === 0 && ctx.body && ctx.body.readable)
counter = ctx.body.pipe(new Counter()).on('error', ctx.onerror);
// log when the response is finished or closed,
// whichever happens first.
onFinished(ctx.res, (error) => {
const event = error ? 'close' : 'finish';
const length = counter ? counter.length : ctx.response.length;
log(print, ctx, start, length, null, event);
});
};
}
module.exports = koaLogger;