Sélectionner une révision Git
-
Charles Anteunis a rédigéCharles Anteunis a rédigé
log.ts 4,69 Kio
/*
Copyright 2018 matrix-appservice-discord
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { createLogger, Logger, format, transports } from "winston";
import { DiscordBridgeConfigLogging, LoggingFile} from "./config";
import { inspect } from "util";
import "winston-daily-rotate-file";
const FORMAT_FUNC = format.printf((info) => {
return `${info.timestamp} [${info.module}] ${info.level}: ${info.message}`;
});
export class Log {
public static get level() {
return this.logger.level;
}
public static set level(level) {
this.logger.level = level;
}
public static Configure(config: DiscordBridgeConfigLogging) {
// Merge defaults.
Log.config = Object.assign(new DiscordBridgeConfigLogging(), config);
Log.setupLogger();
}
public static ForceSilent() {
new Log("Log").warn("Log set to silent");
Log.logger.silent = true;
}
private static config: DiscordBridgeConfigLogging;
private static logger: Logger;
private static isValidLevel(level: string) {
return ["silly", "verbose", "info", "http", "warn", "error", "silent"].includes(level);
}
private static setupLogger() {
if (Log.logger) {
Log.logger.close();
}
const tsports: transports.StreamTransportInstance[] = Log.config.files.map((file) =>
Log.setupFileTransport(file),
);
if (Log.config.console && !Log.isValidLevel(Log.config.console)) {
new Log("Log").warn("Console log level is invalid. Please pick one of the case-sensitive levels provided in the sample config.");
}
tsports.push(new transports.Console({
level: Log.config.console,
}));
Log.logger = createLogger({
format: format.combine(
format.timestamp({
format: Log.config.lineDateFormat,
}),