Search code examples
deno

How to control log level of deno/std/log module from command line?


for example, how to log out debug info Hello world for

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

// Simple default logger out of the box. You can customize it
// by overriding logger and handler named "default", or providing
// additional logger configurations. You can log any data type.
log.debug("Hello world");
log.info(123456);
log.warning(true);
log.error({ foo: "bar", fizz: "bazz" });
log.critical("500 Internal server error");

from https://deno.land/std@0.173.0/log/mod.ts

I tried deno run -A --log-level=DEBUG with no luck.


Solution

  • for example, how to log out debug info Hello world for...

    Below is an example of how you can configure all levels of logging to the console at "DEBUG" and above.

    The module at https://deno.land/std@0.174.0/log/mod.ts includes some example code that looks just like the lines in your question (on lines 97-153 in the file) — it is what I used as a basis for the code below.

    so-75244033.ts:

    import * as log from "https://deno.land/std@0.174.0/log/mod.ts";
    
    // This could be read from an environment variable or parsed from a CLI argument:
    const LOG_LEVEL = "DEBUG";
    
    log.setup({
      handlers: {
        console: new log.handlers.ConsoleHandler(LOG_LEVEL),
      },
      loggers: {
        default: {
          level: LOG_LEVEL,
          handlers: ["console"],
        },
      },
    });
    
    // Simple default logger out of the box. You can customize it
    // by overriding logger and handler named "default", or providing
    // additional logger configurations. You can log any data type.
    log.debug("Hello world");
    log.info(123456);
    log.warning(true);
    log.error({ foo: "bar", fizz: "bazz" });
    log.critical("500 Internal server error");
    
    

    In the terminal:

    % deno --version
    deno 1.30.0 (release, x86_64-apple-darwin)
    v8 10.9.194.5
    typescript 4.9.4
    
    % deno run so-75244033.ts
    DEBUG Hello world
    INFO 123456
    WARNING true
    ERROR {"foo":"bar","fizz":"bazz"}
    CRITICAL 500 Internal server error