Skip to content

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.

Import Formats and pass a preset to opts.format:

import Logger, { Formats } from "mime-logger";
const logger = new Logger("app", { format: Formats.pretty });
FieldTypeRequiredDescription
Formats.prettyFormatFnoptionalDefault. Dimmed timestamp, colored level badge, yellow name in parens, colored message.
Formats.minimalFormatFnoptionalLevel padded to 5 chars, dimmed name, plain message. Compact single-process output.
Formats.detailedFormatFnoptionalCaller file:line prepended, ISO timestamp. Best for debugging stack locations.
Formats.jsonFormatFnoptionalStructured 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 presets output

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}",
});
FieldTypeRequiredDescription
{time}stringoptionalLocalized time with milliseconds: [HH:MM:SS.mmm]
{date}stringoptionalLocalized date string.
{iso}stringoptionalISO 8601 timestamp.
{level}stringoptionalLevel name: info | warn | error.
{name}stringoptionalLogger name (empty string if unnamed).
{message}stringoptionalThe log message.
{pid}stringoptionalCurrent process ID.
{env}stringoptionalNODE_ENV or "production" as fallback.
{file}stringoptionalCaller source filename.
{line}stringoptionalCaller line number.
{function}stringoptionalCaller function name.
{caller}stringoptionalOne frame above the caller function.
%sstringoptionalInterpolation placeholder for extra args.

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 });
interface FormatObject {
message: string;
name?: string;
timestamp: Date;
level: LogLevel;
args: any[];
}
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).