-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
caterpillar.coffee
executable file
·174 lines (140 loc) · 3.29 KB
/
caterpillar.coffee
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Import
extendr = require('extendr')
stream = require('readable-stream')
# Transform
class Transform extends stream.Transform
config: null
constructor: (opts) ->
@config = extendr.deepClone(@config)
@setConfig(opts)
super
pipe: (child) ->
if child.setConfig?(@config) then @on('config', child.setConfig)
super
setConfig: (opts) =>
extendr.deepExtend(@config, opts)
@emit('config', @config)
@
getConfig: ->
return @config
_transform: (chunk, encoding, next) =>
entry = JSON.parse(chunk.toString())
message = @format(entry)
message = JSON.stringify(message) if message
return next(null, message)
format: (entry) ->
return entry
# Logger
class Logger extends Transform
config:
lineOffset: 0
levels:
emergency: 0
alert: 1
critical: 2
error: 3
warning: 4
notice: 5
info: 6
debug: 7
emerg: 0
crit: 2
err: 3
warn: 4
note: 5
default: 6
_transform: (chunk, encoding, next) ->
return next(null, chunk)
getLevelNumber: (name) ->
return @config.levels[name] ? null
getLevelName: (number) ->
# Try to treturn the levelName
for own key,value of @config.levels
if value is number
return key
# Return
return null
getLevelInfo: (level) ->
# Prepare
result =
levelNumber: null
levelName: null
defaulted: false
# Log the entry
if typeof level is 'number'
levelNumber = level
levelName = @getLevelName(levelNumber)
else
levelName = level
levelNumber = @getLevelNumber(levelName)
levelName = @getLevelName(levelNumber)
unless levelNumber?
levelNumber = @getLevelNumber('default')
levelName = @getLevelName(levelNumber)
result.defaulted = true
# Apply
result.levelNumber = levelNumber
result.levelName = levelName
# Return
return result
getLineInfo: ->
# Prepare
result =
line: -1
method: 'unknown'
file: 'unknown'
# Retrieve
err = new Error()
lines = err.stack?.split('\n') or [] # ios devices do not have err.stack available
offset = @config.lineOffset
for line in lines
if line.indexOf(__dirname) isnt -1 or line.indexOf(' at ') is -1
continue
if offset isnt 0
--offset
continue
parts = line.split(':')
if parts[0].indexOf('(') is -1
result.method = 'unknown'
result.file = parts[0].replace(/^.+?\s+at\s+/, '')
else
result.method = parts[0].replace(/^.+?\s+at\s+/, '').replace(/\s+\(.+$/, '')
result.file = parts[0].replace(/^.+?\(/, '')
result.line = parts[1]
break
# Return
return result
format: (level, args...) ->
# Prepare
entry = {}
entry.date = new Date().toISOString()
# Prepare
levelInfo = @getLevelInfo(level)
lineInfo = @getLineInfo(level)
# Add the level to the message arguments if it was not a level
args.unshift(level) if levelInfo.defaulted and level isnt 'default'
delete levelInfo.defaulted
# Apply
entry.args = args
extendr.extend(entry, levelInfo, lineInfo)
# Return
return entry
log: (args...) =>
# Prepare
entry = @format(args...)
# Emit the entry
@emit('log', entry)
# Write the entry
entryString = JSON.stringify(entry)
@write(entryString)
# Chain
@
# Export
module.exports = {
# Classes
Transform
Logger
# Instantiators
createTransform: (args...) -> new Transform(args...)
createLogger: (args...) -> new Logger(args...)
}