From e0d5f8df18e72b2e6407b2283db9d5809f32d3b1 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:42:40 +0100 Subject: [PATCH] feat: bring back seq for the bot --- bot/src/config.ts | 5 +++++ bot/src/utils/logger.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/bot/src/config.ts b/bot/src/config.ts index fec146626..83b8adcdb 100644 --- a/bot/src/config.ts +++ b/bot/src/config.ts @@ -106,6 +106,8 @@ export interface ProcessEnvironment { nodeEnv: "dev" | "production" | "staging" | "test"; mongoUrl: string; port: integer; + seqUrl?: string; + seqApiKey?: string; } function ParseEnvVars() { @@ -123,6 +125,7 @@ function ParseEnvVars() { p.isPositiveInteger(Number(self)) === true || "Should be a string representing a whole integer port.", SEQ_URL: "*string", + SEQ_API_KEY: "*string", }, {}, { allowExcessKeys: true } @@ -138,6 +141,8 @@ function ParseEnvVars() { nodeEnv: process.env.NODE_ENV, mongoUrl: process.env.MONGO_URL, port: Number(process.env.PORT), + seqUrl: process.env.SEQ_URL, + seqApiKey: process.env.SEQ_API_KEY, } as ProcessEnvironment; } diff --git a/bot/src/utils/logger.ts b/bot/src/utils/logger.ts index c30cd4a0c..c2681f7e8 100644 --- a/bot/src/utils/logger.ts +++ b/bot/src/utils/logger.ts @@ -1,10 +1,44 @@ +import { Transport as SeqTransport } from "@valuabletouch/winston-seq"; +import { ProcessEnv } from "config"; import { CreateLogger } from "mei-logger"; import { transports } from "winston"; import type { LoggerLayers } from "../data/data"; import type { MeiLogger } from "mei-logger"; +import type { SeqLogLevel } from "seq-logging"; const tports: Array = [new transports.Console({})]; +if (ProcessEnv.seqUrl) { + // Turns winston log levels into seq format. + const levelMap: Record = { + crit: "Fatal", + severe: "Error", + error: "Error", + warn: "Warning", + info: "Information", + + // Note that Seq interprets these in reverse, + // however, it's easier to read this code if I just + // use the same levels, instead of the right ones. + verbose: "Verbose", + debug: "Debug", + }; + + tports.push( + new SeqTransport({ + apiKey: ProcessEnv.seqApiKey, + serverUrl: ProcessEnv.seqUrl, + onError: (err) => { + // eslint-disable-next-line no-console + console.error(`Failed to send seq message: ${err.message}.`); + }, + levelMapper(level = "") { + return levelMap[level] ?? "Information"; + }, + }) + ); +} + const logger = CreateLogger(`tachi-bot`, undefined, tports); export default logger;