// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import type { LogRecord } from "./logger.ts";

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;
  }
}

export const formatters: {
  jsonFormatter(logRecord: LogRecord): string;
} = {
  jsonFormatter,
};

// denoCacheMetadata={"headers":{"content-type":"text/typescript","access-control-allow-origin":"*","access-control-expose-headers":"*","x-jsr-backend":"modules","etag":"\"53b9809060328d614885f5ad14964e5e\"","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","vary":"Origin","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","x-robots-tag":"noindex","server":"cloudflare","cross-origin-resource-policy":"cross-origin","x-content-type-options":"nosniff","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=lDBWuQbjACcZYy3F7gKQwhTaPh%2Br2ZFlzHLQdTyec%2BzM6xFIqlbbexdfDDvGJipqV4PdZm02u3frhk4Zt5yITZ%2BqVNUljajkJ%2F20N7K6wqn5D5c2TXVcpv0%3D\"}]}","cf-ray":"a223e657699a7db6-LAX","set-cookie":"__cf_bm=lmncwaJBh9GZdpg0okWH5ncitthFl8BvottNebgdQYs-1785241875.1103175-1.0.1.1-tZnnx.bH5Z9LvtQXlSZTzj0O3Jg1EcoLk1orpGCuJp8vLTJ74G66hI9iJwN.Q35FpjO0csoMXvsb4GSJFu5hCPZPJknqgOJL0WiYqCHFd1LZmPiH7iAfB8ToEXKShuei; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","date":"Tue, 28 Jul 2026 12:31:15 GMT","content-length":"652","alt-svc":"h3=\":443\"; ma=86400","cache-control":"public, max-age=31536000, immutable"},"url":"https://jsr.io/@std/log/0.224.7/formatters.ts","time":1785241875}