mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor: ♻️ Rewrite logging logic into a unified package
This commit is contained in:
parent
e1bd389bf1
commit
aff51b651c
32 changed files with 479 additions and 402 deletions
144
utils/loggers.ts
144
utils/loggers.ts
|
|
@ -1,144 +0,0 @@
|
|||
import { mkdir } from "node:fs/promises";
|
||||
import { dirname } from "node:path";
|
||||
import { getRotatingFileSink } from "@logtape/file";
|
||||
import {
|
||||
configure,
|
||||
getConsoleSink,
|
||||
getLevelFilter,
|
||||
type LogLevel,
|
||||
type LogRecord,
|
||||
} from "@logtape/logtape";
|
||||
import { config } from "@versia-server/config";
|
||||
import chalk from "chalk";
|
||||
|
||||
// config.logging.log_file_path is a path to a file, create the directory if it doesn't exist
|
||||
await mkdir(dirname(config.logging.log_file_path), { recursive: true });
|
||||
|
||||
const levelAbbreviations: Record<LogLevel, string> = {
|
||||
debug: "DBG",
|
||||
info: "INF",
|
||||
warning: "WRN",
|
||||
error: "ERR",
|
||||
fatal: "FTL",
|
||||
trace: "TRC",
|
||||
};
|
||||
|
||||
/**
|
||||
* The styles for the log level in the console.
|
||||
*/
|
||||
const logLevelStyles: Record<LogLevel, (text: string) => string> = {
|
||||
debug: chalk.white.bgGray,
|
||||
info: chalk.black.bgWhite,
|
||||
warning: chalk.black.bgYellow,
|
||||
error: chalk.white.bgRed,
|
||||
fatal: chalk.white.bgRedBright,
|
||||
trace: chalk.white.bgBlue,
|
||||
};
|
||||
|
||||
/**
|
||||
* The default console formatter.
|
||||
*
|
||||
* @param record The log record to format.
|
||||
* @returns The formatted log record, as an array of arguments for
|
||||
* {@link console.log}.
|
||||
*/
|
||||
export function defaultConsoleFormatter(record: LogRecord): string[] {
|
||||
const msg = record.message.join("");
|
||||
const date = new Date(record.timestamp);
|
||||
const time = `${date.getUTCHours().toString().padStart(2, "0")}:${date
|
||||
.getUTCMinutes()
|
||||
.toString()
|
||||
.padStart(
|
||||
2,
|
||||
"0",
|
||||
)}:${date.getUTCSeconds().toString().padStart(2, "0")}.${date
|
||||
.getUTCMilliseconds()
|
||||
.toString()
|
||||
.padStart(3, "0")}`;
|
||||
|
||||
const formattedTime = chalk.gray(time);
|
||||
const formattedLevel = logLevelStyles[record.level](
|
||||
levelAbbreviations[record.level],
|
||||
);
|
||||
const formattedCategory = chalk.gray(record.category.join("\xb7"));
|
||||
const formattedMsg = chalk.reset(msg);
|
||||
|
||||
return [
|
||||
`${formattedTime} ${formattedLevel} ${formattedCategory} ${formattedMsg}`,
|
||||
];
|
||||
}
|
||||
|
||||
export const configureLoggers = (silent = false): Promise<void> =>
|
||||
configure({
|
||||
reset: true,
|
||||
sinks: {
|
||||
console: getConsoleSink({
|
||||
formatter: defaultConsoleFormatter,
|
||||
}),
|
||||
file: getRotatingFileSink(config.logging.log_file_path, {
|
||||
maxFiles: 10,
|
||||
maxSize: 10 * 1024 * 1024,
|
||||
}),
|
||||
},
|
||||
filters: {
|
||||
configFilter: silent
|
||||
? getLevelFilter(null)
|
||||
: getLevelFilter(config.logging.log_level),
|
||||
},
|
||||
loggers: [
|
||||
{
|
||||
category: "server",
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: ["federation", "inbox"],
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: ["federation", "delivery"],
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: ["federation", "bridge"],
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: ["federation", "resolvers"],
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: ["federation", "messaging"],
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: "database",
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: "webfinger",
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: "sonic",
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
{
|
||||
category: ["logtape", "meta"],
|
||||
lowestLevel: "error",
|
||||
},
|
||||
{
|
||||
category: "plugin",
|
||||
sinks: ["console", "file"],
|
||||
filters: ["configFilter"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
import * as Sentry from "@sentry/bun";
|
||||
import { config } from "@versia-server/config";
|
||||
import { env } from "bun";
|
||||
import pkg from "~/package.json" with { type: "json" };
|
||||
|
||||
const sentryInstance =
|
||||
config.logging.sentry &&
|
||||
Sentry.init({
|
||||
dsn: config.logging.sentry.dsn.origin,
|
||||
debug: config.logging.sentry.debug,
|
||||
sampleRate: config.logging.sentry.sample_rate,
|
||||
maxBreadcrumbs: config.logging.sentry.max_breadcrumbs,
|
||||
tracesSampleRate: config.logging.sentry.traces_sample_rate,
|
||||
environment: config.logging.sentry.environment,
|
||||
tracePropagationTargets:
|
||||
config.logging.sentry.trace_propagation_targets,
|
||||
release: env.GIT_COMMIT
|
||||
? `${pkg.version}-${env.GIT_COMMIT}`
|
||||
: pkg.version,
|
||||
integrations: [Sentry.extraErrorDataIntegration()],
|
||||
});
|
||||
|
||||
export const sentry = sentryInstance || undefined;
|
||||
|
|
@ -24,9 +24,7 @@ export const createServer = (
|
|||
async fetch(req, server): Promise<Response> {
|
||||
const output = await app.fetch(req, { ip: server.requestIP(req) });
|
||||
|
||||
if (config.logging.types.responses) {
|
||||
await debugResponse(output.clone());
|
||||
}
|
||||
await debugResponse(output.clone());
|
||||
|
||||
return output;
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue