log statements are a noop when bound at the top-level
Opened this issue · 1 comments
SebastienGllmt commented
I'm running Deno 2 and I tried to do the following
const levelMethods = {
[log.levels.TRACE]: log.trace,
[log.levels.DEBUG]: log.debug,
[log.levels.INFO]: log.info,
[log.levels.WARN]: log.warn,
[log.levels.ERROR]: log.error,
} as const;
function doLog(
level: keyof typeof levelMethods,
...msg: unknown[]
): void {
levelMethods[level](...msg);
}
However, this prints nothing to the console. If I try and look at the result of levelMethods[level]
, the type is [Function: noop]
which seems to be the default type before things are initialized (see here)
This seems like an initialization order issue though, because if I do
function doLog(
level: typeof log.levels[keyof typeof log.levels],
...msg: unknown[]
): void {
const levelMethods = {
[log.levels.TRACE]: log.trace,
[log.levels.DEBUG]: log.debug,
[log.levels.INFO]: log.info,
[log.levels.WARN]: log.warn,
[log.levels.ERROR]: log.error,
[log.levels.SILENT]: () => {},
} as const;
levelMethods[level](...msg);
}
then everything works
Mr0grog commented
Can you give a more complete picture of your project? Your first example seems to print just fine for me.
My setup: Using Deno 2.1.2, with…
// file: deno.json
{
"imports": {
"loglevel": "npm:loglevel"
}
}
…and:
// file: main.ts
import log from 'loglevel';
const levelMethods = {
[log.levels.TRACE]: log.trace,
[log.levels.DEBUG]: log.debug,
[log.levels.INFO]: log.info,
[log.levels.WARN]: log.warn,
[log.levels.ERROR]: log.error,
} as const;
function doLog(
level: keyof typeof levelMethods,
...msg: unknown[]
): void {
levelMethods[level](...msg);
}
doLog(log.levels.WARN, 'Hello from loglevel!');
…then running deno run main.ts
prints out Hello from loglevel!
exactly as you’d expect.