-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (36 loc) · 1.23 KB
/
index.js
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
import EventEmitter from 'events'
import morgan from 'koa-morgan'
import streamLogStats from 'stream-log-stats'
class Log extends EventEmitter {
description () {
return 'Outputs an access log or stats view to the console.'
}
optionDefinitions () {
return [{
name: 'log.format',
alias: 'f',
type: String,
description: "Possible values: 'stats', 'logstalgia' or anything defined by https://github.com/expressjs/morgan, typically 'dev', 'combined', 'short', 'tiny' or a custom format (e.g. ':method -> :url')"
}]
}
middleware (config) {
let format = config.logFormat
if (format) {
this.emit('verbose', 'middleware.log.config', { logFormat: config.logFormat })
let stream
if (format === 'stats') {
stream = streamLogStats({ refreshRate: 500 })
format = 'combined'
/* logstalgia-specific output */
} else if (format === 'logstalgia') {
morgan.token('date', () => {
var d = new Date()
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
})
format = 'combined'
}
return morgan(format, { stream })
}
}
}
export default Log