包详细信息

logdna-winston

logdna14.2kSEE LICENSE IN LICENSE4.0.1

LogDNA's Node.js logging module with support for Winston

logdna, logs, logging, winston

自述文件

Node.js Winston library for logging to LogDNA


All Contributors

Install

$ npm install --save logdna-winston

API

Please see @logdna/logger for instantiation options to passthrough to LogDNA's logger client.

Winston Transport

This module also provides a transport object, which can be added to winston using:

const logdnaWinston = require('logdna-winston');
const winston = require('winston');
const logger = winston.createLogger({});
const options = {
    key: apikey,
    hostname: myHostname,
    ip: ipAddress,
    mac: macAddress,
    app: appName,
    env: envName,
    level: level, // Default to debug, maximum level of log, doc: https://github.com/winstonjs/winston#logging-levels
    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;

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 indexMetaare 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
})

Custom Log Levels

As per the Winston documentation, custom log levels may be used. In order to use such levels in LogDNA, custom levels must be defined for that logger as well. If levels is passed to this transport, they will automatically be configured as custom levels for the LogDNA logger.

Similarly, if no custom winston levels are used, then the Winston default of "npm" levels will be automatically configured for LogDNA to understand.

NOTE: The "levels" parameter is in the context of Winston, thus it should be an object where the keys are the level names, and the values are a numeric priority.

  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'
  }
  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'
  })

The maxLevel Parameter

Both the Winston logger and the LogDNA logger accept a level parameter, but they mean different things:

  • For Winston, level represents the maximum log level by priority. In other words, anything "higher" in priority number WILL NOT be logged.
  • In LogDNA, level represents a default log level which is used in the absence of a level being defined for each log entry. Since Winston always passed a log level, this parameter is not usable in this transport.

To disambiguate the two level parameters (even though the LogDNA one cannot be used), this custom transport will accept the maxLevel parameter to be used as Winston's level parameter. This is only needed if level has not been defined during Winston's createLogger call prior to adding this custom transport.

const winston = require('winston')
const logdnaTransport = require('logdna-winston')

const logger = winston.createLogger({
  level: 'verbose'
, transports:[new logdnaTransport({key: 'abc123'})]
})
logger.silly('This will not be logged')
logger.verbose('This will be logged')

// ...is the same as this
 const logger = winston.createLogger({
  transports:[
    new logdnaTransport({
      key: 'abc123'
    , maxLevel: 'verbose'
    })
  ]
})
logger.silly('This will not be logged')
logger.verbose('This will be logged')

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Muaz Siddiqui

💻 📖

Samir Musali

💻 📖

Darin Spivey

💻 📖

Mike Huang

💻 📖

Brian Conn

📖

Mario Casciaro

💻

vilyapilya

🔧

Mike Del Tito

💻 🔧

This project follows the all-contributors specification. Contributions of any kind welcome!

License

Copyright © LogDNA, released under an MIT license. See the LICENSE file and https://opensource.org/licenses/MIT

Happy Logging!

更新日志

Changelog

4.0.1 (2021-09-21)

Bug Fixes

  • level: Separate concerns for the "level" parameter 0f734d8 - Darin Spivey, closes: #33
  • levels: Automatically set up LogDNA custom levels for defaults e3101e9 - Darin Spivey

4.0.0 (2021-09-17)

Chores

  • deps: eslint-config-logdna@5.1.0 1313046 - Darin Spivey
  • deps: eslint@7.32.0 12f2235 - Darin Spivey
  • deps: semantic-release-config-logdna@1.3.0 500705c - Darin Spivey
  • deps: semantic-release@17.4.7 a6e4501 - Darin Spivey

Features

  • levels: Support custom log levels in LogDNA 02c6b3e - Darin Spivey, closes: #31

BREAKING CHANGES

  • levels: 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.

3.0.5 (2021-04-15)

Build System

  • replace npmignore with explicit files list 0e2058a - Mike Del Tito

Chores

  • deps: update eslint tooling 2298f36 - Mike Del Tito
  • deps: update logdna/logger@2.2.4 f891936 - Mike Del Tito

Continuous Integration

  • add semantic-release da609d7 - Mike Del Tito

Miscellaneous

  • add @darinspivey as a contributor b11ed79 - Mike Del Tito
  • add @LYHuang as a contributor b58fdac - Mike Del Tito
  • add @mariocasciaro as a contributor 03e4def - Mike Del Tito
  • add @mdeltito as a contributor 4b401ff - Mike Del Tito
  • add @respectus as a contributor 3b3dd66 - Mike Del Tito
  • add @smusali as a contributor 10b207c - Mike Del Tito
  • add @TheConnMan as a contributor 43ba8bd - Mike Del Tito
  • add @vilyapilya as a contributor 75c4348 - Mike Del Tito

2020-12-17, Version 3.0.4 (Stable)

  • [7be3610c3b] - Always translate the log level. (Mario Casciaro)

2020-12-16, Version 3.0.3 (Stable)

  • [c5123aa947] - fix(ci): Expose linter results in the junit output (Darin Spivey)

2020-10-28, Version 3.0.2 (Stable)

  • [29f225ac1d] - deps: @logdna/logger@2.0.0 (Darin Spivey)

2020-10-27, Version 3.0.1 (Stable)

2020-10-08, Version 3.0.0 (Stable)