Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom rewriter for winston-logsene #6

Merged
merged 1 commit into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ logger.add (logsene, {token: process.env.LOGSENE_TOKEN, type: 'test_logs'})
- __handleExceptions__ - boolean 'true' logs 'uncaught exceptions'
- __exitOnError__ - if set to 'false' process will not exit after logging the 'uncaught exceptions'
- __source__ - name of the logging source, by default name of the main node.js module
- __rewriter__ - similar to rewriters in winston, rewriter allows modifying of __log meta__ but only for the logsene
transport. This is a simple function which takes `level, msg, meta` as parameter and returns the new __meta__ array


### Examples
Expand All @@ -38,6 +40,16 @@ logger.error ("Error message no. %d logged to %s",1,'Logsene', {metadata: "test-
logger.warn ("Warning message no. %d logged to %s",1,'Logsene', {metadata: "test-warning", count:1, tags: ['test', 'warning', 'winston']})
logger.debug ("Debug message no. %d logged to %s",1,'Logsene', {metadata: "test-debug", count:1})

// use custom rewriter
var serverIp = "10.0.0.12";
logger.add (logsene, {
token: process.env.LOGSENE_TOKEN,
rewriter: function (level, msg, meta) {
meta.ip = serverIp;
return meta;
}
})

```

### Schema / Mapping definition for Meta-Data
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Logsene = function (options) {
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false
this.label = options.label || null
this.source = options.source || (process.mainModule ? null : _dirname(process.mainModule.filename)) || module.filename
this.rewriter = options.rewriter || null
if (this.json) {
this.stringify = options.stringify || function (obj) {
return JSON.stringify(obj, null, 2)
Expand Down Expand Up @@ -62,6 +63,9 @@ Logsene.prototype.log = function (level, msg, meta, callback) {
if (meta && (!meta['source'])) {
meta.source = this.source
}
if (this.rewriter){
meta = this.rewriter(level, msg, meta)
}
var output = common.log({
colorize: this.colorize,
json: this.json,
Expand Down