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

Can't use custom levels in multistream with TypeScript #1595

Closed
frzsombor opened this issue Nov 21, 2022 · 11 comments
Closed

Can't use custom levels in multistream with TypeScript #1595

frzsombor opened this issue Nov 21, 2022 · 11 comments

Comments

@frzsombor
Copy link
Contributor

frzsombor commented Nov 21, 2022

The following code worked totally fine in JS, but I'm currently in the process of converting my NodeJS app to typescript.
I would like to use custom levels in multistream with TypeScript, but the definitions are not allowing me to do so.
I have this file, where I prepare and configure pino to use it in different parts of my application:

import * as fs from 'fs';
import pino, { StreamEntry } from 'pino';
import pretty from 'pino-pretty';

const customLevels = {
    debug   : 1,
    info    : 2,
    network : 3, // <- my custom level
    error   : 4,
};

const pinoOpts = {
    //...
    useOnlyCustomLevels: true,
    customLevels: customLevels,
    //...
};

const multistreamOpts = {
    dedupe: true, //send logs only to the stream with the higher level
    levels: customLevels,
};

const streams: StreamEntry[] = [
    { level : 'debug',   stream : fs.createWriteStream(`./log/log-debug.log`,   { flags: 'a' }) },
    { level : 'info',    stream : pretty() },
    { level : 'info',    stream : fs.createWriteStream(`./log/log-info.log`,    { flags: 'a' }) },
    { level : 'network', stream : fs.createWriteStream(`./log/log-network.log`, { flags: 'a' }) },
/*             ^^^^^^^
                  └ - ❗ TypeScript error ❗
*/
    { level : 'error',   stream : pretty() },
    { level : 'error',   stream : fs.createWriteStream(`./log/log-error.log`,   { flags: 'a' }) },
];

export default pino(pinoOpts, pino.multistream(streams, multistreamOpts));

However, I get the following error where I define streams:
Type '"network"' is not assignable to type 'Level | undefined'.ts(2322)

The reason is that in pino.d.ts, Level is explicitly defined to be "fatal" | "error" | "warn" | "info" | "debug" | "trace" and no matter how many times and different approaches I tried to override/extend the definition, none of my attempts worked.

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@frzsombor
Copy link
Contributor Author

@mcollina I'm quite new to TS, and not sure what would be the best practice, to fix this issue.
However if you, or someone could help me with an idea, I can try to implement it in a PR.

@aalzubidy
Copy link

I can submit a PR, is it removing the type checking for that one so users can add custom levels or map the custom levels values to the type?

@frzsombor
Copy link
Contributor Author

frzsombor commented Feb 18, 2023

@mcollina @VladimirMikulic Please correct me, if I'm wrong, but I think this issue still exists, I can still only get around this problem by using @ts-ignore on the line above the custom level, even with the latest v8.10.0 version.

@ramiel
Copy link

ramiel commented Mar 1, 2023

I see the very same error

@mcollina mcollina reopened this Mar 1, 2023
@mcollina
Copy link
Member

mcollina commented Mar 3, 2023

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina
Copy link
Member

mcollina commented Mar 3, 2023

cc @kibertoad

@Puppo
Copy link
Contributor

Puppo commented Aug 8, 2023

@mcollina, Could I help with this bug? I have an idea in mind.

Puppo added a commit to Puppo/pino that referenced this issue Aug 8, 2023
Puppo added a commit to Puppo/pino that referenced this issue Aug 8, 2023
Puppo added a commit to Puppo/pino that referenced this issue Aug 8, 2023
@simoneb
Copy link
Contributor

simoneb commented Aug 21, 2023

This was actually closed by #1778. @mcollina can you close this issue?

@mcollina
Copy link
Member

thx @simoneb

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants