-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (97 loc) · 2.42 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
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
const debug = require('debug')('apex-logs-winston')
const Transport = require('winston-transport')
const { Client } = require('apex-logs')
const Buffer = require('./buffer')
// levels is a map of valid Winston to Apex Logs levels.
const levels = {
warn: 'warning',
verbose: 'debug',
silly: 'debug',
emerg: 'emergency',
crit: 'critical',
help: 'info',
data: 'info',
prompt: 'info',
http: 'info',
input: 'info'
}
/**
* ApexLogsTransport is the Apex Logs transport.
*/
exports = module.exports = class ApexLogsTransport extends Transport {
/**
* Initialize with the given config:
*
* - `url`: Apex Logs instance endpoint
* - `authToken`: API auth token
* - `projectId`: Project id
* - `buffer`: Options for buffering
* - `maxEntries`: The maximum number of entries before flushing (defaults to 250)
* - `flushInterval`: The flush interval in milliseconds (defaults to 15,000)
* - `json`: Enable JSON output for Heroku or AWS Lambda
*/
constructor({ url, authToken, projectId, json, buffer = {}, ...options }) {
super(options)
this.projectId = projectId
this.client = new Client({ url, authToken })
this.json = json
this.buffer = new Buffer({
onFlush: this.onFlush.bind(this),
onError: this.onError.bind(this),
...buffer
})
}
/**
* Log handler, buffer the event.
*/
async log(info, callback) {
const { level, message, ...fields } = info
// normalize level
const l = levels[level] || level
// error details
if (fields.error instanceof Error) {
fields.error = fields.error.stack || fields.error.toString()
}
// event
const e = {
timestamp: new Date,
level: l,
message,
fields
}
// json output
if (this.json) {
console.log(JSON.stringify(e))
return callback()
}
// buffer event
this.buffer.push(e)
callback()
}
/**
* End handler, close the buffer.
*/
async end(args) {
debug('closing')
await this.buffer.close()
super.end(args)
}
/**
* Handle flushing.
*/
async onFlush(events) {
await this.client.addEvents({
project_id: this.projectId,
events
})
}
/**
* Handle errors.
*/
async onError(error) {
// TODO: maybe delegate here
console.error('apex/logs-winston: error flushing logs: %s', error)
}
}
exports.JSON = class ApexLogsJSONTransport extends Transport {
}