iccicci / rotating-file-stream

Opens a stream.Writable to a file rotated by interval and/or size. A logrotate alternative.
MIT License
286 stars 39 forks source link

Error: this.timeout.unref is not a function #90

Closed viniciusteixeiradias closed 1 year ago

viniciusteixeiradias commented 1 year ago

Guys, I installed this lib in my project to help me with the log storage work, but I'm stuck in an error. I added the simplest configuration to my project and every time I see the system compile, I get the following error:

Screenshot 2023-03-09 at 7 57 21 pm

I could be doing some wrong configuration, I already looked in the doc and in other issues but I didn't find it, I hope you can help me :).

My code:

import { Logger, ILogObj } from "tslog";
import { createStream } from "rotating-file-stream";

const stream = createStream('tslog.log', {
  size: "10M", // rotate every 10 MegaBytes written
  compress: "gzip", // compress rotated files
  interval: "1d",
});

const logger: Logger<ILogObj> = new Logger({ type: "hidden", hideLogPositionForProduction: true });
logger.attachTransport((logObj) => {
  stream.write(JSON.stringify(logObj) + "\n");
});
iccicci commented 1 year ago

Hi @viniciusteixeiradias ,

this is very strange! This is the only call to unref in the source code.

Could it be, in your project, something replaces the setTimeout builtin function?

viniciusteixeiradias commented 1 year ago

@iccicci I don't think it can be in my code. I'll check the project settings, but today the "rotating-file-stream" code is just this:

I am using vue 3 and typescript

import type { InjectionKey } from 'vue'
import { Logger, ILogObj } from "tslog";
import { createStream } from "rotating-file-stream";

const stream = createStream('tslog.log', {
  size: "10M",
  compress: "gzip",
  interval: '1d',
});

const logger: Logger<ILogObj> = new Logger({ type: "hidden", hideLogPositionForProduction: true });
logger.attachTransport((logObj) => {
  stream.write(JSON.stringify(logObj) + "\n");
});

const key = Symbol() as InjectionKey<Logger<ILogObj>>

export { logger, key };
iccicci commented 1 year ago

The portion of code you pasted is fine, TypeScript is fine, I don't know vue, but it should be fine as well.

When I spoken about your project I was not speaking directly about your code, maybe some other packages...

Please check this two lines of code; the built-in setTimeout function returns an object which has an unref method, the only explaination coming to my mind for the problem is you reported is that something replaced the built-in setTimeout function with anouther function which returns something else.

viniciusteixeiradias commented 1 year ago

I created a project from scratch to validate if the error still persists. The error remains the same, if you want to test: https://github.com/viniciusteixeiradias/issue-rotate-file-system

The rotate-file-system config https://github.com/viniciusteixeiradias/issue-rotate-file-system/blob/main/src/App.vue

iccicci commented 1 year ago

https://stackoverflow.com/a/54785005/4690946

leviplj commented 1 year ago

@iccicci , quick question... Does rotating-file-stream run on the browser?

iccicci commented 1 year ago

Nope @leviplj , it is designed only for Node.js, it doesn't implement writing files from browsers; it takes advantage from Node.js libraries to hadle files IO.

viniciusteixeiradias commented 1 year ago

@iccicci "Universal: Works in Browsers and Node.js"?

Screenshot 2023-03-10 at 10 00 27 pm
iccicci commented 1 year ago

@viniciusteixeiradias I don't know universal

viniciusteixeiradias commented 1 year ago

@iccicci Yes sorry. It's just that I was using the ts-log lib and they indicate using rotate-file-system, ts-log runs in browsers, but apparently rotate-file-system does not. The only problem I had was with the interval, but since it's not browser compatible, I closed the issue.