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.
For winston@3
users: winston.transports.Memory
has been removed 鈥撀爑se 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?
Most helpful comment
@lonix1 @indexzero something like this should work: