Skip to main content
Home
This release is 2 versions behind 0.9.2 — the latest version of @logtape/logtape. Jump to latest

Built and signed on GitHub Actions

Simple logging library with zero dependencies for Deno/Node.js/Bun/browsers

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (0.9.0)
interface Logger

A logger interface. It provides methods to log messages at different severity levels.

const logger = getLogger("category");
logger.debug `A debug message with ${value}.`;
logger.info `An info message with ${value}.`;
logger.warn `A warning message with ${value}.`;
logger.error `An error message with ${value}.`;
logger.fatal `A fatal error message with ${value}.`;

Properties

readonly
category: readonly string[]

The category of the logger. It is an array of strings.

readonly
parent: Logger | null

The logger with the supercategory of the current logger. If the current logger is the root logger, this is null.

Methods

getChild(subcategory:
string
| readonly [string]
| readonly [string, ...string[]]
): Logger

Get a child logger with the given subcategory.

const logger = getLogger("category");
const subLogger = logger.getChild("sub-category");

The above code is equivalent to:

const logger = getLogger("category");
const subLogger = getLogger(["category", "sub-category"]);
with(properties: Record<string, unknown>): Logger

Get a logger with contextual properties. This is useful for log multiple messages with the shared set of properties.

const logger = getLogger("category");
const ctx = logger.with({ foo: 123, bar: "abc" });
ctx.info("A message with {foo} and {bar}.");
ctx.warn("Another message with {foo}, {bar}, and {baz}.", { baz: true });

The above code is equivalent to:

const logger = getLogger("category");
logger.info("A message with {foo} and {bar}.", { foo: 123, bar: "abc" });
logger.warn(
  "Another message with {foo}, {bar}, and {baz}.",
  { foo: 123, bar: "abc", baz: true },
);
debug(
message: TemplateStringsArray,
...values: readonly unknown[],
): void

Log a debug message. Use this as a template string prefix.

logger.debug `A debug message with ${value}.`;
debug(
message: string,
properties?: Record<string, unknown> | (() => Record<string, unknown>),
): void

Log a debug message with properties.

logger.debug('A debug message with {value}.', { value });

If the properties are expensive to compute, you can pass a callback that returns the properties:

logger.debug(
  'A debug message with {value}.',
  () => ({ value: expensiveComputation() })
);
debug(callback: LogCallback): void

Lazily log a debug message. Use this when the message values are expensive to compute and should only be computed if the message is actually logged.

logger.debug(l => l`A debug message with ${expensiveValue()}.`);
info(
message: TemplateStringsArray,
...values: readonly unknown[],
): void

Log an informational message. Use this as a template string prefix.

logger.info `An info message with ${value}.`;
info(
message: string,
properties?: Record<string, unknown> | (() => Record<string, unknown>),
): void

Log an informational message with properties.

logger.info('An info message with {value}.', { value });

If the properties are expensive to compute, you can pass a callback that returns the properties:

logger.info(
  'An info message with {value}.',
  () => ({ value: expensiveComputation() })
);
info(callback: LogCallback): void

Lazily log an informational message. Use this when the message values are expensive to compute and should only be computed if the message is actually logged.

logger.info(l => l`An info message with ${expensiveValue()}.`);
warn(
message: TemplateStringsArray,
...values: readonly unknown[],
): void

Log a warning message. Use this as a template string prefix.

logger.warn `A warning message with ${value}.`;
warn(
message: string,
properties?: Record<string, unknown> | (() => Record<string, unknown>),
): void

Log a warning message with properties.

logger.warn('A warning message with {value}.', { value });

If the properties are expensive to compute, you can pass a callback that returns the properties:

logger.warn(
  'A warning message with {value}.',
  () => ({ value: expensiveComputation() })
);
warn(callback: LogCallback): void

Lazily log a warning message. Use this when the message values are expensive to compute and should only be computed if the message is actually logged.

logger.warn(l => l`A warning message with ${expensiveValue()}.`);
error(
message: TemplateStringsArray,
...values: readonly unknown[],
): void

Log an error message. Use this as a template string prefix.

logger.error `An error message with ${value}.`;
error(
message: string,
properties?: Record<string, unknown> | (() => Record<string, unknown>),
): void

Log an error message with properties.

logger.warn('An error message with {value}.', { value });

If the properties are expensive to compute, you can pass a callback that returns the properties:

logger.error(
  'An error message with {value}.',
  () => ({ value: expensiveComputation() })
);
error(callback: LogCallback): void

Lazily log an error message. Use this when the message values are expensive to compute and should only be computed if the message is actually logged.

logger.error(l => l`An error message with ${expensiveValue()}.`);
fatal(
message: TemplateStringsArray,
...values: readonly unknown[],
): void

Log a fatal error message. Use this as a template string prefix.

logger.fatal `A fatal error message with ${value}.`;
fatal(
message: string,
properties?: Record<string, unknown> | (() => Record<string, unknown>),
): void

Log a fatal error message with properties.

logger.warn('A fatal error message with {value}.', { value });

If the properties are expensive to compute, you can pass a callback that returns the properties:

logger.fatal(
  'A fatal error message with {value}.',
  () => ({ value: expensiveComputation() })
);
fatal(callback: LogCallback): void

Lazily log a fatal error message. Use this when the message values are expensive to compute and should only be computed if the message is actually logged.

logger.fatal(l => l`A fatal error message with ${expensiveValue()}.`);

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@logtape/logtape

Import symbol

import { type Logger } from "@logtape/logtape";
or

Import directly with a jsr specifier

import { type Logger } from "jsr:@logtape/logtape";

Add Package

pnpm i jsr:@logtape/logtape
or (using pnpm 10.8 or older)
pnpm dlx jsr add @logtape/logtape

Import symbol

import { type Logger } from "@logtape/logtape";

Add Package

yarn add jsr:@logtape/logtape
or (using Yarn 4.8 or older)
yarn dlx jsr add @logtape/logtape

Import symbol

import { type Logger } from "@logtape/logtape";

Add Package

npx jsr add @logtape/logtape

Import symbol

import { type Logger } from "@logtape/logtape";

Add Package

bunx jsr add @logtape/logtape

Import symbol

import { type Logger } from "@logtape/logtape";