Skip to content

API Reference

import { MimeLogger } from "mime-logger";
import Logger from "mime-logger"; // default export, same class
new MimeLogger(name?: string, opts?: LoggerOptions)
FieldTypeRequiredDescription
namestringoptionalLogger name. Appears in log output as (name).
optsLoggerOptionsoptionalFormat and output options.
FieldTypeRequiredDescription
info(message, ...args)voidoptionalLog at INFO level.
warn(message, ...args)voidoptionalLog at WARN level.
error(message, ...args)voidoptionalLog at ERROR level.
log(level, message, args)voidoptionalLow-level log. args is an array.
child(name)MimeLoggeroptionalCreate a child logger. Name is appended as parent/child.
write(message, ...args)voidoptionalWrite a formatted line to stdout. No level badge.
promisesWrite(message, level, ...promises)Promise<void>optionalResolve promises inline in log output. Use %p as placeholder.

Awaits each promise and writes its resolved value where %p appears in the message. Throws if the message has no %p. (I admits this is a bit useless)

await logger.promisesWrite(
"User: %p, Role: %p",
LogLevel.INFO,
fetchUser(id),
fetchRole(id),
);
// [12:34:56.789] INFO (app): User: alice, Role: admin
interface LoggerOptions {
format?: string | FormatFn;
output?: OutputTarget | OutputTarget[];
}
enum LogLevel {
INFO = "info",
WARN = "warn",
ERROR = "error",
}
interface FormatObject {
message: string;
name?: string;
timestamp: Date;
level: LogLevel;
args: any[];
}
type FormatFn = (obj: FormatObject, colors: ColorHelpers) => string;
type OutputTarget =
| "console"
| FileOutputOptions
| ((message: string, obj: FormatObject) => void);
interface FileOutputOptions {
type: "file";
path: string; // supports {year} {month} {day} {date} {hour} {minutes} {seconds} {ms}
}
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;
}
interface CallerInfoOptions {
skipFrames?: number;
additionalSkip?: RegExp;
}
import { Formats } from "mime-logger";
FieldTypeRequiredDescription
Formats.prettyFormatFnoptionalDefault. Colored timestamp, level badge, name, message.
Formats.minimalFormatFnoptionalCompact. Level padded to 5 chars, dimmed name, plain message.
Formats.detailedFormatFnoptionalCaller location + ISO timestamp prepended.
Formats.jsonFormatFnoptionalStructured JSON output: { timestamp, level, name, message }.

Returns a colored ANSI string for the given level — e.g. "INFO" in green.

import { getLevelString } from "mime-logger";
getLevelString(LogLevel.WARN); // yellow "WARN"

Extracts stack frame information for the calling site. Used internally by Formats.detailed and {file} / {line} format tokens.

import { getCallerInfo } from "mime-logger";
const { file, line, function: fn, caller } = getCallerInfo({ skipFrames: 1 });

The ColorHelpers instance used by all built-in presets.

import { colors } from "mime-logger";
console.log(colors.green("ok"));