2025-02-26 00:28:05 +01:00
|
|
|
import { mkdir } from "node:fs/promises";
|
|
|
|
|
import { dirname } from "node:path";
|
|
|
|
|
import { getRotatingFileSink } from "@logtape/file";
|
2024-06-27 01:11:39 +02:00
|
|
|
import {
|
|
|
|
|
configure,
|
|
|
|
|
getConsoleSink,
|
|
|
|
|
getLevelFilter,
|
2025-04-10 19:15:31 +02:00
|
|
|
type LogLevel,
|
|
|
|
|
type LogRecord,
|
2024-06-27 01:11:39 +02:00
|
|
|
} from "@logtape/logtape";
|
|
|
|
|
import chalk from "chalk";
|
2025-02-15 02:47:29 +01:00
|
|
|
import { config } from "~/config.ts";
|
2024-04-14 13:20:55 +02:00
|
|
|
|
2025-02-26 00:28:05 +01:00
|
|
|
// 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 });
|
|
|
|
|
|
2024-06-27 01:11:39 +02:00
|
|
|
const levelAbbreviations: Record<LogLevel, string> = {
|
|
|
|
|
debug: "DBG",
|
|
|
|
|
info: "INF",
|
|
|
|
|
warning: "WRN",
|
|
|
|
|
error: "ERR",
|
|
|
|
|
fatal: "FTL",
|
|
|
|
|
};
|
2024-04-14 13:20:55 +02:00
|
|
|
|
2024-06-27 01:11:39 +02:00
|
|
|
/**
|
|
|
|
|
* 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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The default console formatter.
|
|
|
|
|
*
|
|
|
|
|
* @param record The log record to format.
|
|
|
|
|
* @returns The formatted log record, as an array of arguments for
|
2025-02-25 23:32:05 +01:00
|
|
|
* {@link console.log}.
|
2024-06-27 01:11:39 +02:00
|
|
|
*/
|
|
|
|
|
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}`,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 00:43:33 +01:00
|
|
|
export const configureLoggers = (silent = false): Promise<void> =>
|
2024-06-27 01:11:39 +02:00
|
|
|
configure({
|
2024-06-27 03:16:50 +02:00
|
|
|
reset: true,
|
2024-06-27 01:11:39 +02:00
|
|
|
sinks: {
|
|
|
|
|
console: getConsoleSink({
|
|
|
|
|
formatter: defaultConsoleFormatter,
|
|
|
|
|
}),
|
2025-02-25 23:32:05 +01:00
|
|
|
file: getRotatingFileSink(config.logging.log_file_path, {
|
2024-06-27 01:11:39 +02:00
|
|
|
maxFiles: 10,
|
|
|
|
|
maxSize: 10 * 1024 * 1024,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
filters: {
|
|
|
|
|
configFilter: silent
|
2024-06-27 02:44:08 +02:00
|
|
|
? getLevelFilter(null)
|
|
|
|
|
: getLevelFilter(config.logging.log_level),
|
2024-06-27 01:11:39 +02:00
|
|
|
},
|
|
|
|
|
loggers: [
|
|
|
|
|
{
|
|
|
|
|
category: "server",
|
|
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-11-24 23:13:29 +01:00
|
|
|
category: ["federation", "inbox"],
|
2024-06-27 01:11:39 +02:00
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-11-25 11:29:48 +01:00
|
|
|
category: ["federation", "delivery"],
|
2024-11-24 23:13:29 +01:00
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: ["federation", "bridge"],
|
|
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: ["federation", "resolvers"],
|
2024-06-27 01:11:39 +02:00
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
2024-11-25 16:54:46 +01:00
|
|
|
{
|
|
|
|
|
category: ["federation", "messaging"],
|
|
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
2024-06-27 01:11:39 +02:00
|
|
|
{
|
|
|
|
|
category: "database",
|
|
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: "webfinger",
|
|
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-06-29 11:40:44 +02:00
|
|
|
category: "sonic",
|
2024-06-27 01:11:39 +02:00
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: ["logtape", "meta"],
|
2024-11-28 10:26:28 +01:00
|
|
|
lowestLevel: "error",
|
2024-06-27 01:11:39 +02:00
|
|
|
},
|
2024-09-23 11:51:15 +02:00
|
|
|
{
|
|
|
|
|
category: "plugin",
|
|
|
|
|
sinks: ["console", "file"],
|
|
|
|
|
filters: ["configFilter"],
|
|
|
|
|
},
|
2024-06-27 01:11:39 +02:00
|
|
|
],
|
|
|
|
|
});
|