From 82fba39684418356152ef0bd5001a2f185d89881 Mon Sep 17 00:00:00 2001 From: dswbx Date: Sat, 22 Feb 2025 13:17:43 +0100 Subject: [PATCH 1/2] added timestamps to app console logs --- app/src/App.ts | 4 +++- app/src/core/console.ts | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/src/App.ts b/app/src/App.ts index 8765a13..fa79ebc 100644 --- a/app/src/App.ts +++ b/app/src/App.ts @@ -14,7 +14,7 @@ import * as SystemPermissions from "modules/permissions"; import { AdminController, type AdminControllerOptions } from "modules/server/AdminController"; import { SystemController } from "modules/server/SystemController"; -// biome-ignore +// biome-ignore format: must be there import { Api, type ApiOptions } from "Api"; export type AppPlugin = (app: App) => Promise | void; @@ -122,6 +122,8 @@ export class App { this.trigger_first_boot = false; await this.emgr.emit(new AppFirstBoot({ app: this })); } + + $console.log("App built"); } mutateConfig(module: Module) { diff --git a/app/src/core/console.ts b/app/src/core/console.ts index 6bcb637..ced396c 100644 --- a/app/src/core/console.ts +++ b/app/src/core/console.ts @@ -1,3 +1,4 @@ +import { datetimeStringLocal } from "core/utils"; import colors from "picocolors"; function hasColors() { @@ -8,10 +9,10 @@ function hasColors() { env = p.env || {}; return ( !(!!env.NO_COLOR || argv.includes("--no-color")) && - // biome-ignore lint/complexity/useOptionalChain: (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || + // biome-ignore lint/complexity/useOptionalChain: ((p.stdout || {}).isTTY && env.TERM !== "dumb") || !!env.CI) ); @@ -43,19 +44,17 @@ function __tty(type: any, args: any[]) { prefix: colors.cyan }, log: { - prefix: colors.gray + prefix: colors.dim }, debug: { prefix: colors.yellow } } as const; - const prefix = styles[type].prefix( - `[${type.toUpperCase()}]${has ? " ".repeat(5 - type.length) : ""}` - ); + const prefix = styles[type].prefix(`[${type.toUpperCase()}]`); const _args = args.map((a) => "args" in styles[type] && has && typeof a === "string" ? styles[type].args(a) : a ); - return originalConsoles[type](prefix, ..._args); + return originalConsoles[type](prefix, colors.gray(datetimeStringLocal()), ..._args); } export type TConsoleSeverity = keyof typeof originalConsoles; From 147d6b7ff719a298d225a6de257b780452e49c63 Mon Sep 17 00:00:00 2001 From: dswbx Date: Sat, 22 Feb 2025 13:18:23 +0100 Subject: [PATCH 2/2] added timestamps to app console logs --- app/__test__/core/utils.spec.ts | 12 +++++++++++- app/src/core/utils/dates.ts | 17 +++++++++++++++++ app/src/modules/server/SystemController.ts | 18 ++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/__test__/core/utils.spec.ts b/app/__test__/core/utils.spec.ts index 3de8a48..8b8a185 100644 --- a/app/__test__/core/utils.spec.ts +++ b/app/__test__/core/utils.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { Perf, isBlob, ucFirst } from "../../src/core/utils"; +import { Perf, datetimeStringUTC, isBlob, ucFirst } from "../../src/core/utils"; import * as utils from "../../src/core/utils"; async function wait(ms: number) { @@ -245,4 +245,14 @@ describe("Core Utils", async () => { } }); }); + + describe("dates", () => { + test.only("formats local time", () => { + expect(utils.datetimeStringUTC("2025-02-21T16:48:25.841Z")).toBe("2025-02-21 16:48:25"); + console.log(utils.datetimeStringUTC(new Date())); + console.log(utils.datetimeStringUTC()); + console.log(new Date()); + console.log("timezone", Intl.DateTimeFormat().resolvedOptions().timeZone); + }); + }); }); diff --git a/app/src/core/utils/dates.ts b/app/src/core/utils/dates.ts index c33b496..4628004 100644 --- a/app/src/core/utils/dates.ts +++ b/app/src/core/utils/dates.ts @@ -11,4 +11,21 @@ declare module "dayjs" { dayjs.extend(weekOfYear); +export function datetimeStringLocal(dateOrString?: string | Date | undefined): string { + return dayjs(dateOrString).format("YYYY-MM-DD HH:mm:ss"); +} + +export function datetimeStringUTC(dateOrString?: string | Date | undefined): string { + const date = dateOrString ? new Date(dateOrString) : new Date(); + return date.toISOString().replace("T", " ").split(".")[0]!; +} + +export function getTimezoneOffset(): number { + return new Date().getTimezoneOffset(); +} + +export function getTimezone(): string { + return Intl.DateTimeFormat().resolvedOptions().timeZone; +} + export { dayjs }; diff --git a/app/src/modules/server/SystemController.ts b/app/src/modules/server/SystemController.ts index 79b9064..728a73f 100644 --- a/app/src/modules/server/SystemController.ts +++ b/app/src/modules/server/SystemController.ts @@ -2,7 +2,15 @@ import type { App } from "App"; import { tbValidator as tb } from "core"; -import { StringEnum, Type, TypeInvalidError } from "core/utils"; +import { + StringEnum, + Type, + TypeInvalidError, + datetimeStringLocal, + datetimeStringUTC, + getTimezone, + getTimezoneOffset +} from "core/utils"; import { getRuntimeKey } from "core/utils"; import type { Context, Hono } from "hono"; import { Controller } from "modules/Controller"; @@ -273,7 +281,13 @@ export class SystemController extends Controller { hono.get("/info", (c) => c.json({ version: c.get("app")?.version(), - runtime: getRuntimeKey() + runtime: getRuntimeKey(), + timezone: { + name: getTimezone(), + offset: getTimezoneOffset(), + local: datetimeStringLocal(), + utc: datetimeStringUTC() + } }) );