From 1925b0cb7532fd2ee013ad2b68590f21ace96ab2 Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Tue, 24 Oct 2023 08:39:03 +0200 Subject: [PATCH] chore(cli): flush all logs when app is starting --- .../src/executors/system/system.executors.ts | 2 + packages/cli/src/services/watcher/watcher.ts | 2 +- packages/cli/src/utils/logger/file-logger.ts | 43 ++++++++++++++++++- packages/shared/src/utils/logger/Logger.ts | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/executors/system/system.executors.ts b/packages/cli/src/executors/system/system.executors.ts index 2307006e..db2f9a28 100644 --- a/packages/cli/src/executors/system/system.executors.ts +++ b/packages/cli/src/executors/system/system.executors.ts @@ -174,6 +174,8 @@ export class SystemExecutors { public start = async (sudo = true, killWatchers = true) => { const spinner = new TerminalSpinner('Starting Tipi...'); try { + this.logger.flush(); + const { isSudo } = getUserIds(); if (!sudo) { diff --git a/packages/cli/src/services/watcher/watcher.ts b/packages/cli/src/services/watcher/watcher.ts index 1f475474..e7472ceb 100644 --- a/packages/cli/src/services/watcher/watcher.ts +++ b/packages/cli/src/services/watcher/watcher.ts @@ -113,7 +113,7 @@ export const startWorker = async () => { }); worker.on('completed', (job) => { - fileLogger.info(`Job ${job.id} completed with result: ${JSON.stringify(job.returnvalue)}`); + fileLogger.info(`Job ${job.id} completed with result:`, JSON.stringify(job.returnvalue)); }); worker.on('failed', (job) => { diff --git a/packages/cli/src/utils/logger/file-logger.ts b/packages/cli/src/utils/logger/file-logger.ts index d35de7fc..3c16b1cb 100644 --- a/packages/cli/src/utils/logger/file-logger.ts +++ b/packages/cli/src/utils/logger/file-logger.ts @@ -1,4 +1,45 @@ +import fs from 'fs'; import { createLogger } from '@runtipi/shared'; import path from 'path'; -export const fileLogger = createLogger('cli', path.join(process.cwd(), 'logs')); +class FileLogger { + private winstonLogger = createLogger('cli', path.join(process.cwd(), 'logs')); + + private logsFolder = path.join(process.cwd(), 'logs'); + + public flush = () => { + try { + if (fs.existsSync(path.join(this.logsFolder, 'app.log'))) { + const appLog = fs.readFileSync(path.join(this.logsFolder, 'app.log'), 'utf-8'); + fs.appendFileSync(path.join(this.logsFolder, 'app.log.history'), appLog); + fs.writeFileSync(path.join(this.logsFolder, 'app.log'), ''); + } + + if (fs.existsSync(path.join(this.logsFolder, 'error.log'))) { + const appErrorLog = fs.readFileSync(path.join(this.logsFolder, 'error.log'), 'utf-8'); + fs.appendFileSync(path.join(this.logsFolder, 'error.log.history'), appErrorLog); + fs.writeFileSync(path.join(this.logsFolder, 'error.log'), ''); + } + } catch (error) { + this.winstonLogger.error('Error flushing logs', error); + } + }; + + public error = (...message: unknown[]) => { + this.winstonLogger.error(message.join(' ')); + }; + + public info = (...message: unknown[]) => { + this.winstonLogger.info(message.join(' ')); + }; + + public warn = (...message: unknown[]) => { + this.winstonLogger.warn(message.join(' ')); + }; + + public debug = (...message: unknown[]) => { + this.winstonLogger.debug(message.join(' ')); + }; +} + +export const fileLogger = new FileLogger(); diff --git a/packages/shared/src/utils/logger/Logger.ts b/packages/shared/src/utils/logger/Logger.ts index 297f4bfa..bf5692dd 100644 --- a/packages/shared/src/utils/logger/Logger.ts +++ b/packages/shared/src/utils/logger/Logger.ts @@ -45,7 +45,7 @@ export const newLogger = (id: string, logsFolder: string) => { colorize(), timestamp(), align(), - printf((info) => `${info.timestamp} - ${info.level} > ${info.message}`), + printf((info) => `${id}: ${info.timestamp} - ${info.level} > ${info.message}`), ), transports: tr, exceptionHandlers,