How are you supposed to create Winston logger stream for Morgan in TypeScript
stream
is expected to be factory function that returns a a stream, not a stream itself.
A stream is expected to be a real readable stream, not an object that mimics it.
Since it is supposed to be writable as well, it should be a duplex:
logger.stream = (options?: any) => new stream.Duplex({
write: function (message: string, encoding: any) {
logger.info(message);
}
});
This is a solution that is suggested by Winston TS types. I cannot confirm if it works correctly.
Ultimately I ended up with this as a solution. I created a class with one method called write
export class LoggerStream {
write(message: string) {
logger.info(message.substring(0, message.lastIndexOf('\n')));
}
}
then when adding to express I created an instance of the class:
app.use(morgan('combined', { stream: new LoggerStream() }));
This works well for my situation
Thanks to @estus who got me past where I was hung up. Here is the solution I ended up using:
import { Logger, transports } from 'winston';
import stream from 'stream';
import split from 'split';
// http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
// https://www.loggly.com/ultimate-guide/node-logging-basics/
const logger = new Logger({
transports: [
new (transports.Console)({
level: process.env.NODE_ENV === 'production' ? 'error' : 'debug',
handleExceptions: true,
json: false,
colorize: true
}),
new (transports.File)({
filename: 'debug.log', level: 'info',
handleExceptions: true,
json: true,
colorize: false
})
],
exitOnError: false,
});
if (process.env.NODE_ENV !== 'production') {
logger.debug('Logging initialized at debug level');
}
logger.stream = split().on('data', function (message: string) {
logger.info(message);
});
export default logger;
Ultimately this issue got me to the final solution - https://github.com/expressjs/morgan/issues/70