mirror of
https://github.com/denoland/std.git
synced 2024-11-21 12:40:03 +00:00
docs(log): document formatters
module (#6073)
This commit is contained in:
parent
106b7077d8
commit
361be4569d
@ -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",
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user