From 9f13e6438d3da219d3a4f7d59c89b948d3a0b008 Mon Sep 17 00:00:00 2001 From: LY-Huang Date: Thu, 7 Mar 2019 13:11:39 -0800 Subject: [PATCH] update transport function to make it more consistent with winston3 --- README.md | 23 +++++++++++++---------- index.js | 38 +++++++++++++++----------------------- package.json | 2 +- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 69144a8..90aa1b5 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ const options = { mac: macAddress, app: appName, env: envName, + level: level, // Default to debug, maximum level of log, doc: https://github.com/winstonjs/winston#logging-levels index_meta: true // Defaults to false, when true ensures meta object will be searchable }; @@ -48,16 +49,18 @@ options.handleExceptions = true; logger.add(new logdnaWinston(options)); -let meta = { - data:'Some information' -}; -logger.log('info', 'Log from LogDNA-winston', meta); -logger.log('debug', 'Log from LogDNA-winston', meta); -logger.log('warn', 'Log from LogDNA-winston', meta); -logger.log('error', 'Log from LogDNA-winston', meta); -logger.info('Info: Log from LogDNA-winston', meta); -logger.warn('Warn: Log from LogDNA-winston', meta); -logger.error('Error: Log from LogDNA-winston', meta); + +// log with meta +logger.log({ + level: 'info' + , message: 'Log from LogDNA-winston' + , index_meta: true // Ignore this if you would like to use default setting + , data:'Some information' // Properties besides level, message and index_meta are considered as "meta" + , error: new Error("It's a trap.") // Transport will parse the error object under property 'error' +}); + +// log without meta +logger.info('Info: Log from LogDNA-winston'); ``` ## License diff --git a/index.js b/index.js index a1cf4f9..8f72e7b 100644 --- a/index.js +++ b/index.js @@ -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); } } }; diff --git a/package.json b/package.json index 3a6de56..be6c4bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logdna-winston", - "version": "2.0.0", + "version": "2.1.0", "description": "LogDNA's Node.js logging module with support for Winston", "main": "index.js", "scripts": {