Skip to content

Commit deb5f68

Browse files
Its4Nikimgbot[bot]ImgBotApp
authored
Dev (#68)
* chore(api): Add startup animation, logger config, and API dev script (#63) (#64) * chore(api): Add startup animation, logger config, and API dev script (#63) * docs: sync from Outline * 66 logger x docker client the current workers do not forward their logs (#67) * refactor(logging): Standardize cross-process log handling and improve UI Introduces a centralized logging mechanism to forward log entries generated by workers to the main process. This ensures all application logs are consistently processed and displayed. - **Logger:** Made `Logger.emitLogEntry` public and enhanced it to accept metadata for accurate log re-emission from various sources. Added a visual indicator for log hooks in formatted messages. - **Docker Client:** - Implemented `WorkerLogger` in `docker-client` workers to proxy log entries via a new `__log__` event. - The `docker-client` manager now listens for and re-emits `__log__` events through the main logger. - Refactored worker-related types (`InboundMessage`, `InitMessage`, `MetricsMessage`) and utility functions (`tryBuildFromProxy`) into dedicated shared files for better organization. - **Typings:** Defined `LogLevel`, `LogEntry` types, and added the `__log__` event to the `EVENTS` interface to support structured log forwarding. - **UI:** Updated the `Sidebar` to display the unified log stream with improved formatting, including colored log levels, badges for logger names/request IDs, and a new `log` timestamp format. - **Utils:** Added a new `log` format to `formatDate` and standardized the default locale for date formatting to `de-DE`. - **Dependencies:** Upgraded `@dockstat/docker-client` to `2.0.0` and numerous other dependencies (e.g., `framer-motion`, `react-router`, `@types/node`) to their latest patch/minor versions. - **Cleanup:** Removed a debug `console.log` statement from `plugin-handler`. * refactor(logging, client): improve logging detail and worker robustness Update .env.example with refined default logger configurations to reduce noise and focus on `PluginHandler` messages. Forward `requestId` within `__log__` events to enhance log correlation. Strengthen worker initialization checks by explicitly verifying `message.success`. Streamline event typings by removing `workerLoggerResponse`. Includes minor import reordering and variable name correction for consistency. * [ImgBot] Optimize images (#65) * chore(api): Add startup animation, logger config, and API dev script (#63) * docs: sync from Outline * [ImgBot] Optimize images *Total -- 389.34kb -> 261.94kb (32.72%) /.github/assets/logos/square.png -- 81.10kb -> 20.42kb (74.82%) /.github/assets/gifs/DockStatAPI-startup.gif -- 306.82kb -> 240.10kb (21.74%) /packages/ui/public/vite.svg -- 1.42kb -> 1.42kb (0.07%) Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com> * chore(assets): Add and organize GitHub assets, update README logo --------- Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com> Co-authored-by: ItsNik <info@itsnik.de> Co-authored-by: Its4Nik <106100177+Its4Nik@users.noreply.github.com> Co-authored-by: ImgBotApp <ImgBotHelp@gmail.com> * chore(gifs): Add .gitignore to ignore YML files --------- Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com> Co-authored-by: imgbot[bot] <31301654+imgbot[bot]@users.noreply.github.com> Co-authored-by: ImgBotApp <ImgBotHelp@gmail.com>
1 parent 58bc310 commit deb5f68

File tree

19 files changed

+175
-81
lines changed

19 files changed

+175
-81
lines changed

.github/assets/gifs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.yml
-33.7 KB
Loading

.github/assets/logos/square.png

-60.7 KB
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<img src="./.github/DockStat2-04.png" alt="DockStat Logo" />
2+
<img src="./.github/assets/logos/withText-white.png" alt="DockStat Logo" />
33
</p>
44

55
<p align="center">

apps/api/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
DOCKSTAT_LOGGER_DISABLED_LOGGERS=""
2-
DOCKSTAT_LOGGER_ONLY_SHOW=""
3-
DOCKSTAT_LOGGER_IGNORE_MESSAGES="Logger Status: active"
2+
DOCKSTAT_LOGGER_ONLY_SHOW="PluginHandler"
3+
DOCKSTAT_LOGGER_IGNORE_MESSAGES="Logger Status: active,Proxying Event (__log__) to DCM,QueryBuilder initialized for table"
44
DOCKSTAT_LOGGER_FULL_FILE_PATH=false
55
DOCKSTAT_API_PORT="3030"
66
DOCKSTAT_LOGGER_LEVEL="INFO"

bun.lock

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docker-client/src/_worker.index.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import Logger from "@dockstat/logger"
22
import DB from "@dockstat/sqlite-wrapper"
3-
import type { DOCKER } from "@dockstat/typings"
43
import DockerClient, { type DockerClientInstance } from "./client"
54
import { proxyEvent } from "./events/workerEventProxy"
65
import type { WorkerRequest, WorkerResponse } from "./shared/types"
6+
import type { InboundMessage } from "./shared/worker"
77

8-
declare const self: Worker
9-
10-
type InitMessage = {
11-
type: "__init__"
12-
dbPath: string
13-
clientId: number
14-
clientName: string
15-
options: DOCKER.DockerAdapterOptions
16-
}
8+
const WorkerLogger = new Logger("DCW", [], (entry) => {
9+
proxyEvent("__log__", entry)
10+
})
1711

18-
type MetricsMessage = {
19-
type: "__get_metrics__"
20-
}
21-
22-
type InboundMessage = WorkerRequest | InitMessage | MetricsMessage
12+
declare const self: Worker
2313

2414
let client: DockerClientInstance | null = null
2515
let clientId = -1
@@ -40,7 +30,7 @@ self.onmessage = async (event: MessageEvent<InboundMessage>) => {
4030
clientId = Number(msg.clientId)
4131
clientName = msg.clientName
4232

43-
const logger = new Logger(`DockerClient-${clientId}`)
33+
const logger = WorkerLogger.spawn(`DC-${clientId}`)
4434
client = new DockerClient(clientId, clientName, db, msg.options, logger)
4535

4636
// Initialize with whatever is already in the host table; explicit init with hosts can come later.

packages/docker-client/src/manager/core.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { heapStats } from "bun:jsc"
22
import type Logger from "@dockstat/logger"
3+
import type { LogEntry } from "@dockstat/logger"
34
import type PluginHandler from "@dockstat/plugin-handler"
45
import { column, type QueryBuilder } from "@dockstat/sqlite-wrapper"
56
import type { DOCKER, EVENTS } from "@dockstat/typings"
67
import type { buildMessageFromProxyRes } from "@dockstat/typings/types"
7-
import { truncate, worker as workerUtils } from "@dockstat/utils"
8-
8+
import { truncate } from "@dockstat/utils"
99
import type { PoolMetrics, WorkerMetrics, WorkerRequest } from "../shared/types"
10-
1110
import type {
1211
DBType,
1312
DockerClientTable,
@@ -16,6 +15,7 @@ import type {
1615
WorkerWrapper,
1716
} from "./types"
1817
import { isInitCompleteMessage, looksLikeEventMessage } from "./types"
18+
import { tryBuildFromProxy } from "./utils/buildFromProxy"
1919
import { sendWorkerMessage } from "./utils/sendWorkerMessage"
2020

2121
export class DockerClientManagerCore {
@@ -238,7 +238,7 @@ export class DockerClientManagerCore {
238238

239239
this.workers.set(clientId, wrapper)
240240

241-
// Persistent plugin event listener
241+
// Persistent event listener
242242
this.attachEventListener(wrapper)
243243

244244
// Initialize worker
@@ -268,6 +268,10 @@ export class DockerClientManagerCore {
268268
const initHandler = (event: MessageEvent) => {
269269
const message = event.data
270270

271+
if (message?.type === "__event__") {
272+
return
273+
}
274+
271275
this.logger.debug(`Init handler received: ${JSON.stringify(message)}`)
272276

273277
if (!isInitCompleteMessage(message)) {
@@ -282,7 +286,7 @@ export class DockerClientManagerCore {
282286
this.logger.info(`Worker ${clientId} initialized successfully`)
283287
resolve()
284288
} else {
285-
wrapper.lastError = message.error ?? "Unknown init error"
289+
wrapper.lastError = (message as { error: string }).error ?? "Unknown init error"
286290
wrapper.errorCount += 1
287291

288292
try {
@@ -292,7 +296,9 @@ export class DockerClientManagerCore {
292296
}
293297
this.workers.delete(clientId)
294298

295-
reject(new Error(`Worker init failed: ${message.error ?? "unknown"}`))
299+
reject(
300+
new Error(`Worker init failed: ${(message as { error: string }).error ?? "unknown"}`)
301+
)
296302
}
297303
}
298304

@@ -492,21 +498,17 @@ export class DockerClientManagerCore {
492498

493499
const { worker } = wrapper
494500

495-
const tryBuildFromProxy = (payload: unknown): EventMessage<keyof EVENTS> | null => {
496-
const message = workerUtils.buildMessage.tryBuildMessageFromProxy(payload)
497-
if (!message) return null
498-
499-
return message
500-
}
501-
502501
const listener = (event: MessageEvent) => {
503502
const payload = event.data
504503

505504
const message: EventMessage<keyof EVENTS> | null = looksLikeEventMessage(payload)
506505
? payload
507506
: tryBuildFromProxy(payload)
508507

509-
if (!message) return
508+
if (!message) {
509+
this.logger.debug(`No message! ${JSON.stringify(payload)}`)
510+
return null
511+
}
510512

511513
this.internalListeners(wrapper, message)
512514
this.triggerHooks(message)
@@ -535,6 +537,13 @@ export class DockerClientManagerCore {
535537
wrapper.hostIds.delete(hostId)
536538
break
537539
}
540+
541+
case "__log__": {
542+
const { level, message, caller, name, parents, timestamp, requestId }: LogEntry =
543+
msg.ctx as Parameters<EVENTS[typeof msg.type]>[0]
544+
545+
this.logger.emitLogEntry(level, message, { caller, name, parents, timestamp, requestId })
546+
}
538547
}
539548
}
540549

@@ -560,6 +569,10 @@ export class DockerClientManagerCore {
560569
return
561570
}
562571

572+
if (message.type === "__log__") {
573+
return
574+
}
575+
563576
this.logger.debug(
564577
`Triggering hooks for event "${String(message.type)} - ctx: ${truncate(JSON.stringify(message.ctx), 100)}"`
565578
)

packages/docker-client/src/manager/hosts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DATABASE } from "@dockstat/typings"
2+
import { truncate } from "@dockstat/utils"
23
import { DockerClientManagerCore } from "./core"
34

45
export class Hosts extends DockerClientManagerCore {
@@ -95,7 +96,7 @@ export class Hosts extends DockerClientManagerCore {
9596
public override async getAllHosts() {
9697
this.logger.debug("Getting all hosts")
9798
const clients = this.getAllClients()
98-
this.logger.debug(`Clients: ${JSON.stringify(clients)}`)
99+
this.logger.debug(`Clients: ${truncate(JSON.stringify(clients), 100)}`)
99100

100101
try {
101102
const allClientHosts = await Promise.all(

0 commit comments

Comments
 (0)