winston-like-debug
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

winston-like-debug

Format logs with Winston like you do with Debug

sample

const log = require('./logger')('my.controller')

log.info('hi', { from: 'foo' })
log.verbose({ some: 'test' })
log.warn('Invalid userid')
log.silly('Hi %s', 'Bob', { greetings: true }, { when: Date.now() })
log.error('Error reading file')
log.debug('config', { my: 'var' })

Install

yarn add winston-like-debug
npm install winston-like-debug

Usage

logger.js

const winston = require('winston')
const { likeDebug, withNamespace } = require('winston-like-debug')

const logger = winston.createLogger({
  // This is required to be at top level format
  // See https://github.com/winstonjs/winston/issues/1430
  format: winston.format.splat(),
})

logger.add(
  new winston.transports.Console({
    level: 'silly',
    format: likeDebug(),
  })
)

logger.add(
  new winston.transports.File({
    level: 'debug',
    filename: __dirname + '/output.log',
    format: likeDebug({ colors: false }),
  })
)

module.exports = withNamespace(logger)

Using namespaced logger

index.js

const log = require('./logger')('root')

log.verbose('listening', { host: 'localhost' }, { port: 3000 })
Console output:
V root listening { host: 'localhost', port: 3000 } +0ms
output.log file content:
2010-01-30T10:04:43.824Z VERBOSE root listening {"host":"localhost","port":3000}

Namespace based on filename

auth.controller.js

const log = require('./logger')(module)

log.warn('login', { userId: 98356190 })
Console output:
W auth.controller login { userId: 98356190 } +0ms
output.log file content:
2010-01-30T10:08:03.386Z WARN auth.controller login {"userId":98356190}

Package Sidebar

Install

npm i winston-like-debug

Weekly Downloads

2,396

Version

1.3.1

License

ISC

Unpacked Size

7.28 kB

Total Files

5

Last publish

Collaborators

  • edus44