Référence API
Classe MimeLogger
Section intitulée « Classe MimeLogger »import { MimeLogger } from "mime-logger";import Logger from "mime-logger"; // export par défaut, même classeConstructeur
Section intitulée « Constructeur »new MimeLogger(name?: string, opts?: LoggerOptions)| Champ | Type | Requis | Description |
|---|---|---|---|
name | string | optionnel | Nom du logger. Apparaît dans la sortie sous forme (name). |
opts | LoggerOptions | optionnel | Options de format et de sortie. |
Méthodes d’instance
Section intitulée « Méthodes d’instance »| Champ | Type | Requis | Description |
|---|---|---|---|
info(message, ...args) | void | optionnel | Log au niveau INFO. |
warn(message, ...args) | void | optionnel | Log au niveau WARN. |
error(message, ...args) | void | optionnel | Log au niveau ERROR. |
log(level, message, args) | void | optionnel | Log bas niveau. args est un tableau. |
child(name) | MimeLogger | optionnel | Crée un logger enfant. Le nom est ajouté sous forme parent/enfant. |
write(message, ...args) | void | optionnel | Écrit une ligne formatée sur stdout. Sans badge de niveau. |
promisesWrite(message, level, ...promises) | Promise<void> | optionnel | Résout les promesses inline dans la sortie. Utilisez %p comme espace réservé. |
promisesWrite
Section intitulée « promisesWrite »Attend chaque promesse et écrit la valeur résolue là où %p apparaît dans le message.
Lève une erreur si le message ne contient pas de %p.
await logger.promisesWrite( "Utilisateur : %p, Rôle : %p", LogLevel.INFO, fetchUser(id), fetchRole(id),);// [12:34:56.789] INFO (app): Utilisateur : alice, Rôle : adminLoggerOptions
Section intitulée « LoggerOptions »interface LoggerOptions { format?: string | FormatFn; output?: OutputTarget | OutputTarget[];}LogLevel
Section intitulée « LogLevel »enum LogLevel { INFO = "info", WARN = "warn", ERROR = "error",}FormatObject
Section intitulée « FormatObject »interface FormatObject { message: string; name?: string; timestamp: Date; level: LogLevel; args: any[];}FormatFn
Section intitulée « FormatFn »type FormatFn = (obj: FormatObject, colors: ColorHelpers) => string;OutputTarget
Section intitulée « OutputTarget »type OutputTarget = | "console" | FileOutputOptions | ((message: string, obj: FormatObject) => void);FileOutputOptions
Section intitulée « FileOutputOptions »interface FileOutputOptions { type: "file"; path: string; // supporte {year} {month} {day} {date} {hour} {minutes} {seconds} {ms}}ColorHelpers
Section intitulée « 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;}CallerInfoOptions
Section intitulée « CallerInfoOptions »interface CallerInfoOptions { skipFrames?: number; additionalSkip?: RegExp;}Objet Formats
Section intitulée « Objet Formats »import { Formats } from "mime-logger";| Champ | Type | Requis | Description |
|---|---|---|---|
Formats.pretty | FormatFn | optionnel | Par défaut. Horodatage coloré, badge de niveau, nom, message. |
Formats.minimal | FormatFn | optionnel | Compact. Niveau complété à 5 caractères, nom atténué, message brut. |
Formats.detailed | FormatFn | optionnel | Emplacement de l'appelant + horodatage ISO préfixés. |
Formats.json | FormatFn | optionnel | Sortie JSON structurée : { timestamp, level, name, message }. |
Utilitaires
Section intitulée « Utilitaires »getLevelString
Section intitulée « getLevelString »Retourne une chaîne ANSI colorée pour le niveau donné — ex. "INFO" en vert.
import { getLevelString } from "mime-logger";getLevelString(LogLevel.WARN); // "WARN" en jaunegetCallerInfo
Section intitulée « getCallerInfo »Extrait les informations de frame de pile pour le site appelant. Utilisé en interne par
Formats.detailed et les tokens de format {file} / {line}.
import { getCallerInfo } from "mime-logger";
const { file, line, function: fn, caller } = getCallerInfo({ skipFrames: 1 });L’instance ColorHelpers utilisée par tous les présets intégrés.
import { colors } from "mime-logger";console.log(colors.green("ok"));