Class Logger

A logger that can log messages at different levels.

import { Logger } from "@std/log/logger";
import { assertEquals } from "@std/assert/equals";

const logger = new Logger("example", "INFO");
const result = logger.info("Hello, world!");

assertEquals(result, "Hello, world!");

Constructors

Properties

Accessors

Methods

Constructors

  • Constructs a new instance.

    Parameters

    • loggerName: string

      The name of the logger.

    • levelName: LevelName

      The name of the log level.

    • options: LoggerOptions = {}

      The options to create a new logger.

    Returns Logger

Properties

handlers: BaseHandler[]

The handlers to use for the logger.

import { Logger } from "@std/log/logger";
import { ConsoleHandler } from "@std/log/console-handler";
import { assertEquals } from "@std/assert/equals";

const handler = new ConsoleHandler("INFO");
const logger = new Logger("example", "INFO", {
handlers: [handler],
});

assertEquals(logger.handlers, [handler]);

Accessors

  • get level(): LogLevel
  • Getter for the log level.

    Returns LogLevel

    The log level.

    import { Logger } from "@std/log/logger";
    import { LogLevels } from "@std/log/levels";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");
    assertEquals(logger.level, LogLevels.INFO);
  • set level(level): void
  • Setter for the log level.

    Parameters

    Returns void

    import { Logger } from "@std/log/logger";
    import { LogLevels } from "@std/log/levels";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");
    logger.level = LogLevels.DEBUG;

    assertEquals(logger.level, LogLevels.DEBUG);
  • get levelName(): LevelName
  • Getter for the name of the log level.

    Returns LevelName

    The name of the log level.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");
    assertEquals(logger.levelName, "INFO");
  • set levelName(levelName): void
  • Setter for the name of the log level.

    Parameters

    • levelName: LevelName

      The name of the log level to set.

    Returns void

    import { Logger } from "@std/log/logger";
    import { LogLevels } from "@std/log/levels";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");
    logger.levelName = "DEBUG";

    assertEquals(logger.level, LogLevels.DEBUG);
  • get loggerName(): string
  • Getter for the name of the logger.

    Returns string

    The name of the logger.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.loggerName, "example");

Methods

  • Log at the critical level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: (() => T)

      The message to log.

        • (): T
        • Returns T

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns undefined | T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.critical("Hello, world!"), "Hello, world!");

    assertEquals(logger.critical(() => "Hello, world!"), "Hello, world!");
  • Log at the critical level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: T extends GenericFunction
          ? never
          : T

      The message to log.

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.critical("Hello, world!"), "Hello, world!");

    assertEquals(logger.critical(() => "Hello, world!"), "Hello, world!");
  • Log at the debug level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: (() => T)

      The message to log.

        • (): T
        • Returns T

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns undefined | T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.debug("Hello, world!"), "Hello, world!");

    assertEquals(logger.debug(() => "Hello, world!"), undefined);
  • Log at the debug level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: T extends GenericFunction
          ? never
          : T

      The message to log.

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.debug("Hello, world!"), "Hello, world!");

    assertEquals(logger.debug(() => "Hello, world!"), undefined);
  • Log at the error level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: (() => T)

      The message to log.

        • (): T
        • Returns T

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns undefined | T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.error("Hello, world!"), "Hello, world!");

    assertEquals(logger.error(() => "Hello, world!"), "Hello, world!");
  • Log at the error level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: T extends GenericFunction
          ? never
          : T

      The message to log.

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.error("Hello, world!"), "Hello, world!");

    assertEquals(logger.error(() => "Hello, world!"), "Hello, world!");
  • Log at the info level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: (() => T)

      The message to log.

        • (): T
        • Returns T

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns undefined | T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.info("Hello, world!"), "Hello, world!");

    assertEquals(logger.info(() => "Hello, world!"), "Hello, world!");
  • Log at the info level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: T extends GenericFunction
          ? never
          : T

      The message to log.

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.info("Hello, world!"), "Hello, world!");

    assertEquals(logger.info(() => "Hello, world!"), "Hello, world!");
  • Log at the warning level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: (() => T)

      The message to log.

        • (): T
        • Returns T

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns undefined | T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.warn("Hello, world!"), "Hello, world!");

    assertEquals(logger.warn(() => "Hello, world!"), "Hello, world!");
  • Log at the warning level.

    Type Parameters

    • T

      The type of the message to log.

    Parameters

    • msg: T extends GenericFunction
          ? never
          : T

      The message to log.

    • Rest...args: unknown[]

      Arguments to be formatted into the message.

    Returns T

    The message that was logged.

    import { Logger } from "@std/log/logger";
    import { assertEquals } from "@std/assert/equals";

    const logger = new Logger("example", "INFO");

    assertEquals(logger.warn("Hello, world!"), "Hello, world!");

    assertEquals(logger.warn(() => "Hello, world!"), "Hello, world!");