Skip to content

Instantly share code, notes, and snippets.

@GitaiQAQ
Created March 24, 2023 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GitaiQAQ/f3cfbac5263ca5604e391ac043c176e8 to your computer and use it in GitHub Desktop.
Save GitaiQAQ/f3cfbac5263ca5604e391ac043c176e8 to your computer and use it in GitHub Desktop.
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
// Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
// 0. You just DO WHAT THE FUCK YOU WANT TO.
const noop = () => {};
const LOG_LEVELS = {
debug: 0,
trace: 1,
log: 2,
info: 3,
warn: 4,
error: 5,
};
type LOG_LEVEL = keyof typeof LOG_LEVELS;
const ENV_VAR_NODE_LOG = "NODE_INFO";
const DEFAULT_NODE_LOG: LOG_LEVEL = "info";
const ENV_NODE_LOG =
process.env[ENV_VAR_NODE_LOG] || (DEFAULT_NODE_LOG as LOG_LEVEL);
const mapValues = (
obj: Record<string, any>,
fn: (value: any, key: string) => any
) => {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, fn(value, key)])
);
};
export const getLogger = (...prefix: any[]) => {
return mapValues(LOG_LEVELS, (level, type) => {
// @ts-ignore
if (level >= LOG_LEVELS[ENV_NODE_LOG]) {
// @ts-ignore
return console[type].bind(console, ...prefix);
}
return noop;
}) as Pick<Console, keyof typeof LOG_LEVELS>;
};
export const logger = getLogger("[default]");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment