Skip to content

Commit

Permalink
update transport function to make it more consistent with winston3
Browse files Browse the repository at this point in the history
  • Loading branch information
LYHuang committed Mar 7, 2019
1 parent 4a94080 commit 9f13e64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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
Expand Down
38 changes: 15 additions & 23 deletions index.js
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); }
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

1 comment on commit 9f13e64

@lgfa29
Copy link

@lgfa29 lgfa29 commented on 9f13e64 Mar 14, 2019

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 transport

It's also good to have an implementation for _final to make sure LogDNA events are flushed when logger.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.

Please sign in to comment.