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

log statements are a noop when bound at the top-level #201

Open
SebastienGllmt opened this issue Dec 3, 2024 · 1 comment
Open

log statements are a noop when bound at the top-level #201

SebastienGllmt opened this issue Dec 3, 2024 · 1 comment

Comments

@SebastienGllmt
Copy link

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

@SebastienGllmt SebastienGllmt changed the title log statements are a noop log statements are a noop when bound at the top-level Dec 3, 2024
@Mr0grog
Copy link
Contributor

Mr0grog commented Dec 3, 2024

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.

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

2 participants