Module

x/justaos_orm/deps.ts>Logger

JUSTAOS's ORM (Object–relational mapping) tool is built for Deno and provides transparent persistence for JavaScript objects to Postgres database.
Go to Latest
class Logger
import { Logger } from "https://dotland.deno.dev/x/justaos_orm@v6.4.0/deps.ts";

Constructors

new
Logger(
loggerName: string,
levelName: LevelName,
options?: LoggerOptions,
)

Properties

handlers: BaseHandler[]
level: LogLevel

Use this to retrieve the current numeric log level.

levelName: LevelName
readonly
loggerName: string

Methods

asString(data: unknown, isProperty?): string
critical<T>(msg: () => T, ...args: unknown[]): T | undefined
critical<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T
debug<T>(msg: () => T, ...args: unknown[]): T | undefined
debug<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T
error<T>(msg: () => T, ...args: unknown[]): T | undefined
error<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T
info<T>(msg: () => T, ...args: unknown[]): T | undefined
info<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T
warn<T>(msg: () => T, ...args: unknown[]): T | undefined
warn<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T
deprecated
warning<T>(msg: () => T, ...args: unknown[]): T | undefined
deprecated
warning<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T
import Logger
import { Logger } from "https://dotland.deno.dev/x/justaos_orm@v6.4.0/deps.ts";

Logging library with the support for terminal and file outputs. Also provides interfaces for building custom loggers.

Loggers

Loggers are objects that you interact with. When you use a logger method it constructs a LogRecord and passes it down to its handlers for output. To create custom loggers, specify them in loggers when calling log.setup.

Custom message format

If you want to override default format of message you can define formatter option for handler. It can be either simple string-based format that uses LogRecord fields or more complicated function-based one that takes LogRecord as argument and outputs string.

The default log format is {levelName} {msg}.

Logging Structured JSON Lines

To output logs in a structured JSON format you can configure most handlers with a formatter that produces a JSON string. Either use the premade log.formatters.jsonFormatter or write your own function that takes a {@link LogRecord} and returns a JSON.stringify'd object. If you want the log to go to stdout then use {@link ConsoleHandler} with the configuration useColors: false to turn off the ANSI terminal colours.

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

log.setup({
  handlers: {
    default: new log.ConsoleHandler("DEBUG", {
      formatter: log.formatters.jsonFormatter,
      useColors: false,
    }),
  },
});

The first argument passed to a log function is always treated as the message and will be stringified differently. To have arguments JSON.stringify'd you must pass them after the first.

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

log.info("This is the message", { thisWillBe: "JSON.stringify'd"});
// {"level":"INFO","datetime":1702501580505,"message":"This is the message","args":{"thisWillBe":"JSON.stringify'd"}}

log.info({ thisWontBe: "JSON.stringify'd"}, "This is an argument");
// {"level":"INFO","datetime":1702501580505,"message":"{\"thisWontBe\":\"JSON.stringify'd\"}","args":"This is an argument"}

Inline Logging

Log functions return the data passed in the msg parameter. Data is returned regardless if the logger actually logs it.

Lazy Log Evaluation

Some log statements are expensive to compute. In these cases, you can use lazy log evaluation to prevent the computation taking place if the logger won't log the message.

NOTE: When using lazy log evaluation, undefined will be returned if the resolver function is not called because the logger won't log it. It is an antipattern use lazy evaluation with inline logging because the return value depends on the current log level.

For module authors

The authors of public modules can let the users display the internal logs of the module by using a custom logger:

import { getLogger } from "https://deno.land/std@$STD_VERSION/log/mod.ts";

function logger() {
  return getLogger("my-awesome-module");
}

export function sum(a: number, b: number) {
  logger().debug(`running ${a} + ${b}`);
  return a + b;
}

export function mult(a: number, b: number) {
  logger().debug(`running ${a} * ${b}`);
  return a * b;
}

The user of the module can then display the internal logs with:

import * as log from "https://deno.land/std@$STD_VERSION/log/mod.ts";
import { sum } from "<the-awesome-module>/mod.ts";

log.setup({
  handlers: {
    console: new log.ConsoleHandler("DEBUG"),
  },

  loggers: {
    "my-awesome-module": {
      level: "DEBUG",
      handlers: ["console"],
    },
  },
});

sum(1, 2); // prints "running 1 + 2" to the console

Please note that, due to the order of initialization of the loggers, the following won't work:

import { getLogger } from "https://deno.land/std@$STD_VERSION/log/mod.ts";

const logger = getLogger("my-awesome-module");

export function sum(a: number, b: number) {
  logger.debug(`running ${a} + ${b}`); // no message will be logged, because getLogger() was called before log.setup()
  return a + b;
}