Skip to content

Commit

Permalink
feat!: Change to use @logdna/logger as the client
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

This changes the client to be the newer logdna node client.
Other changes were to add tests and map the log levels
between winston and logdna since they differ.  Also, the
`index_meta` property was changed to `indexMeta` as required
by the logdna logger client.

Semver: major
Ref: LOG-7378
  • Loading branch information
darinspivey committed Oct 8, 2020
1 parent 57d2bc5 commit 1bba9a2
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 31 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
package-lock.json
**/*.zip
node_modules
coverage
.nyc_output
.tap-output
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.log
npm-debug.log*
coverage
.nyc_output
*.tgz
.env
*.swp
*.vim
.npm
.tap-output
Jenkinsfile
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

## Install

```javascript
```sh
$ npm install --save logdna-winston
```

## API

Please see the [logdna](https://github.com/logdna/nodejs/) npm module for the API.
Please see [@logdna/logger](https://www.npmjs.com/package/@logdna/logger#createloggerkey-options) for
instantiation options to passthrough to LogDNA's logger client.

## Winston Transport

Expand All @@ -39,8 +40,8 @@ const options = {
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
};
indexMeta: true // Defaults to false, when true ensures meta object will be searchable
}

// Only add this line in order to track exceptions
options.handleExceptions = true;
Expand All @@ -52,17 +53,25 @@ logger.add(new logdnaWinston(options));
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"
, 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'
});
})

// log without meta
logger.info('Info: Log from LogDNA-winston');

// A payload without 'message' will log the stringified object as the message
logger.info({
key: 'value'
, text: 'This is some text to get logged'
, bool: true
})
```

## License

MIT © [LogDNA](https://logdna.com/)
Copyright © [LogDNA](https://logdna.com), released under an MIT license.
See the [LICENSE](./LICENSE) file and https://opensource.org/licenses/MIT

*Happy Logging!*
42 changes: 25 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,53 @@

const pkg = require('./package.json')
const Transport = require('winston-transport')
const Logger = require('logdna').Logger

const DEFAULT_LEVEL = 'debug'
const DEFAULT_NAME = 'LogDNA'

const {createLogger} = require('@logdna/logger')

// Convert between Winston levels and @logdna/logger levels
const levelTranslate = new Map([
['error', 'error']
, ['warn', 'warn']
, ['info', 'info']
, ['http', 'debug']
, ['verbose', 'debug']
, ['silly', 'trace']
])
/*
* Support for Winston Transport
*/
module.exports = class LogDNATransport extends Transport {
constructor(options) {
super(options)
this.name = options.name || DEFAULT_NAME
this.level = options.level || DEFAULT_LEVEL
this.index_meta = options.index_meta || false
this.logger = new Logger(options.key, {

// Create an instance of @logdna/logger
this.logger = createLogger(options.key, {
...options
, UserAgent: `${pkg.name}/${pkg.version}`
})
}

log(info, callback) {
info = info || {}

if (info.error instanceof Error) {
info.error = info.error.stack || info.error.toString()
}

if (!info.message) {
info.message = JSON.stringify(info, null, 2, function() { return undefined })
// Send the incoming object payload as the message
const level = levelTranslate.get(info.level)
this.logger.log(info, level)
callback(null, true)
return
}

const {level, message, index_meta, ...meta} = info
const {level, message, indexMeta, timestamp, ...meta} = info
const opts = {
level: level
, index_meta: typeof info.index_meta === 'boolean' ? index_meta : this.index_meta
, context: meta || {}
level
, indexMeta
, timestamp
, meta
}

this.logger.log(message, opts)
if (callback) { callback(null, true) }
callback(null, true)
}
}
34 changes: 31 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "LogDNA's Node.js logging module with support for Winston",
"main": "index.js",
"scripts": {
"lint": "eslint ."
"lint": "eslint .",
"pretest": "npm run lint",
"test": "tap"
},
"repository": {
"type": "git",
Expand All @@ -30,12 +32,15 @@
},
"homepage": "https://github.com/logdna/logdna-winston#readme",
"dependencies": {
"logdna": "^3.5.0"
"@logdna/logger": "^1.3.2",
"winston-transport": "^4.4.0"
},
"devDependencies": {
"eslint": "^7.10.0",
"eslint-config-logdna": "^2.0.0",
"winston-transport": "^4.4.0"
"nock": "^13.0.4",
"tap": "^14.10.8",
"winston": "^3.3.3"
},
"eslintConfig": {
"extends": [
Expand All @@ -49,5 +54,28 @@
"parserOptions": {
"ecmaVersion": 2019
}
},
"tap": {
"100": true,
"esm": false,
"ts": false,
"jsx": false,
"check-coverage": true,
"coverage-report": [
"text",
"text-summary",
"json",
"html"
],
"reporter": "tap",
"nyc-arg": [
"--exclude=test/",
"--exclude=tools",
"--all"
],
"files": [
"test/**/*.js"
],
"output-file": ".tap-output"
}
}
Loading

0 comments on commit 1bba9a2

Please sign in to comment.