@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 4.46 kB
Source Map (JSON)
{"version":3,"file":"formatters.mjs","names":[],"sources":["../../../../src/libs/logger/console-transport/formatters.ts"],"sourcesContent":["import type { LogEntry, LogEntryLevel } from \"../types.js\";\n\nexport const consoleColors = {\n\treset: \"\\x1b[0m\",\n\tdim: \"\\x1b[2m\",\n\terror: \"\\x1b[31m\",\n\twarn: \"\\x1b[33m\",\n\tinfo: \"\\x1b[36m\",\n\tdebug: \"\\x1b[90m\",\n\thttp: \"\\x1b[35m\",\n\tsuccess: \"\\x1b[32m\",\n};\n\nexport const levelLabels: Record<LogEntryLevel, string> = {\n\terror: \"ERROR\",\n\twarn: \"WARN\",\n\tinfo: \"INFO\",\n\tdebug: \"DEBUG\",\n};\n\n/**\n * Routes each log level through the matching console output stream.\n */\nexport const getConsoleLogger = (\n\tlevel: LogEntryLevel,\n): Console[\"error\"] | Console[\"warn\"] | Console[\"info\"] | Console[\"debug\"] => {\n\tswitch (level) {\n\t\tcase \"error\":\n\t\t\treturn console.error;\n\t\tcase \"warn\":\n\t\t\treturn console.warn;\n\t\tcase \"info\":\n\t\t\treturn console.info;\n\t\tcase \"debug\":\n\t\t\treturn console.debug;\n\t}\n};\n\n/**\n * Extracts a short error message so normal console logs stay on one line.\n */\nexport const getErrorMessage = (entry: LogEntry): string | undefined => {\n\tif (entry.error instanceof Error) return entry.error.message;\n\tif (typeof entry.error === \"string\") return entry.error;\n\n\tif (\n\t\ttypeof entry.error === \"object\" &&\n\t\tentry.error !== null &&\n\t\t\"message\" in entry.error &&\n\t\ttypeof entry.error.message === \"string\"\n\t) {\n\t\treturn entry.error.message;\n\t}\n\n\tconst dataErrorMessage = entry.data?.errorMessage;\n\treturn typeof dataErrorMessage === \"string\" ? dataErrorMessage : undefined;\n};\n\n/**\n * Formats structured timestamps as compact local times for human output.\n */\nexport const formatTimestamp = (timestamp: string, enabled: boolean) => {\n\tif (!enabled) return undefined;\n\n\tconst date = new Date(timestamp);\n\tif (Number.isNaN(date.getTime())) return timestamp;\n\n\treturn [date.getHours(), date.getMinutes(), date.getSeconds()]\n\t\t.map((part) => String(part).padStart(2, \"0\"))\n\t\t.join(\":\");\n};\n\n/**\n * Keeps HTTP durations compact while retaining useful precision.\n */\nexport const formatDuration = (durationMs: number) => {\n\tif (durationMs < 1000) return `${Math.round(durationMs)}ms`;\n\treturn `${(durationMs / 1000).toFixed(2)}s`;\n};\n\n/**\n * Applies terminal color only when the active console supports it.\n */\nexport const colorize = (value: string, color: string, enabled: boolean) => {\n\treturn enabled ? `${color}${value}${consoleColors.reset}` : value;\n};\n\n/**\n * Builds an aligned prefix shared by standard and HTTP console entries.\n */\nexport const createPrefix = (props: {\n\tcolor: string;\n\tcolors: boolean;\n\tlabel: string;\n\tscope?: string;\n\ttimestamp?: string;\n}) => {\n\tconst parts = [];\n\n\tif (props.timestamp) {\n\t\tparts.push(colorize(props.timestamp, consoleColors.dim, props.colors));\n\t}\n\n\tparts.push(colorize(props.label.padEnd(5), props.color, props.colors));\n\n\tif (props.scope) {\n\t\tparts.push(colorize(props.scope, consoleColors.dim, props.colors));\n\t}\n\n\treturn parts.join(\" \");\n};\n"],"mappings":"AAEA,MAAa,EAAgB,CAC5B,MAAO,UACP,IAAK,UACL,MAAO,WACP,KAAM,WACN,KAAM,WACN,MAAO,WACP,KAAM,WACN,QAAS,UACV,EAEa,EAA6C,CACzD,MAAO,QACP,KAAM,OACN,KAAM,OACN,MAAO,OACR,EAKa,EACZ,GAC6E,CAC7E,OAAQ,EAAR,CACC,IAAK,QACJ,OAAO,QAAQ,MAChB,IAAK,OACJ,OAAO,QAAQ,KAChB,IAAK,OACJ,OAAO,QAAQ,KAChB,IAAK,QACJ,OAAO,QAAQ,KACjB,CACD,EAKa,EAAmB,GAAwC,CACvE,GAAI,EAAM,iBAAiB,MAAO,OAAO,EAAM,MAAM,QACrD,GAAI,OAAO,EAAM,OAAU,SAAU,OAAO,EAAM,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