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

[bug] File not rotating when using a generator function #100

Open
JosuerBague opened this issue Mar 7, 2024 · 6 comments
Open

[bug] File not rotating when using a generator function #100

JosuerBague opened this issue Mar 7, 2024 · 6 comments
Assignees

Comments

@JosuerBague
Copy link

JosuerBague commented Mar 7, 2024

Hey there,
I'm trying to use you package for my pino logger like so:

import { Logger as PinoLogger } from './logger.package';
const rfsa = require('rotating-file-stream');

const pad = (num: number) => (num > 9 ? ""  : "0") + num;

const generator = () => {
    const time = new Date();

    const year = time.getFullYear();
    const month = pad(time.getMonth() + 1);
    const day = pad(time.getDate());
    const hour = pad(time.getHours());

    return `application-${year}-${month}-${day}-${hour}.log`;
}

const stream = rfsa.createStream(generator, {
    interval: '15s',
    compress: true,
    path: 'logs'
})

const logger = new PinoLogger(stream);

export { logger };
export type { ILogger } from './libs/interfaces/interfaces';

This doesn't rotate the file. But when I provide a filename directly like so, it does:

const stream = rfsa.createStrearm('sample.log', { interval: '15s', compress: true, path: 'logs' })

@iccicci iccicci self-assigned this Mar 7, 2024
@iccicci
Copy link
Owner

iccicci commented Mar 7, 2024

Your filename generator doesn't accepts parameters...

Please refer to the documentation.

If this solves the problem, feel free to close the issue.

@JosuerBague
Copy link
Author

I tried using the filename generator in the documentation but it still wouldn't rotate the file.
Had it set to rotate at 10s.

const generator = (time: Date, index: number) => {
    if (!time) return "file.log";
  
    const month = time.getFullYear() + "" + pad(time.getMonth() + 1);
    const day = pad(time.getDate());
    const hour = pad(time.getHours());
    const minute = pad(time.getMinutes());
  
    return `${month}/${month}${day}-${hour}${minute}-${index}-file.log`;
  };



const stream = rfsa.createStream(generator, {
    interval: '10s',
    compress: true,
})

@iccicci
Copy link
Owner

iccicci commented Mar 8, 2024

I suggest you to follow error and warning events.

stream.on("error", console.log);
stream.on("warning", console.log);

More than this there some known problems when rfs.createStream is called twice with the same configuration; I suggest you to ensure that piece of code runs only once.

@nodermann2
Copy link

Hey there, I'm trying to use you package for my pino logger like so:

import { Logger as PinoLogger } from './logger.package';
const rfsa = require('rotating-file-stream');

const pad = (num: number) => (num > 9 ? ""  : "0") + num;

const generator = () => {
    const time = new Date();

    const year = time.getFullYear();
    const month = pad(time.getMonth() + 1);
    const day = pad(time.getDate());
    const hour = pad(time.getHours());

    return `application-${year}-${month}-${day}-${hour}.log`;
}

const stream = rfsa.createStream(generator, {
    interval: '15s',
    compress: true,
    path: 'logs'
})

const logger = new PinoLogger(stream);

export { logger };
export type { ILogger } from './libs/interfaces/interfaces';

This doesn't rotate the file. But when I provide a filename directly like so, it does:

const stream = rfsa.createStrearm('sample.log', { interval: '15s', compress: true, path: 'logs' })

The same problem, try to remove compress option.

@iccicci
Copy link
Owner

iccicci commented Jun 16, 2024

Hi @nodermann ,

Also your filename generator doesn't accepts parameters...

It must accept two parameters. Please refer to the documentation.

@iccicci
Copy link
Owner

iccicci commented Jun 16, 2024

Also compress: true probably works only on Linux, if you are using other operating systems... butter to use compress: "gzip".

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

3 participants