-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathlogger.js
33 lines (30 loc) · 1 KB
/
logger.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
import {
LOG_LEVEL_ERROR,
LOG_LEVEL_WARN,
LOG_LEVEL_INFO,
LOG_LEVEL_DEBUG
} from './common'
let SESSIONCOUNTER = 0
export default function createDefaultLogger (username, hostname) {
const session = ++SESSIONCOUNTER
const log = (level, messages) => {
messages = messages.map(msg => typeof msg === 'function' ? msg() : msg)
const date = new Date().toISOString()
const logMessage = `[${date}][${session}][${username}][${hostname}] ${messages.join(' ')}`
if (level === LOG_LEVEL_DEBUG) {
console.log('[DEBUG]' + logMessage)
} else if (level === LOG_LEVEL_INFO) {
console.info('[INFO]' + logMessage)
} else if (level === LOG_LEVEL_WARN) {
console.warn('[WARN]' + logMessage)
} else if (level === LOG_LEVEL_ERROR) {
console.error('[ERROR]' + logMessage)
}
}
return {
debug: msgs => log(LOG_LEVEL_DEBUG, msgs),
info: msgs => log(LOG_LEVEL_INFO, msgs),
warn: msgs => log(LOG_LEVEL_WARN, msgs),
error: msgs => log(LOG_LEVEL_ERROR, msgs)
}
}