2024-06-27 01:11:39 +02:00
|
|
|
import {
|
2024-11-02 00:43:33 +01:00
|
|
|
type Stats,
|
2024-06-27 01:11:39 +02:00
|
|
|
appendFileSync,
|
|
|
|
|
closeSync,
|
|
|
|
|
existsSync,
|
|
|
|
|
mkdirSync,
|
|
|
|
|
openSync,
|
|
|
|
|
renameSync,
|
|
|
|
|
statSync,
|
|
|
|
|
} from "node:fs";
|
2025-02-25 23:32:05 +01:00
|
|
|
import {
|
|
|
|
|
type RotatingFileSinkDriver,
|
|
|
|
|
getRotatingFileSink,
|
|
|
|
|
} from "@logtape/file";
|
2024-06-27 01:11:39 +02:00
|
|
|
import {
|
|
|
|
|
type LogLevel,
|
|
|
|
|
type LogRecord,
|
|
|
|
|
configure,
|
|
|
|
|
getConsoleSink,
|
|
|
|
|
getLevelFilter,
|
|
|
|
|
} 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
|
|
|
|
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}`,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const nodeDriver: RotatingFileSinkDriver<number> = {
|
2024-11-02 00:43:33 +01:00
|
|
|
openSync(path: string): number {
|
2024-06-27 01:11:39 +02:00
|
|
|
return openSync(path, "a");
|
|
|
|
|
},
|
2024-11-02 00:43:33 +01:00
|
|
|
writeSync(fd, chunk): void {
|
2024-06-27 01:11:39 +02:00
|
|
|
appendFileSync(fd, chunk, {
|
|
|
|
|
flush: true,
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-11-02 00:43:33 +01:00
|
|
|
flushSync(): void {
|
2024-06-27 01:11:39 +02:00
|
|
|
// ...
|
|
|
|
|
},
|
2024-11-02 00:43:33 +01:00
|
|
|
closeSync(fd): void {
|
2024-06-27 01:11:39 +02:00
|
|
|
closeSync(fd);
|
|
|
|
|
},
|
2024-11-02 00:43:33 +01:00
|
|
|
statSync(path): Stats {
|
2024-06-27 01:11:39 +02:00
|
|
|
// If file does not exist, create it
|
|
|
|
|
if (!existsSync(path)) {
|
|
|
|
|
// Mkdir all directories in path
|
|
|
|
|
const dirs = path.split("/");
|
|
|
|
|
dirs.pop();
|
|
|
|
|
mkdirSync(dirs.join("/"), { recursive: true });
|
|
|
|
|
appendFileSync(path, "");
|
|
|
|
|
}
|
|
|
|
|
return statSync(path);
|
|
|
|
|
},
|
2024-10-03 13:41:58 +02:00
|
|
|
renameSync,
|
2024-06-27 01:11:39 +02:00
|
|
|
};
|
|
|
|
|
|
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
|
|
|
],
|
|
|
|
|
});
|