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

Improve generics for Logger, LoggerOptions, LogFn and WriteFn #2011

Open
emilio-toledo opened this issue Jul 19, 2024 · 0 comments
Open

Improve generics for Logger, LoggerOptions, LogFn and WriteFn #2011

emilio-toledo opened this issue Jul 19, 2024 · 0 comments

Comments

@emilio-toledo
Copy link

Use Case

Hello, I am a type obsessive person and currently the only way that I found to strictly type everything in Pino is with something like what I am currently using:

import CustomToast from "$lib/components/CustomToast.svelte";
import type { Error as Err } from "@repo/models";
import { pino, type Logger, type LoggerOptions } from "pino";
import toast from "svelte-french-toast";

type CustomLogger<CustomLevels extends string = never> = Omit<
	Logger<CustomLevels>,
	InfoLogLevel | ErrorLogLevel
> &
	(InfoLogLevelFunctions & ErrorLogLevelFunctions);

type CustomLoggerOptions = Omit<
	NonNullable<LoggerOptions<CustomLevels>>,
	"browser" | "customLevels"
> & {
	browser?: Omit<NonNullable<LoggerOptions["browser"]>, "write"> & {
		write?:
			| CustomLogFn
			| Partial<InfoLogLevelFunctions>
			| Partial<ErrorLogLevelFunctions>;
	};
	customLevels: {
		[K in CustomLevels]: number;
	};
};

type CustomLevels = "success";

type ErrorLogLevel = "fatal" | "error";
type InfoLogLevel =
	| "warn"
	| "info"
	| "debug"
	| "trace"
	| "silent"
	| CustomLevels;

type InfoLogLevelFunctions = {
	[K in InfoLogLevel]: CustomLogFn;
};

type ErrorLogLevelFunctions = {
	[K in ErrorLogLevel]: CustomErrorFn;
};

type CustomLogFn = (data: {
	name: string;
	message: string;
}) => void;

type CustomErrorFn = (data: Err) => void;

const options: CustomLoggerOptions = {
	browser: {
		write: {
			error: (args) => {
				toast(CustomToast, {
					duration: 5000,
					position: "bottom-right",

					//@ts-ignore
					props: {
						type: "error",
						title: "Failed",
						id: args.id,
						description: args.message,
					},
				});

				console.log(args);
			},

			info: (args) => {
				toast(CustomToast, {
					duration: 5000,
					position: "bottom-right",

					//@ts-ignore
					props: {
						type: "info",
						title: args.name,
						description: args.message,
					},
				});

				console.log(args);
			},
		},
	},
	customLevels: {
		success: 20,
	},
};

export const logger: CustomLogger<CustomLevels> = pino(
	options as LoggerOptions<CustomLevels>,
);

Is it possible to have some generics for these types outside of the individual logger.method<type>()?

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

1 participant