bunyan-rotating-file-stream icon indicating copy to clipboard operation
bunyan-rotating-file-stream copied to clipboard

Using Typescript: Type 'RotatingFileStream' is not assignable to type 'WritableStream | WriteFn | undefined'.ts(2322)

Open saki82 opened this issue 2 years ago • 3 comments

I am using typescript in my project, and implemented the exact example:

import * as bunyan from "bunyan";
import RotatingFileStream from "bunyan-rotating-file-stream";

let log = bunyan.createLogger({
    name: "foo",
    streams: [{
        stream: new RotatingFileStream({
            path: 'c:/NodeJs/Applications/api-gateway/access.log',
            period: '1d',          // daily rotation
            totalFiles: 30,        // keep up to 10 back copies
            rotateExisting: false,  // Give ourselves a clean file when we start up, based on period
            threshold: '10m',      // Rotate log files larger than 10 megabytes
            totalSize: '20m',      // Don't keep more than 20mb of archived log files
            gzip: false             // Compress the archive log files to save space
        })
    }]
});

Unfortunately this gives me a type error on the line stream: new RotatingFileStream: image

I may be overlooking something, as i am new to TS and JS in general. Can you please give me a hint in the right direction?

Thanks Sakis

saki82 avatar Aug 14 '22 13:08 saki82

Edit: Fixed it by converting to NodeJs.WritableStream, but i don't know if this is the intended way for usage or if theres a "correct" way:

let log = bunyan.createLogger({
    name: "API-Gateway",
    serializers: {
        req: reqSerializer,
        res: resSerializer,
        body: httpBodySerializer,
        err: errSerializer,
    },
    streams: [
        {
            stream: new RotatingFileStream({
                path: 'c:/NodeJs/Applications/api-gateway/log/access.log',
                period: '1d',          // daily rotation
                totalFiles: 30,        // keep up to 10 back copies
                rotateExisting: false,  // Give ourselves a clean file when we start up, based on period
                threshold: '10m',      // Rotate log files larger than 10 megabytes
                totalSize: '20m',      // Don't keep more than 20mb of archived log files
                gzip: false             // Compress the archive log files to save space
            }) as NodeJS.WriteStream
        }
    ]
});

saki82 avatar Aug 14 '22 16:08 saki82

I've actually not used this with typescript. The solution looks like something reasonable from your side, but is definitely a hack, the library needs to change. Is it the TS definitions are wrong?

Currently the class is declared as: image

Should that be derived from something else?

Rcomian avatar Aug 15 '22 07:08 Rcomian

Yes, that may be the reason because the bunyan stream type doesn't know about the type "RotatingFileStream". I guess it may be correct if the class is deriving from one of the three accepted types, but like i said i am new to JS and TS so i am not sure about this.

saki82 avatar Aug 15 '22 17:08 saki82