docs(log): document formatters module (#6073)

This commit is contained in:
Asher Gomez 2024-09-29 16:38:09 +10:00 committed by GitHub
parent 106b7077d8
commit 361be4569d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 5 deletions

View File

@ -76,6 +76,7 @@ const ENTRY_POINTS = [
"../log/debug.ts",
"../log/error.ts",
"../log/info.ts",
"../log/formatters.ts",
"../media_types/mod.ts",
"../msgpack/mod.ts",
"../net/mod.ts",

View File

@ -2,6 +2,33 @@
// This module is browser compatible.
import type { LogRecord } from "./logger.ts";
/**
* JSON log formatter.
*
* @example Usage
* ```ts
* import { LogRecord } from "@std/log/logger";
* import { jsonFormatter } from "@std/log/formatters";
* import { LogLevels } from "@std/log/levels";
* import { assertEquals } from "@std/assert/equals";
*
* const record = new LogRecord({
* msg: "Hello, world!",
* args: ["foo", "bar"],
* level: LogLevels.INFO,
* loggerName: "example",
* });
* const formatted = jsonFormatter(record);
*
* assertEquals(
* formatted,
* `{"level":"INFO","datetime":${record.datetime.getTime()},"message":"Hello, world!","args":["foo","bar"]}`,
* );
* ```
*
* @param logRecord Log record to format.
* @returns JSON string representation of the log record.
*/
export function jsonFormatter(logRecord: LogRecord): string {
return JSON.stringify({
level: logRecord.levelName,
@ -19,8 +46,5 @@ function flattenArgs(args: unknown[]): unknown {
}
}
export const formatters: {
jsonFormatter(logRecord: LogRecord): string;
} = {
jsonFormatter,
};
/** Formatters for log records. */
export const formatters = { jsonFormatter } as const;