Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
saki82 opened this issue Aug 14, 2022 · 3 comments

Comments

@saki82
Copy link

saki82 commented Aug 14, 2022

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
Copy link
Author

saki82 commented Aug 14, 2022

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
        }
    ]
});

@Rcomian
Copy link
Owner

Rcomian commented Aug 15, 2022

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?

@saki82
Copy link
Author

saki82 commented Aug 15, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants