From e4dd9bff2eeef67db4ae3c8cd290e5c589bba711 Mon Sep 17 00:00:00 2001 From: Alexey Prokhorov Date: Thu, 25 Jul 2024 20:44:05 +0500 Subject: [PATCH] feat(docs): Update format documentation, fixes #431 --- docs/transports/format.md | 26 ++++++++++++++++++++++---- src/index.d.ts | 10 +++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/docs/transports/format.md b/docs/transports/format.md index e64ffaf..06f185b 100644 --- a/docs/transports/format.md +++ b/docs/transports/format.md @@ -32,10 +32,28 @@ log.transports.console.format = '[{h}:{i}:{s}.{ms}] [{label}] {text}'; ## Function -`(message: LogMessage) => string` +`(params: FormatParams) => any[]` + +```ts +interface FormatParams { + data: any[]; + level: LogLevel; + logger: Logger; + message: LogMessage; + transport: Transport; +} +``` ```js -log.transports.console.format = ({ message }) => { - return util.format(...message.data); -} +import util from 'node:util'; + +log.transports.console.format = ({ data, level, message }) => { + const text = util.format(...data); + + return [ + message.date.toISOString().slice(11, -1), + `[${level}]`, + text + ]; +}; ``` diff --git a/src/index.d.ts b/src/index.d.ts index 9949f08..c293e5c 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -6,7 +6,15 @@ declare namespace Logger { 'silly'; type LevelOption = LogLevel | false; - type Format = (({ message: LogMessage }) => any[]) | string; + interface FormatParams { + data: any[]; + level: LogLevel; + logger: Logger; + message: LogMessage; + transport: Transport; + } + + type Format = string | ((params: FormatParams) => any[]); type FOpenFlags = 'r' | 'r+' | 'rs+' | 'w' | 'wx' | 'w+' | 'wx+' | 'a' | 'ax' | 'a+' | 'ax+';