Skip to content

Commit db2e6c4

Browse files
committed
added winston compat and error handler
1 parent 24f4a1b commit db2e6c4

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

lib/index.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var util = require('util')
33
var Transport = require('winston-transport')
44
var winston = require('winston')
55

6-
var common = require('winston/lib/winston/common')
6+
// var common = require('winston/lib/winston/common')
7+
const winstonCompat = require('winston-compat')
78
var _dirname = require('path').dirname
89
var LogseneJS = require('logsene-js')
910
var flat = require('flat')
@@ -25,7 +26,7 @@ var Logsene = function (options) {
2526
this.setSource = false
2627
}
2728
this.logsene_debug = options.logsene_debug
28-
this.flatOptions = options.flatOptions || {safe: true, delimiter: '_'}
29+
this.flatOptions = options.flatOptions || { safe: true, delimiter: '_' }
2930
this.json = options.json || false
3031
this.colorize = options.colorize || false
3132
this.prettyPrint = options.prettyPrint || false
@@ -92,6 +93,21 @@ var Logsene = function (options) {
9293
})
9394
}
9495

96+
// maybe this?
97+
if (options.handleErrors) {
98+
process.on('error', function (err) {
99+
console.log(new Date().toISOString() + ' winston-logsene: unhandledError: ' + err + ' ' + err.stack)
100+
self.logger.log('error', err.stack || err.toString(), err)
101+
flushLogs(function (flushErr, exitTime) {
102+
if (options.exitOnError) {
103+
setTimeout(function () {
104+
process.exit(-1)
105+
}, exitTime)
106+
}
107+
})
108+
})
109+
}
110+
95111
process.on('unhandledRejection', function (err) {
96112
console.log(new Date().toISOString() + ' winston-logsene: unhandledRejection: ' + err + ' ' + err.stack)
97113
self.logger.log('warning', err.stack || err.toString(), err)
@@ -147,7 +163,7 @@ Logsene.prototype.log = function (meta, callback) {
147163
return callback(null, true)
148164
}
149165
if (this.setSource) {
150-
if (meta && (!meta['source'])) {
166+
if (meta && (!meta.source)) {
151167
meta.source = this.source
152168
}
153169
}
@@ -159,10 +175,10 @@ Logsene.prototype.log = function (meta, callback) {
159175
if (!msg) {
160176
msg = meta.stack || meta.message
161177
}
162-
metaData = {error_stack: meta.stack || meta.message}
178+
metaData = { error_stack: meta.stack || meta.message }
163179
}
164180
if (this.logsene_debug) {
165-
var output = common.log({
181+
var output = winstonCompat.log({
166182
colorize: this.colorize,
167183
json: this.json,
168184
level: level,

0 commit comments

Comments
 (0)