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

[question] How do I create dynamic customized logfile names? #1917

Open
JosuerBague opened this issue Mar 8, 2024 · 3 comments
Open

[question] How do I create dynamic customized logfile names? #1917

JosuerBague opened this issue Mar 8, 2024 · 3 comments

Comments

@JosuerBague
Copy link

JosuerBague commented Mar 8, 2024

Hi there,
I'm new to pino so sorry if my question is very basic.

I'm trying to use a custom transport to log to a file with this format: application-<year>-<month>-<date>.log
I want it to switch files when the day ends / transfers.

Currently I have this in my custom transport:

import { createWriteStream } from "fs";

const pad = num => (num > 9) ? num : `0${num}`;

export default (options) => {
    const filename = generateFileName(options.filename);

    return createWriteStream(filename, { flags: 'a' });
}

function generateFileName(filenameParams) {
    const currentDate = new Date();

    const filename = filenameParams
        .replace('<year>', currentDate.getFullYear())
        .replace('<month>', pad(currentDate.getMonth() + 1))
        .replace('<date>', pad(currentDate.getDate()))
    
    return filename;
}

My logger:

const absolutePath = path.resolve(__dirname, './test.mjs');

const transport = pino.transport({
     target: absolutePath,
     options: { filename: 'application-<year>-<month>-<date>.log'}
 })

this.#logger = pino(transport)

This works for the first file but not for subsequent dates.
I understand this is because the custom transport is only called once on initialization and not each time the write functionality is called.

I've been scouring the net for examples for my use case to no avail. Help would be much appreciated.

@jsumners
Copy link
Member

jsumners commented Mar 8, 2024

I recommend utilizing your system tools to do this https://getpino.io/#/docs/help?id=rotate

@mcollina
Copy link
Member

mcollina commented Mar 9, 2024

check out the pino-roll module.

@JosuerBague
Copy link
Author

Hey guys,
thanks for the replies. I'm trying out using my system tools for now using a VM, but I'll take a look at the pino-roll module too.
Thanks a bunch!

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