rework config and logger

This commit is contained in:
mightypanders 2023-04-16 02:02:15 +02:00
parent e17c7601da
commit f5be22fb6f
2 changed files with 52 additions and 33 deletions

View File

@ -1,7 +1,27 @@
import dotenv from "dotenv" import dotenv from "dotenv"
dotenv.config() dotenv.config()
export const config = { interface options {
[k: string]: boolean | number | string | undefined
}
interface bodyParserOptions {
urlEncodedOptions: options,
jsonOptions: options
}
export interface Config {
server: { bodyParser: bodyParserOptions },
bot: {
debug: boolean
silent: boolean
token: string
guild_id: string
client_id: string
jellfin_token: string
jellyfin_url: string
port: number
}
}
export const config: Config = {
server: { server: {
bodyParser: { bodyParser: {
urlEncodedOptions: { urlEncodedOptions: {
@ -19,12 +39,14 @@ export const config = {
} }
} }
}, },
bot: {
debug: true, debug: true,
port: 1234,
silent: false, silent: false,
port: 1234,
token: process.env.BOT_TOKEN ?? "", token: process.env.BOT_TOKEN ?? "",
guild_id: process.env.GUILD_ID ?? "", guild_id: process.env.GUILD_ID ?? "",
client_id: process.env.CLIENT_ID ?? "", client_id: process.env.CLIENT_ID ?? "",
jellfin_token: process.env.JELLYFIN_TOKEN ?? "", jellfin_token: process.env.JELLYFIN_TOKEN ?? "",
jellyfin_url: process.env.JELLYFIN_URL ?? "" jellyfin_url: process.env.JELLYFIN_URL ?? ""
} }
}

View File

@ -3,9 +3,7 @@ import { config } from "./configuration"
const printFn = format.printf(({ guildId, level, message, errorCode, requestId, timestamp: logTimestamp }: { [k: string]: string }) => { const printFn = format.printf(({ guildId, level, message, errorCode, requestId, timestamp: logTimestamp }: { [k: string]: string }) => {
return `[${guildId}][${level}][${logTimestamp}][${errorCode return `[${guildId ?? ''}][${level}][${logTimestamp}][${errorCode ?? ''}][${requestId ?? ''}]:${message}`
? `[${errorCode}]` : `[]`}][${requestId
? `[${requestId}]` : `[]`}]:${message}`
}) })
const logFormat = format.combine( const logFormat = format.combine(
@ -18,10 +16,9 @@ const consoleTransports = [
format: logFormat format: logFormat
}) })
] ]
const logger = createLogger({ export const logger = createLogger({
level: config.debug ? 'debug' : 'info', level: config.bot.debug ? 'debug' : 'info',
format: logFormat, format: logFormat,
silent: config.silent, silent: config.bot.silent,
transports: consoleTransports transports: consoleTransports
}) })
logger.info("text", { requestId: "2", guildId: "asd" })