bunyan-rotating-file-stream
bunyan-rotating-file-stream copied to clipboard
Using Typescript: Type 'RotatingFileStream' is not assignable to type 'WritableStream | WriteFn | undefined'.ts(2322)
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
:
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
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
}
]
});
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:
Should that be derived from something else?
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.