refactor(logging): share local iso timestamp format
This commit is contained in:
@@ -2,6 +2,7 @@ import type { Command } from "commander";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import { buildGatewayConnectionDetails } from "../gateway/call.js";
|
||||
import { parseLogLine } from "../logging/parse-log-line.js";
|
||||
import { formatLocalIsoWithOffset } from "../logging/timestamps.js";
|
||||
import { formatDocsLink } from "../terminal/links.js";
|
||||
import { clearActiveProgressLine } from "../terminal/progress-line.js";
|
||||
import { createSafeStreamWriter } from "../terminal/stream-writer.js";
|
||||
@@ -73,21 +74,6 @@ export function formatLogTimestamp(
|
||||
return value;
|
||||
}
|
||||
|
||||
const formatLocalIsoWithOffset = (now: Date) => {
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
const h = String(now.getHours()).padStart(2, "0");
|
||||
const m = String(now.getMinutes()).padStart(2, "0");
|
||||
const s = String(now.getSeconds()).padStart(2, "0");
|
||||
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
||||
const tzOffset = now.getTimezoneOffset();
|
||||
const tzSign = tzOffset <= 0 ? "+" : "-";
|
||||
const tzHours = String(Math.floor(Math.abs(tzOffset) / 60)).padStart(2, "0");
|
||||
const tzMinutes = String(Math.abs(tzOffset) % 60).padStart(2, "0");
|
||||
return `${year}-${month}-${day}T${h}:${m}:${s}.${ms}${tzSign}${tzHours}:${tzMinutes}`;
|
||||
};
|
||||
|
||||
let timeString: string;
|
||||
if (localTime) {
|
||||
timeString = formatLocalIsoWithOffset(parsed);
|
||||
|
||||
@@ -7,6 +7,7 @@ import { readLoggingConfig } from "./config.js";
|
||||
import { type LogLevel, normalizeLogLevel } from "./levels.js";
|
||||
import { getLogger, type LoggerSettings } from "./logger.js";
|
||||
import { loggingState } from "./state.js";
|
||||
import { formatLocalIsoWithOffset } from "./timestamps.js";
|
||||
|
||||
export type ConsoleStyle = "pretty" | "compact" | "json";
|
||||
type ConsoleSettings = {
|
||||
@@ -157,18 +158,7 @@ export function formatConsoleTimestamp(style: ConsoleStyle): string {
|
||||
const s = String(now.getSeconds()).padStart(2, "0");
|
||||
return `${h}:${m}:${s}`;
|
||||
}
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
const h = String(now.getHours()).padStart(2, "0");
|
||||
const m = String(now.getMinutes()).padStart(2, "0");
|
||||
const s = String(now.getSeconds()).padStart(2, "0");
|
||||
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
||||
const tzOffset = now.getTimezoneOffset();
|
||||
const tzSign = tzOffset <= 0 ? "+" : "-";
|
||||
const tzHours = String(Math.floor(Math.abs(tzOffset) / 60)).padStart(2, "0");
|
||||
const tzMinutes = String(Math.abs(tzOffset) % 60).padStart(2, "0");
|
||||
return `${year}-${month}-${day}T${h}:${m}:${s}.${ms}${tzSign}${tzHours}:${tzMinutes}`;
|
||||
return formatLocalIsoWithOffset(now);
|
||||
}
|
||||
|
||||
function hasTimestampPrefix(value: string): boolean {
|
||||
|
||||
14
src/logging/timestamps.ts
Normal file
14
src/logging/timestamps.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export function formatLocalIsoWithOffset(now: Date): string {
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(now.getDate()).padStart(2, "0");
|
||||
const h = String(now.getHours()).padStart(2, "0");
|
||||
const m = String(now.getMinutes()).padStart(2, "0");
|
||||
const s = String(now.getSeconds()).padStart(2, "0");
|
||||
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
||||
const tzOffset = now.getTimezoneOffset();
|
||||
const tzSign = tzOffset <= 0 ? "+" : "-";
|
||||
const tzHours = String(Math.floor(Math.abs(tzOffset) / 60)).padStart(2, "0");
|
||||
const tzMinutes = String(Math.abs(tzOffset) % 60).padStart(2, "0");
|
||||
return `${year}-${month}-${day}T${h}:${m}:${s}.${ms}${tzSign}${tzHours}:${tzMinutes}`;
|
||||
}
|
||||
Reference in New Issue
Block a user