-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (42 loc) · 1.27 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
44
45
46
module.exports = function Log({logsDb, accountId, repoSlug, context}) {
const log = (type, message = 'no message specified', params) => {
let date = new Date()
const shortDate = date.toISOString().replace(/[-,:]/g,'').replace('T', '-').split('.')[0]
const randomString = Math.ceil(Math.random(1)*1000000)
const repoSlugIfExists = repoSlug ? repoSlug + ':' : ''
const accountIdIfExists = accountId ? accountId + ':' : ''
const logDoc = {
_id: `${accountIdIfExists}${repoSlugIfExists}${type}:${context}:${shortDate}:${randomString}`,
accountId,
repoSlug: repoSlug ? repoSlug.toLowerCase() : '',
context,
type,
message,
params,
createdAt: date.toISOString()
}
if (process.env.GK_DEBUG) {
console.log(logDoc)
}
logsDb.put(logDoc).catch(err => { throw err })
}
return {
debug: (message, params) => {
if (process.env.GK_DEBUG) {
log('debug', message, params)
}
},
info: (message, params) => {
log('info', message, params)
},
success: (message, params) => {
log('success', message, params)
},
warn: (message, params) => {
log('warn', message, params)
},
error: (message, params) => {
log('error', message, params)
}
}
}