Winston: Could someone share an example of using memory transport and ways to listen to it?

Created on 16 Feb 2016  ·  4Comments  ·  Source: winstonjs/winston

When I use Console transport, I know that I can add a listener like this:
logger.on('logging', (transport, level, msg, meta) => use the logged data....

But how can I log to memory and listen to it?
I'm basically looking for a solution to listen to logs without having them written in the console/file/sent over HTTP.

Cheers.

2.x only

Most helpful comment

@lonix1 @indexzero something like this should work:

import { Writable } from 'stream';

let output = ''
const stream = new Writable()
stream._write = (chunk, encoding, next) => {
     output = output += chunk.toString()
     next()
}

const streamTransport = new winston.transports.Stream({ stream })
const logger = winston.createLogger({ transports: [streamTransport]})
logger.info('test message')
const logEvents = output.trim().split('\n')
assert(logEvents[0].includes('test message'))

All 4 comments

For winston@3 users: winston.transports.Memory has been removed – use winston.transports.Stream instead with any buffered stream implementation.

Historically from a winston < 3 perspective winston.transports.Memory was used for unit testing purposes. It was removed in winston@3 since streams are capable of handling back pressure. Please consider upgrading & thanks for using winston!

Hi the original question is still valid though - is there an example?

We also want to do this, to be able to perform logging to memory during testing.

@lonix1 @indexzero something like this should work:

import { Writable } from 'stream';

let output = ''
const stream = new Writable()
stream._write = (chunk, encoding, next) => {
     output = output += chunk.toString()
     next()
}

const streamTransport = new winston.transports.Stream({ stream })
const logger = winston.createLogger({ transports: [streamTransport]})
logger.info('test message')
const logEvents = output.trim().split('\n')
assert(logEvents[0].includes('test message'))

I don't suppose this could work with IE11, could it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tagyoureit picture tagyoureit  ·  4Comments

alditis picture alditis  ·  3Comments

xungwoo picture xungwoo  ·  3Comments

exortech picture exortech  ·  3Comments

greenhat616 picture greenhat616  ·  3Comments