Winston: typscript error logger.stream write function

Created on 30 Jun 2018  ·  4Comments  ·  Source: winstonjs/winston

Please tell us about your environment:

  • _winston version?_

    • [ ] winston@2

    • [X] winston@3

  • _node -v outputs:_ v9.8.0
  • _Operating System?_ MacOS
  • _Language?_ typescript: Version 2.9.1

What is the problem?

I have a typescript error when trying to create a write function in the logger.stream object.

error TS2322: Type '{ write(message: any, encoding: any): void; }' is not assignable to type '(options?: any) => ReadableStream'.
  Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.

39             write(message: any, encoding: any) {
               ~~~~~

What do you expect to happen instead?

No error

Other

const options = {
            file: {
                level: "info",
                filename: `${appRoot}/logs/app.log`,
                handleExceptions: true,
                json: true,
                maxsize: 5242880, // 5MB
                maxFiles: 5,
                colorize: false,
            },
            console: {
                level: "debug",
                handleExceptions: true,
                json: false,
                colorize: true,
            },
        };

        this.logger = winston.createLogger({
            transports: [
                new winston.transports.File(options.file),
                new winston.transports.Console(options.console),
            ],
            exitOnError: false, // do not exit on handled exceptions
        });

        // create a stream object with a 'write' function that will be used by `morgan`
        this.logger.stream = {

            write(message: any, encoding: any) {
                // use the 'info' log level so the output will be picked up by both transports (file and console)
                this.logger.info(message);
            },
        };

I am following this tutorial:

https://www.digitalocean.com/community/tutorials/how-to-use-winston-to-log-node-js-applications

bug

Most helpful comment

This is an issue because you're adding stream directly into your logger try this instead:

// At the bottom of your winston config add

export const stream = {
  write: (message) => {
    logger.info(message);
  },
};

Then change your logging in express like this:

import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';

app.use(morgan('combined', { stream }));

Because of the error you posted I'm assuming that you're using typescript, hence my example is written in typescript. One small gotcha, you may be tempted to write your stream like this:

export const stream = {
  write: logger.info,
};

This however will throw an exception as the second argument of the stream function is not what your logger info function is expecting. Let me know if this helps.

All 4 comments

have same error

At first glance it looks like the issue is in the type definitions: stream(options?: any): NodeJS.ReadableStream; It needs to be duplex I suspect.

Edit: Actually writeable may do it as well. I'll see if I can get it to work.

@DABH could you take a look at this?

This is an issue because you're adding stream directly into your logger try this instead:

// At the bottom of your winston config add

export const stream = {
  write: (message) => {
    logger.info(message);
  },
};

Then change your logging in express like this:

import { stream } from 'config/winston.ts';
import * as morgan from 'morgan';

app.use(morgan('combined', { stream }));

Because of the error you posted I'm assuming that you're using typescript, hence my example is written in typescript. One small gotcha, you may be tempted to write your stream like this:

export const stream = {
  write: logger.info,
};

This however will throw an exception as the second argument of the stream function is not what your logger info function is expecting. Let me know if this helps.

Was this page helpful?
0 / 5 - 0 ratings