From 37f61f82997f623cbde2531e42bdd698d9822d20 Mon Sep 17 00:00:00 2001 From: c-wide <53365073+c-wide@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:28:33 -0500 Subject: [PATCH] chore: use separated pkg --- features/logs/server/logger.ts | 9 ++++- features/logs/server/transport.ts | 66 ------------------------------- package.json | 4 +- pnpm-lock.yaml | 15 +++++-- 4 files changed, 21 insertions(+), 73 deletions(-) delete mode 100644 features/logs/server/transport.ts diff --git a/features/logs/server/logger.ts b/features/logs/server/logger.ts index debcf5f..3164680 100644 --- a/features/logs/server/logger.ts +++ b/features/logs/server/logger.ts @@ -1,7 +1,7 @@ import { createLogger, transports, format } from "winston"; import { config } from "~/utils/common/config"; import { LogColor, type LogMetadata } from "~/logs/common/misc"; -import { FivemanageTransport } from "~/logs/server/transport"; +import { FivemanageTransport } from "@fivemanage/winston"; import { getFormattedPlayerIdentifiers } from "~/utils/server/identifiers"; import { minLength, @@ -13,6 +13,7 @@ import { ValiError, flatten, } from "valibot"; +import { convars } from "~/utils/server/convars"; const levels = config.logs.levels.reduce>( (acc, curr, idx) => { @@ -45,7 +46,11 @@ if (config.logs.console === true) { } if (config.logs.enableCloudLogging === true) { - logger.add(new FivemanageTransport()); + logger.add( + new FivemanageTransport({ + apiKey: convars.FIVEMANAGE_LOGS_API_KEY, + }), + ); } const LogSchema = object({ diff --git a/features/logs/server/transport.ts b/features/logs/server/transport.ts deleted file mode 100644 index 71d3425..0000000 --- a/features/logs/server/transport.ts +++ /dev/null @@ -1,66 +0,0 @@ -import fetch from "node-fetch"; -import Transport from "winston-transport"; -import type { Log } from "~/logs/common/misc"; -import { getErrorMessage, setImmediateInterval } from "~/utils/common/misc"; -import { convars } from "~/utils/server/convars"; - -export class FivemanageTransport extends Transport { - private readonly apiUrl = "https://api.fivemanage.com/api/logs/batch"; - private batch: Array = []; - private readonly batchInterval = 5000; - private readonly batchCount = 10; - - constructor(opts?: Transport.TransportStreamOptions) { - super(opts); - - this.startInterval(); - } - - startInterval() { - setImmediateInterval(async () => { - await this.processBatch(); - }, this.batchInterval); - } - - async processBatch() { - if (this.batch.length === 0) return; - - const batch = this.batch; - this.batch = []; - - try { - const res = await fetch(this.apiUrl, { - method: "POST", - body: JSON.stringify(batch), - headers: { - "Content-Type": "application/json", - Authorization: convars.FIVEMANAGE_LOGS_API_KEY, - }, - }); - - if (res.ok === false) { - throw new Error("Failed to upload logs to Fivemanage"); - } - } catch (error) { - console.error(`Failed to process log batch: ${getErrorMessage(error)}`); - - // re-add the batch to be processed again - this.batch.concat(batch); - } - } - - log(info: Log, next: () => void): void { - this.batch.push({ - level: info.level, - message: info.message, - resource: info.resource, - metadata: info.metadata, - }); - - if (this.batch.length >= this.batchCount) { - this.processBatch(); - } - - next(); - } -} diff --git a/package.json b/package.json index 94887a2..24bdbb9 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,13 @@ "typescript": "^5.3.3" }, "dependencies": { + "@fivemanage/winston": "^0.1.0", "@paralleldrive/cuid2": "^2.2.2", "form-data": "^4.0.0", "lru-cache": "^10.2.0", "node-fetch": "2", "valibot": "^0.29.0", - "winston": "^3.11.0", - "winston-transport": "^4.7.0" + "winston": "^3.11.0" }, "packageManager": "pnpm@8.15.3" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8813c99..6277449 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + '@fivemanage/winston': + specifier: ^0.1.0 + version: 0.1.0 '@paralleldrive/cuid2': specifier: ^2.2.2 version: 2.2.2 @@ -23,9 +26,6 @@ dependencies: winston: specifier: ^3.11.0 version: 3.11.0 - winston-transport: - specifier: ^4.7.0 - version: 4.7.0 devDependencies: '@biomejs/biome': @@ -374,6 +374,15 @@ packages: dev: true optional: true + /@fivemanage/winston@0.1.0: + resolution: {integrity: sha512-zucCcGgQ9c8SBoTETaUt2iPwXC3KFe4xQMTihtPtx6ZIzkM5C97dDOtDWnqCEAPPd66QchTskhkY46mLlAeiAg==} + dependencies: + node-fetch: 2.7.0 + winston-transport: 4.7.0 + transitivePeerDependencies: + - encoding + dev: false + /@noble/hashes@1.3.3: resolution: {integrity: sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==} engines: {node: '>= 16'}