mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// 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,
|
|
datetime: logRecord.datetime.getTime(),
|
|
message: logRecord.msg,
|
|
args: flattenArgs(logRecord.args),
|
|
});
|
|
}
|
|
|
|
function flattenArgs(args: unknown[]): unknown {
|
|
if (args.length === 1) {
|
|
return args[0];
|
|
} else if (args.length > 1) {
|
|
return args;
|
|
}
|
|
}
|
|
|
|
/** Formatters for log records. */
|
|
export const formatters = { jsonFormatter } as const;
|