-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update transport function to make it more consistent with winston3
- Loading branch information
Showing
3 changed files
with
29 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,38 @@ | ||
const Transport = require('winston-transport'); | ||
const logdna = require('logdna'); | ||
const Logger = require('logdna').Logger; | ||
const stringify = require('json-stringify-safe'); | ||
|
||
const DEFAULT_LEVEL = 'debug'; | ||
const DEFAULT_NAME = 'LogDNA'; | ||
/* | ||
* Support for Winston Transport | ||
*/ | ||
module.exports = class LogDNATransport extends Transport { | ||
constructor(options) { | ||
super(options); | ||
this.name = options.name || 'LogDNA'; | ||
this.level = options.level || ''; | ||
this.logger = new logdna.Logger(options.key, options); | ||
this.name = options.name || DEFAULT_NAME; | ||
this.level = options.level || DEFAULT_LEVEL; | ||
this.logger = new Logger(options.key, options); | ||
this.index_meta = options.index_meta || false; | ||
} | ||
|
||
log(info, callback) { | ||
setImmediate(() => this.emit('logged', info)); | ||
info = info || {}; | ||
|
||
if (info.message instanceof Error) { | ||
info.error = info.message.stack || info.message.toString(); | ||
info.message = info.message.message; | ||
if (info.error instanceof Error) { | ||
info.error = info.error.stack || info.error.toString(); | ||
} | ||
|
||
if (!info.message) { | ||
info.message = stringify(info, null, 2, function () { return undefined; }); | ||
info.message = stringify(info, null, 2, function() { return undefined; }); | ||
} | ||
|
||
const { level, message, index_meta, ...meta } = info; | ||
|
||
const opts = { | ||
level | ||
, index_meta: index_meta !== undefined ? index_meta : this.index_meta | ||
, meta: meta || {} | ||
level: level | ||
, index_meta: typeof info.index_meta === 'boolean' ? index_meta : this.index_meta | ||
, context: meta || {} | ||
}; | ||
|
||
this.logger.log(info.message, opts); | ||
if (callback) { callback(); } | ||
} | ||
|
||
// make sure all logs are flushed when the Stream closes | ||
// https://nodejs.org/api/stream.html#stream_writable_final_callback | ||
_final(cb) { | ||
logdna.flushAll(cb); | ||
this.logger.log(message, opts); | ||
if (callback) { callback(null, true); } | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9f13e64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LYHuang it's a good practice to have Winston Transports emit the
logged
event as it's shown the example transportIt's also good to have an implementation for
_final
to make sure LogDNA events are flushed whenlogger.end()
is called. It will close and wait for all registered transports. Without it I ended up losing some entries because the script finishes before all messages are sent to LogDNA.