chore(cli): flush all logs when app is starting

This commit is contained in:
Nicolas Meienberger 2023-10-24 08:39:03 +02:00 committed by Nicolas Meienberger
parent 04b9c9d5e0
commit 1925b0cb75
4 changed files with 46 additions and 3 deletions

View file

@ -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) {

View file

@ -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) => {

View file

@ -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();

View file

@ -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,