diff options
Diffstat (limited to 'gui/src/shared')
| -rw-r--r-- | gui/src/shared/logging-types.ts | 17 | ||||
| -rw-r--r-- | gui/src/shared/logging.ts | 120 |
2 files changed, 63 insertions, 74 deletions
diff --git a/gui/src/shared/logging-types.ts b/gui/src/shared/logging-types.ts new file mode 100644 index 0000000000..09bd3f66cf --- /dev/null +++ b/gui/src/shared/logging-types.ts @@ -0,0 +1,17 @@ +export enum LogLevel { + error, + warning, + info, + verbose, + debug, +} + +export interface ILogOutput { + level: LogLevel; + write(level: LogLevel, message: string): void; + dispose?(): void; +} + +export interface ILogInput { + on(handler: (level: LogLevel, message: string) => void): void; +} diff --git a/gui/src/shared/logging.ts b/gui/src/shared/logging.ts index 471fd758c8..48c290c9b5 100644 --- a/gui/src/shared/logging.ts +++ b/gui/src/shared/logging.ts @@ -1,91 +1,63 @@ -import { app, remote } from 'electron'; -import log from 'electron-log'; -import * as fs from 'fs'; -import * as path from 'path'; +import moment from 'moment'; +import { ILogInput, ILogOutput, LogLevel } from './logging-types'; -const LOG_FORMAT = '[{y}-{m}-{d} {h}:{i}:{s}.{ms}][{level}] {text}'; +export class Logger { + private outputs: ILogOutput[] = []; -// Returns platform specific logs folder for application -// See open issue and PR on Github: -// 1. https://github.com/electron/electron/issues/10118 -// 2. https://github.com/electron/electron/pull/10191 -export function getLogsDirectory() { - const theApp = process.type === 'browser' ? app : remote.app; + public addOutput(output: ILogOutput) { + this.outputs.push(output); + } - switch (process.platform) { - case 'darwin': - // macOS: ~/Library/Logs/{appname} - return path.join(theApp.getPath('home'), 'Library/Logs', theApp.name); - default: - // Windows: %LOCALAPPDATA%\{appname}\logs - // Linux: ~/.config/{appname}/logs - return path.join(theApp.getPath('userData'), 'logs'); + public addInput(input: ILogInput) { + input.on((level: LogLevel, message: string) => this.outputMessage(level, message)); } -} -export function getMainLogFile(): string { - return path.join(getLogsDirectory(), 'frontend.log'); -} + public log(level: LogLevel, ...data: unknown[]) { + const time = moment().format('YYYY-MM-DD HH:mm:ss.SSS'); + const message = `[${time}][${LogLevel[level]}] ${data.join(' ')}`; -export function getRendererLogFile(): string { - return path.join(getLogsDirectory(), 'renderer.log'); -} - -export function setupLogging(logFile: string) { - log.transports.console.format = LOG_FORMAT; - log.transports.file.format = LOG_FORMAT; - log.transports.console.level = 'debug'; + this.outputMessage(level, message); + } - if (process.env.NODE_ENV === 'development') { - // Disable log file in development - log.transports.file.level = false; - } else { - // Configure logging to file - log.transports.file.level = 'debug'; - log.transports.file.resolvePath = (_variables) => logFile; + public error = (...data: unknown[]) => this.log(LogLevel.error, ...data); + public warn = (...data: unknown[]) => this.log(LogLevel.warning, ...data); + public info = (...data: unknown[]) => this.log(LogLevel.info, ...data); + public verbose = (...data: unknown[]) => this.log(LogLevel.verbose, ...data); + public debug = (...data: unknown[]) => this.log(LogLevel.debug, ...data); - log.debug(`Logging to ${logFile}`); + public dispose() { + this.outputs.forEach((output) => output.dispose?.()); } -} -export function backupLogFile(filePath: string): boolean { - const exists = fileExists(filePath); - if (exists) { - const backupFilePath = getBackupFilePath(filePath); - fs.renameSync(filePath, backupFilePath); + private outputMessage(level: LogLevel, message: string) { + this.outputs + .filter((output) => level <= output.level) + .forEach((output) => output.write(level, message)); } - - return exists; } -function getBackupFilePath(filePath: string): string { - const parsedPath = path.parse(filePath); - parsedPath.base = parsedPath.name + '.old' + parsedPath.ext; - return path.normalize(path.format(parsedPath)); -} +export class ConsoleOutput implements ILogOutput { + constructor(public level: LogLevel) {} -function fileExists(filePath: string): boolean { - try { - fs.accessSync(filePath); - return true; - } catch { - return false; + public write(level: LogLevel, message: string) { + switch (level) { + case LogLevel.error: + console.error(message); + break; + case LogLevel.warning: + console.warn(message); + break; + case LogLevel.info: + console.info(message); + break; + case LogLevel.verbose: + console.log(message); + break; + case LogLevel.debug: + console.debug(message); + break; + } } } -// When cleaning up old log files they are first backed up and the next time removed. -export function cleanUpLogDirectory(): void { - const oldLogFileNames = ['frontend-renderer.log']; - - oldLogFileNames.forEach((fileName) => { - const filePath = path.join(getLogsDirectory(), fileName); - rotateOrDeleteFile(filePath); - }); -} - -export function rotateOrDeleteFile(filePath: string) { - const backupFilePath = getBackupFilePath(filePath); - if (!backupLogFile(filePath) && fileExists(backupFilePath)) { - fs.unlinkSync(backupFilePath); - } -} +export default new Logger(); |
