feat: migrate from electron-webpack to electron-forge
Electron upgrade : 10.1.3 => 13.1.7 Angular upgrade : 10.1.3 => 12.1.2
This commit is contained in:
139
workspaces/electron-app/main/utils/logger.ts
Normal file
139
workspaces/electron-app/main/utils/logger.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import { app } from "electron";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import * as winston from "winston";
|
||||
|
||||
declare const global: any;
|
||||
|
||||
export class Logger {
|
||||
private static singleton: Logger;
|
||||
private _logger: winston.Logger;
|
||||
|
||||
public static error(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.error(message, ...meta);
|
||||
}
|
||||
|
||||
public static warn(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.warn(message, ...meta);
|
||||
}
|
||||
|
||||
public static info(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.info(message, ...meta);
|
||||
}
|
||||
|
||||
public static http(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.http(message, ...meta);
|
||||
}
|
||||
|
||||
public static verbose(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.verbose(message, ...meta);
|
||||
}
|
||||
|
||||
public static debug(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.debug(message, ...meta);
|
||||
}
|
||||
|
||||
public static silly(message: string, ...meta: any[]) {
|
||||
Logger.initSingleton();
|
||||
Logger.singleton._logger.silly(message, ...meta);
|
||||
}
|
||||
|
||||
private static initSingleton() {
|
||||
if (!Logger.singleton) {
|
||||
Logger.singleton = new Logger();
|
||||
}
|
||||
}
|
||||
|
||||
private constructor() {
|
||||
this._logger = winston.createLogger({
|
||||
level: "debug",
|
||||
format: winston.format.json(),
|
||||
defaultMeta: { service: "user-service" },
|
||||
transports: [
|
||||
new winston.transports.File({
|
||||
filename: this.getLogFilename(),
|
||||
level: global.gConfig.mainLogLevel,
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
this.fileFormat
|
||||
),
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
// If we're not in production then log also to the `console` with the format:
|
||||
// `${info.timestamp} ${info.level}: ${info.message} JSON.stringify({ ...rest }) `
|
||||
if (global.gConfig.config_id === "development") {
|
||||
this._logger.add(
|
||||
new winston.transports.Console({
|
||||
stderrLevels: ["error", "warn"],
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
this.consoleFormat
|
||||
),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns log filename with standard path
|
||||
* In production, returns absolute standard path depending on platform
|
||||
*/
|
||||
private getLogFilename() {
|
||||
let filename = global.gConfig.mainLogFile;
|
||||
if (global.gConfig.config_id === "production") {
|
||||
const appName = app.getName();
|
||||
if (process.platform == "linux") {
|
||||
filename = `.config/${appName}/${filename}`;
|
||||
} else if (process.platform == "darwin") {
|
||||
filename = `Library/Logs/${appName}/${filename}`;
|
||||
} else if (process.platform == "win32") {
|
||||
filename = `AppData\\Roaming\\${appName}\\${filename}`;
|
||||
}
|
||||
}
|
||||
return path.join(os.homedir(), filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom winston file format
|
||||
* Write JSON logs with given format :
|
||||
* `${timestamp} ${level} : ${info.message} : ${meta})`
|
||||
*/
|
||||
private fileFormat = winston.format.printf((data: any) => {
|
||||
return JSON.stringify(this.prepareLogData(data));
|
||||
});
|
||||
|
||||
/**
|
||||
* Custom winston console format
|
||||
* Write logs with given format :
|
||||
* `${timestamp} ${level} : ${info.message} : JSON.stringify({ ...meta }) `
|
||||
*/
|
||||
private consoleFormat = winston.format.printf((data: any) => {
|
||||
const preparedData = this.prepareLogData(data);
|
||||
return (
|
||||
`${preparedData.timestamp} ${preparedData.level} : ` +
|
||||
`${preparedData.message} : ${JSON.stringify(preparedData.meta)}`
|
||||
);
|
||||
});
|
||||
|
||||
private prepareLogData = (data: any) => {
|
||||
const additionalData = { ...data };
|
||||
delete additionalData.timestamp;
|
||||
delete additionalData.level;
|
||||
delete additionalData.message;
|
||||
delete additionalData.service;
|
||||
return {
|
||||
timestamp: data.timestamp,
|
||||
level: data.level,
|
||||
message: data.message,
|
||||
meta: additionalData,
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user