-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-redis-logger.js
More file actions
92 lines (83 loc) · 3.01 KB
/
simple-redis-logger.js
File metadata and controls
92 lines (83 loc) · 3.01 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
let redis = require('redis'),
log4js = require('log4js'),
{ EventEmitter } = require('events')
class SimpleRedisLogger extends EventEmitter{
constructor (options) {
super()
this._logger = log4js.getLogger('SimpleRedisLogger')
// options
this.options = options || {}
this.options.connectOptions = this.options.connectOptions || {}
this.serviceName = options.serviceName || 'simple-logger'
// create redis client instance
if (options.redis instanceof redis.RedisClient) {
this.client = options.redis
} else {
this.client = redis.createClient(this.options.connectOptions);
this.client.on('error', function (error) {
this._logger.error('Redis Error: ', error);
});
}
}
log (type) {
return (content) => {
let formattedContent = this._formatContent(content),
packedContent = this._packContent(formattedContent)
this.client.lpush(`${this.serviceName}:${type}`, packedContent, (err, result) => {
if (err) {
this._logger.error(err)
if (this.options.throwErrors) {
throw err
}
this.emit('error', err)
}
if (this.options.limitAmount && result > this.options.limitAmount) {
this.client.ltrim(`${this.serviceName}:${type}`, 0, this.options.limitAmount - 1, (error, result) => {
if (error) {
this._logger.error(err)
if (this.options.throwErrors) {
throw err
}
this.emit('error', err)
}
})
}
})
}
}
getLogs (type, from = 0, end = -1) {
return new Promise((resolve, reject) => {
this.client.lrange(`${this.serviceName}:${type}`, from, end, (error, result) => {
if (error) {
this._logger.error(error)
return reject(error)
}
return resolve(result.map((item) => {
try {
let content = JSON.parse(item)
content.time = new Date(content.timestamp * 1000)
return content
} catch (e) {
return item
}
}))
})
});
}
_formatContent (content) {
if (content instanceof Error) {
return content.message
}
if (typeof content === 'object') {
return JSON.stringify(content)
}
return content
}
_packContent (content) {
return JSON.stringify({
content: content,
timestamp: Math.round(Date.now() / 1000)
})
}
}
module.exports = SimpleRedisLogger