Formats
mime-logger lets you control how each log line looks: use one of the four built-in presets,
write a format string with tokens, or provide a fully custom FormatFn.
Presets
Section titled “Presets”Import Formats and pass a preset to opts.format:
import Logger, { Formats } from "mime-logger";
const logger = new Logger("app", { format: Formats.pretty });| Field | Type | Required | Description |
|---|---|---|---|
Formats.pretty | FormatFn | optional | Default. Dimmed timestamp, colored level badge, yellow name in parens, colored message. |
Formats.minimal | FormatFn | optional | Level padded to 5 chars, dimmed name, plain message. Compact single-process output. |
Formats.detailed | FormatFn | optional | Caller file:line prepended, ISO timestamp. Best for debugging stack locations. |
Formats.json | FormatFn | optional | Structured JSON: { timestamp, level, name, message }. Pipe to log aggregators. |
import Logger, { Formats } from "mime-logger";
for (const preset of ["pretty", "minimal", "json"] as const) { console.log(`-- ${preset} --`); const logger = new Logger("app", { format: Formats[preset] }); logger.info("Server started on port 3000");}
Format string tokens
Section titled “Format string tokens”Pass a string instead of a FormatFn to use token substitution. Useful for simple
custom layouts without writing a function.
const logger = new Logger("api", { format: "{time} [{level}] {name} — {message}",});| Field | Type | Required | Description |
|---|---|---|---|
{time} | string | optional | Localized time with milliseconds: [HH:MM:SS.mmm] |
{date} | string | optional | Localized date string. |
{iso} | string | optional | ISO 8601 timestamp. |
{level} | string | optional | Level name: info | warn | error. |
{name} | string | optional | Logger name (empty string if unnamed). |
{message} | string | optional | The log message. |
{pid} | string | optional | Current process ID. |
{env} | string | optional | NODE_ENV or "production" as fallback. |
{file} | string | optional | Caller source filename. |
{line} | string | optional | Caller line number. |
{function} | string | optional | Caller function name. |
{caller} | string | optional | One frame above the caller function. |
%s | string | optional | Interpolation placeholder for extra args. |
Custom format function
Section titled “Custom format function”For full control, provide a FormatFn. It receives a FormatObject and a ColorHelpers
instance.
import Logger, { type FormatFn } from "mime-logger";
const myFormat: FormatFn = (obj, c) => { const ts = c.dim(obj.timestamp.toISOString()); const level = obj.level.toUpperCase().padEnd(5); const name = obj.name ? ` ${c.cyan(obj.name)}` : ""; return `${ts} ${level}${name} ${obj.message}`;};
const logger = new Logger("app", { format: myFormat });FormatObject
Section titled “FormatObject”interface FormatObject { message: string; name?: string; timestamp: Date; level: LogLevel; args: any[];}ColorHelpers
Section titled “ColorHelpers”interface ColorHelpers { red: (s: string) => string; yellow: (s: string) => string; blue: (s: string) => string; green: (s: string) => string; cyan: (s: string) => string; magenta: (s: string) => string; gray: (s: string) => string; bold: (s: string) => string; dim: (s: string) => string;}Color functions are powered by ansis and automatically strip ANSI codes when writing to non-TTY outputs (e.g., files).
