forked from rauchg/slackin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.js
50 lines (40 loc) · 1014 Bytes
/
log.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
44
45
46
47
48
49
50
import dbg from 'debug'
const debug = dbg('slackin')
export default function log (slack, silent){
// keep track of elapsed time
let last
out('fetching')
// attach events
slack.on('ready', () => out('ready'))
slack.on('retry', () => out('retrying'))
slack.on('fetch', () => {
last = new Date
out('fetching')
})
slack.on('data', online)
// log online users
function online (){
out('online %d, total %d %s',
slack.users.active,
slack.users.total,
last ? `(+${new Date - last}ms)` : '')
}
// print out errors and warnings
if (!silent) {
slack.on('error', (err) => {
console.error('%s – ' + err.stack, new Date)
})
slack.on('ready', () => {
if (!slack.org.logo && !silent) {
console.warn('\u001b[92mWARN: no logo configured\u001b[39m')
}
})
}
function out (...args){
if (args) {
args[0] = `${new Date} – ${args[0]}`
}
if (silent) return debug(...args)
console.log(...args)
}
}