Skip to content

Commit

Permalink
feat(levels)!: Support custom log levels in LogDNA
Browse files Browse the repository at this point in the history
As Winston allows for the use of custom log levels, now
so does LogDNA. Custom levels can be defined for both
Winston and LogDNA such that those custom levels will
show up on LogDNA's log viewer.

BREAKING CHANGE: This removes the log level "translation"
that used to be in place to convert Winston levels to ones
that would be acceptable by LogDNA. Since LogDNA can now
define custom levels as well, this translation is no longer
needed, however it may break implementations that are
relying on the translated levels.

Fixes: #31
  • Loading branch information
darinspivey committed Sep 17, 2021
1 parent 1313046 commit 02c6b3e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
46 changes: 41 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ logger.add(new logdnaWinston(options));

// log with meta
logger.log({
level: 'info'
, message: 'Log from LogDNA-winston'
, indexMeta: true // Optional. If not provided, it will use the default.
, data:'Some information' // Properties besides level, message and indexMeta are considered as "meta"
, error: new Error("It's a trap.") // Transport will parse the error object under property 'error'
level: 'info'
, message: 'Log from LogDNA-winston'
, indexMeta: true // Optional. If not provided, it will use the default.
, data:'Some information' // Properties besides level, message and indexMetaare considered as "meta"
, error: new Error("It's a trap.") // Transport will parse the error object under property 'error'
})

// log without meta
Expand All @@ -73,6 +73,42 @@ logger.info({
})
```

## Custom Log Levels

As per the Winston documentation, [custom log levels](https://github.com/winstonjs/winston#using-custom-logging-levels) may be used. In order to use such
levels in LogDNA, [custom levels must be defined](https://github.com/logdna/logger-node#custom-log-levels) for that logger as well.

```javascript
const levels = {
error: 0
, warn: 1
, info: 2
, http: 3
, verbose: 4
, loquacious: 5
, ludicrous: 6
}
const logger = winston.createLogger({
levels
, level: 'ludicrous' // needed, or else it won't log levels <= to 'info'
})

const logdna_options = {
key: 'abc123'
, levels: Object.keys(levels)
}
logger.add(new logdnaWinston(logdna_options))

// Now the custom levels can be logged in Winston and LogDNA
logger.ludicrous('Some text')

logger.log({
msg: 'Custom level log message'
, key: 'value'
, bool: true
, level: 'loquacious'
})
```

## Contributors ✨

Expand Down
12 changes: 1 addition & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ const Transport = require('winston-transport')
const {createLogger} = require('@logdna/logger')
const pkg = require('./package.json')

// Convert between Winston levels and @logdna/logger levels
const levelTranslate = new Map([
['error', 'error']
, ['warn', 'warn']
, ['info', 'info']
, ['http', 'debug']
, ['verbose', 'debug']
, ['debug', 'debug']
, ['silly', 'trace']
])
/*
* Support for Winston Transport
*/
Expand All @@ -29,7 +19,7 @@ module.exports = class LogDNATransport extends Transport {
}

log(info, callback) {
const level = levelTranslate.get(info.level)
const level = info.level

if (info.error instanceof Error) {
info.error = info.error.stack || info.error.toString()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"homepage": "https://github.com/logdna/logdna-winston#readme",
"dependencies": {
"@logdna/logger": "^2.2.4",
"@logdna/logger": "^2.4.0",
"winston-transport": "^4.4.0"
},
"devDependencies": {
Expand Down
42 changes: 27 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,40 +175,52 @@ test('Call .log() with a message object', (t) => {
logger.log(message)
})

test('Call .log() with a message payload and level to translate', (t) => {
test('Call .log() with a message payload and custom level', (t) => {
const levels = {
error: 0
, warn: 1
, info: 2
, http: 3
, verbose: 4
, loquacious: 5
, ludicrous: 6
}
const logger = winston.createLogger({
level: 'silly'
levels
, level: 'ludicrous' // needed, or else it won't log levels <= to 'info'
})
const message = {
msg: 'Log from LogDNA-winston'
, key: 'value'
, bool: true
, level: 'verbose'
}
const options = {

const logdna_options = {
key: 'abc123'
, hostname: 'My-Host'
, ip: '192.168.2.100'
, mac: '9e:a0:f8:20:86:3d'
, url: 'http://localhost:35870'
, app: 'LogDNA'
, levels: Object.keys(levels)
}

logger.add(new logdnaWinston(options))
logger.add(new logdnaWinston(logdna_options))

t.plan(2)
t.on('end', async () => {
nock.cleanAll()
})

nock(options.url)
const message = {
msg: 'Custom level log message'
, key: 'value'
, bool: true
, level: 'ludicrous'
}

nock(logdna_options.url)
.post('/', (body) => {
const payload = body.ls[0]
t.match(payload, {
timestamp: Number
, line: JSON.stringify(message)
, level: 'DEBUG'
, app: options.app
, level: 'LUDICROUS'
, app: logdna_options.app
, meta: '{}' // indexMeta is `false`, so it's stringified
}, 'Options were successfully placed into the message')
return true
Expand All @@ -225,7 +237,7 @@ test('Call .log() with a message payload and level to translate', (t) => {
})
.reply(200, 'Ingester response')

logger.log(message)
logger.ludicrous(message)
})

test('Error will still be processed if there is no stack trace', (t) => {
Expand Down

0 comments on commit 02c6b3e

Please sign in to comment.