Sambit
Sambit

Reputation: 8011

Unable to see in the console in deno logger

I have written a very simple Typescript class. But I am unable to see the loggers in the console. I want to know what I am doing wrong. I referred to the following link. https://deno.land/std/log

import * as log from "https://deno.land/std/log/mod.ts";

export class Test1 {

    public show() {
        log.debug("Hello world");
        log.info("Hello world");
        log.warning("Hello world");
        log.error("Hello world");
        log.critical("500 Internal server error");
        console.log("Hello ....")
    }

}

const test = new Test1();
test.show();

Please suggest me in this case. I am learning deno.

Upvotes: 3

Views: 1140

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40414

There's a bug in the default logger, the setup happens asynchronously, so you have to wait until it's been set up correctly.

For now, a workaround is to wait until the next tick

import * as log from "https://deno.land/std/log/mod.ts";

await new Promise((resolve) => setTimeout(resolve, 0));

export class Test1 {
  /* ... */
}

Or setup your own logger:

import * as log from "https://deno.land/std/log/mod.ts";

await log.setup({
  handlers: {
    console: new log.handlers.ConsoleHandler("DEBUG"),
  },
  loggers: {
    // configure default logger available via short-hand methods above
    default: {
      level: "DEBUG",
      handlers: ["console"],
    }
  },
});

const logger = log.getLogger()

export class Test1 {

    public show() {
        logger.debug("Hello world");
        logger.info("Hello world");
        logger.warning("Hello world");
        logger.error("Hello world");
        logger.critical("500 Internal server error");
        console.log("Hello ....")
    }

}

const test = new Test1();
test.show();

Update: I've submitted a PR for this, and hopefully soon it will work as expected.

2nd Update: The PR was merged and it will be released on std/0.52.0.

Upvotes: 3

Related Questions