From 7cf7febc5c91046a9e634bfc9bcd549ee564b4d1 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Fri, 29 Mar 2024 17:28:57 +1100 Subject: [PATCH 1/9] Remove duplicative logFN type --- src/backend/Logger.ts | 4 ++-- src/backend/routes/LoggerRouter.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/Logger.ts b/src/backend/Logger.ts index 3cb26270..19a72cc5 100644 --- a/src/backend/Logger.ts +++ b/src/backend/Logger.ts @@ -1,7 +1,6 @@ import {Config} from '../common/config/private/Config'; import {LogLevel} from '../common/config/private/PrivateConfig'; -export type logFN = (...args: (string | number | (() => string))[]) => void; const forcedDebug = process.env['NODE_ENV'] === 'debug'; @@ -11,7 +10,8 @@ if (forcedDebug === true) { ); } -export type LoggerArgs = (string | number | (() => string)) +// Match `console.log` inputs +export type LoggerArgs = Parameters[0] export type LoggerFunction = (...args: LoggerArgs[]) => void; export interface ILogger { diff --git a/src/backend/routes/LoggerRouter.ts b/src/backend/routes/LoggerRouter.ts index 896d9422..e58a48c1 100644 --- a/src/backend/routes/LoggerRouter.ts +++ b/src/backend/routes/LoggerRouter.ts @@ -1,5 +1,5 @@ import {Express, NextFunction, Request, Response} from 'express'; -import {logFN, Logger} from '../Logger'; +import {LoggerFunction, Logger} from '../Logger'; import {Config} from '../../common/config/private/Config'; declare global { @@ -16,7 +16,7 @@ declare global { * Adds logging to express */ export class LoggerRouter { - public static log(loggerFn: logFN, req: Request, res: Response): void { + public static log(loggerFn: LoggerFunction, req: Request, res: Response): void { if (req.logged === true) { return; } From fae298069277cd27dc45047fc6505b871a9e9267 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Fri, 29 Mar 2024 17:39:15 +1100 Subject: [PATCH 2/9] Remove dead code --- src/backend/middlewares/RenderingMWs.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index f4022dab..5af9c8ba 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -138,13 +138,6 @@ export class RenderingMWs { const d = err.detailsStr; delete err.detailsStr; console.log(err); - if (err.detailsStr) { - try { - console.log('details:', JSON.stringify(err.detailsStr)); - } catch (_) { - console.log(err.detailsStr); - } - } err.detailsStr = d; delete err.details; // do not send back error object to the client side From 5c8e01c03414539f79c2f5db52847813bd4589a9 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Fri, 29 Mar 2024 17:40:49 +1100 Subject: [PATCH 3/9] Configure log levels for specific error types --- src/backend/Logger.ts | 22 ++++++++++++++++++++++ src/backend/middlewares/RenderingMWs.ts | 3 +-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/backend/Logger.ts b/src/backend/Logger.ts index 19a72cc5..ddea11f3 100644 --- a/src/backend/Logger.ts +++ b/src/backend/Logger.ts @@ -1,5 +1,6 @@ import {Config} from '../common/config/private/Config'; import {LogLevel} from '../common/config/private/PrivateConfig'; +import {ErrorCodes} from '../common/entities/Error'; const forcedDebug = process.env['NODE_ENV'] === 'debug'; @@ -103,4 +104,25 @@ export class Logger { }); console.log(date + tag + LOG_TAG, ...args); } + + public static logLevelForError(e: ErrorCodes): LoggerFunction { + switch (e) { + case ErrorCodes.INPUT_ERROR: + case ErrorCodes.NOT_AUTHENTICATED: + case ErrorCodes.ALREADY_AUTHENTICATED: + case ErrorCodes.NOT_AUTHORISED: + case ErrorCodes.PERMISSION_DENIED: + case ErrorCodes.CREDENTIAL_NOT_FOUND: + return Logger.debug + case ErrorCodes.SETTINGS_ERROR: + case ErrorCodes.TASK_ERROR: + case ErrorCodes.JOB_ERROR: + case ErrorCodes.THUMBNAIL_GENERATION_ERROR: + case ErrorCodes.PHOTO_GENERATION_ERROR: + case ErrorCodes.SERVER_ERROR: + return Logger.error + default: + return Logger.warn + } + } } diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index 5af9c8ba..bb4ca0c1 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -132,8 +132,7 @@ export class RenderingMWs { ): void { if (err instanceof ErrorDTO) { if (err.details) { - Logger.warn('Handled error:'); - LoggerRouter.log(Logger.warn, req, res); + LoggerRouter.log(logFn, req, res); // use separate rendering for detailsStr const d = err.detailsStr; delete err.detailsStr; From 3be8f1b194f63fd7644b565f54290c93cfcd5c0d Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Fri, 29 Mar 2024 17:43:20 +1100 Subject: [PATCH 4/9] Remove duplicative logging These log calls either duplicated the generic error renderer logging or neighbouring log calls --- src/backend/middlewares/SharingMWs.ts | 1 - src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts | 2 -- src/backend/model/jobs/jobs/FileJob.ts | 1 - src/backend/routes/ErrorRouter.ts | 2 -- 4 files changed, 6 deletions(-) diff --git a/src/backend/middlewares/SharingMWs.ts b/src/backend/middlewares/SharingMWs.ts index e4552561..0776a032 100644 --- a/src/backend/middlewares/SharingMWs.ts +++ b/src/backend/middlewares/SharingMWs.ts @@ -118,7 +118,6 @@ export class SharingMWs { ); return next(); } catch (err) { - console.warn(err); return next( new ErrorDTO( ErrorCodes.GENERAL_ERROR, diff --git a/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts b/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts index a8943c09..fc65efd0 100644 --- a/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts +++ b/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts @@ -42,7 +42,6 @@ export class ThumbnailGeneratorMWs { ThumbnailGeneratorMWs.addThInfoToPhotos(cw.searchResult.media); } } catch (error) { - console.error(error); return next( new ErrorDTO( ErrorCodes.SERVER_ERROR, @@ -120,7 +119,6 @@ export class ThumbnailGeneratorMWs { req.resultPipe = await PhotoProcessing.generatePersonThumbnail(person); return next(); } catch (error) { - console.error(error); return next( new ErrorDTO( ErrorCodes.THUMBNAIL_GENERATION_ERROR, diff --git a/src/backend/model/jobs/jobs/FileJob.ts b/src/backend/model/jobs/jobs/FileJob.ts index 2159973b..bce773cc 100644 --- a/src/backend/model/jobs/jobs/FileJob.ts +++ b/src/backend/model/jobs/jobs/FileJob.ts @@ -110,7 +110,6 @@ export abstract class FileJob Date: Fri, 29 Mar 2024 17:45:08 +1100 Subject: [PATCH 5/9] Always use Logger class and try to log once per event This unifies logging across the backend to always use the Logger class, always only logs to stdout (rather than an inconsistent mix of stdout and stderr, depending on whether console.error was used), and removes logging where two log events happened for the same message For example, this pattern: ```js Logger.error("Whoops, something went wrong:") console.error(err) ``` That causes two separate log events, and depending on the log transport used, could cause relevant log messages to get split across multiple events and therefore be harder (usually just more tedious) to connect and debug in production environments. --- src/backend/middlewares/GalleryMWs.ts | 3 ++- src/backend/middlewares/RenderingMWs.ts | 5 ++++- src/backend/model/database/GalleryManager.ts | 2 +- src/backend/model/database/IndexingManager.ts | 4 ++-- src/backend/model/database/SQLConnection.ts | 3 +-- .../model/extension/ExtensionConfigWrapper.ts | 12 +++++------- src/backend/model/fileaccess/DiskManager.ts | 4 ++-- src/backend/model/fileaccess/MetadataLoader.ts | 11 ++++------- src/backend/model/fileaccess/TaskExecuter.ts | 3 ++- src/backend/model/jobs/JobProgressManager.ts | 5 +++-- src/backend/model/jobs/jobs/IndexingJob.ts | 3 +-- src/backend/model/jobs/jobs/Job.ts | 2 +- src/backend/model/messenger/StdoutMessenger.ts | 3 ++- src/backend/routes/ErrorRouter.ts | 3 +-- src/backend/server.ts | 2 +- 15 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/backend/middlewares/GalleryMWs.ts b/src/backend/middlewares/GalleryMWs.ts index da28a013..0b7dc1ec 100644 --- a/src/backend/middlewares/GalleryMWs.ts +++ b/src/backend/middlewares/GalleryMWs.ts @@ -17,6 +17,7 @@ import {LocationLookupException} from '../exceptions/LocationLookupException'; import {SupportedFormats} from '../../common/SupportedFormats'; import {ServerTime} from './ServerTimingMWs'; import {SortByTypes} from '../../common/entities/SortingMethods'; +import {Logger} from '../Logger'; export class GalleryMWs { @ServerTime('1.db', 'List Directory') @@ -109,7 +110,7 @@ export class GalleryMWs { }); res.on('close', () => { - console.log('zip ' + archive.pointer() + ' bytes'); + Logger.info('zip ' + archive.pointer() + ' bytes'); }); archive.on('error', (err: Error) => { diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index bb4ca0c1..a0f4b397 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -132,11 +132,14 @@ export class RenderingMWs { ): void { if (err instanceof ErrorDTO) { if (err.details) { + const logFn = Logger.logLevelForError(err.code) + + logFn('Handled error:'); LoggerRouter.log(logFn, req, res); // use separate rendering for detailsStr const d = err.detailsStr; delete err.detailsStr; - console.log(err); + logFn(err); err.detailsStr = d; delete err.details; // do not send back error object to the client side diff --git a/src/backend/model/database/GalleryManager.ts b/src/backend/model/database/GalleryManager.ts index 7d551e1e..06995371 100644 --- a/src/backend/model/database/GalleryManager.ts +++ b/src/backend/model/database/GalleryManager.ts @@ -123,7 +123,7 @@ export class GalleryManager { ); ObjectManagers.getInstance() .IndexingManager.indexDirectory(relativeDirectoryName) - .catch(console.error); + .catch(Logger.error); } return await this.getParentDirFromId(connection, dir.id); } diff --git a/src/backend/model/database/IndexingManager.ts b/src/backend/model/database/IndexingManager.ts index a07bd083..fd09e679 100644 --- a/src/backend/model/database/IndexingManager.ts +++ b/src/backend/model/database/IndexingManager.ts @@ -100,13 +100,13 @@ export class IndexingManager { resolve(dirClone); // save directory to DB - this.queueForSave(scannedDirectory).catch(console.error); + this.queueForSave(scannedDirectory).catch(Logger.error); } catch (error) { NotificationManager.warning( 'Unknown indexing error for: ' + relativeDirectoryName, error.toString() ); - console.error(error); + Logger.error(error); return reject(error); } }); diff --git a/src/backend/model/database/SQLConnection.ts b/src/backend/model/database/SQLConnection.ts index 07eb2ec9..047ef7a3 100644 --- a/src/backend/model/database/SQLConnection.ts +++ b/src/backend/model/database/SQLConnection.ts @@ -155,8 +155,7 @@ export class SQLConnection { this.connection = null; } } catch (err) { - console.error('Error during closing sql db:'); - console.error(err); + Logger.error('Error during closing sql db:\n', err); } } diff --git a/src/backend/model/extension/ExtensionConfigWrapper.ts b/src/backend/model/extension/ExtensionConfigWrapper.ts index eaa7270f..5f5f248a 100644 --- a/src/backend/model/extension/ExtensionConfigWrapper.ts +++ b/src/backend/model/extension/ExtensionConfigWrapper.ts @@ -3,7 +3,7 @@ import {PrivateConfigClass} from '../../../common/config/private/PrivateConfigCl import {ConfigClassBuilder} from 'typeconfig/node'; import {ExtensionConfigTemplateLoader} from './ExtensionConfigTemplateLoader'; import {NotificationManager} from '../NotifocationManager'; - +import {Logger} from '../../Logger'; const LOG_TAG = '[ExtensionConfigWrapper]'; @@ -19,10 +19,9 @@ export class ExtensionConfigWrapper { await pc.load(); // loading the basic configs, but we do not know the extension config hierarchy yet } catch (e) { - console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.'); - console.error(e); + Logger.error(LOG_TAG, 'Error during loading config. Reverting to defaults.\n', e); if (showDetailedError) { - console.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); + Logger.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); NotificationManager.error('Can\'t load config. Reverting to default. This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.', (e.toString ? e.toString() : JSON.stringify(e))); } } @@ -37,10 +36,9 @@ export class ExtensionConfigWrapper { pc.loadSync(); // loading the basic configs, but we do not know the extension config hierarchy yet } catch (e) { - console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.'); - console.error(e); + Logger.error(LOG_TAG, 'Error during loading config. Reverting to defaults.\n', e); if (showDetailedError) { - console.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); + Logger.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); NotificationManager.error('Ca\'nt load config. Reverting to default. This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.', (e.toString ? e.toString() : JSON.stringify(e))); } } diff --git a/src/backend/model/fileaccess/DiskManager.ts b/src/backend/model/fileaccess/DiskManager.ts index e07ff2e3..5efaf424 100644 --- a/src/backend/model/fileaccess/DiskManager.ts +++ b/src/backend/model/fileaccess/DiskManager.ts @@ -179,7 +179,7 @@ export class DiskManager { 'Unknown directory reading error, skipping: ' + path.join(relativeDirectoryName, file), err.toString() ); - console.error(err); + Logger.error(err); } } else if (PhotoProcessing.isPhoto(fullFilePath)) { try { @@ -218,7 +218,7 @@ export class DiskManager { ', reason: ' + err.toString() ); - console.error(err); + Logger.error(err); } } else if (VideoProcessing.isVideo(fullFilePath)) { try { diff --git a/src/backend/model/fileaccess/MetadataLoader.ts b/src/backend/model/fileaccess/MetadataLoader.ts index 5a61af21..84ffa2be 100644 --- a/src/backend/model/fileaccess/MetadataLoader.ts +++ b/src/backend/model/fileaccess/MetadataLoader.ts @@ -41,7 +41,7 @@ export class MetadataLoader { metadata.fileSize = stat.size; metadata.creationDate = stat.mtime.getTime(); //Default date is file system time of last modification } catch (err) { - console.log(err); + Logger.info(err); // ignoring errors } try { @@ -214,8 +214,7 @@ export class MetadataLoader { try { await fileHandle.read(data, 0, bufferSize, 0); } catch (err) { - Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath); - console.error(err); + Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath + '\n', err); return MetadataLoader.EMPTY_METADATA; } finally { await fileHandle.close(); @@ -295,13 +294,11 @@ export class MetadataLoader { metadata.creationDate = 0; } } catch (err) { - Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath); - console.error(err); + Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath + '\n', err); return MetadataLoader.EMPTY_METADATA; } } catch (err) { - Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath); - console.error(err); + Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath + '\n', err); return MetadataLoader.EMPTY_METADATA; } return metadata; diff --git a/src/backend/model/fileaccess/TaskExecuter.ts b/src/backend/model/fileaccess/TaskExecuter.ts index 4e203f03..33ee75f3 100644 --- a/src/backend/model/fileaccess/TaskExecuter.ts +++ b/src/backend/model/fileaccess/TaskExecuter.ts @@ -1,5 +1,6 @@ import {TaskQue} from './TaskQue'; import {EventLoopHandler} from '../EventLoopHandler'; +import { Logger } from '../../Logger'; export interface ITaskExecuter { execute(input: I): Promise; @@ -30,7 +31,7 @@ export class TaskExecuter implements ITaskExecuter { execute(input: I): Promise { const promise = this.taskQue.add(input).promise.obj; - this.run().catch(console.error); + this.run().catch(Logger.error); return promise; } } diff --git a/src/backend/model/jobs/JobProgressManager.ts b/src/backend/model/jobs/JobProgressManager.ts index a1c46426..4c637a2d 100644 --- a/src/backend/model/jobs/JobProgressManager.ts +++ b/src/backend/model/jobs/JobProgressManager.ts @@ -3,6 +3,7 @@ import * as path from 'path'; import {ProjectPath} from '../../ProjectPath'; import {Config} from '../../../common/config/private/Config'; import {JobProgressDTO, JobProgressStates,} from '../../../common/entities/job/JobProgressDTO'; +import { Logger } from '../../Logger'; export class JobProgressManager { private static readonly VERSION = 3; @@ -20,7 +21,7 @@ export class JobProgressManager { constructor() { this.dbPath = path.join(ProjectPath.DBFolder, 'jobs.db'); - this.loadDB().catch(console.error); + this.loadDB().catch(Logger.error); } get Progresses(): { [key: string]: JobProgressDTO } { @@ -89,7 +90,7 @@ export class JobProgressManager { return; } this.timer = setTimeout(async (): Promise => { - this.saveDB().catch(console.error); + this.saveDB().catch(Logger.error); this.timer = null; }, 5000); } diff --git a/src/backend/model/jobs/jobs/IndexingJob.ts b/src/backend/model/jobs/jobs/IndexingJob.ts index 65973e85..b85beb40 100644 --- a/src/backend/model/jobs/jobs/IndexingJob.ts +++ b/src/backend/model/jobs/jobs/IndexingJob.ts @@ -93,8 +93,7 @@ export class IndexingJob< } catch (e) { this.Progress.log('Skipping. Indexing failed for: ' + directory); this.Progress.Skipped++; - Logger.warn(LOG_TAG, 'Skipping. Indexing failed for: ' + directory); - console.error(e); + Logger.warn(LOG_TAG, 'Skipping. Indexing failed for: ' + directory, + '\n', e); } if (this.Progress.State !== JobProgressStates.running) { return false; diff --git a/src/backend/model/jobs/jobs/Job.ts b/src/backend/model/jobs/jobs/Job.ts index 36a7a0cb..ddf7c3f3 100644 --- a/src/backend/model/jobs/jobs/Job.ts +++ b/src/backend/model/jobs/jobs/Job.ts @@ -71,7 +71,7 @@ export abstract class Job = Record((resolve): void => { this.prResolve = resolve; }); - this.init().catch(console.error); + this.init().catch(Logger.error); this.run(); if (!this.IsInstant) { // if instant, wait for execution, otherwise, return right away diff --git a/src/backend/model/messenger/StdoutMessenger.ts b/src/backend/model/messenger/StdoutMessenger.ts index 1edbf20a..86cabbb6 100644 --- a/src/backend/model/messenger/StdoutMessenger.ts +++ b/src/backend/model/messenger/StdoutMessenger.ts @@ -1,6 +1,7 @@ import {MediaDTOWithThPath, Messenger} from './Messenger'; import {DynamicConfig} from '../../../common/entities/DynamicConfig'; import {DefaultMessengers} from '../../../common/entities/job/JobDTO'; +import { Logger } from '../../Logger'; export class StdoutMessenger extends Messenger { public readonly Name = DefaultMessengers[DefaultMessengers.Stdout]; @@ -12,6 +13,6 @@ export class StdoutMessenger extends Messenger { protected async sendMedia(config: never, media: MediaDTOWithThPath[]) { - console.log(media.map(m => m.thumbnailPath)); + Logger.info(media.map(m => m.thumbnailPath)); } } diff --git a/src/backend/routes/ErrorRouter.ts b/src/backend/routes/ErrorRouter.ts index 3507e631..6fc76e96 100644 --- a/src/backend/routes/ErrorRouter.ts +++ b/src/backend/routes/ErrorRouter.ts @@ -38,8 +38,7 @@ export class ErrorRouter { } // Flush out the stack to the console - Logger.error('Unexpected error:'); - console.error(err); + Logger.error('Unexpected error:\n', err); return next( new ErrorDTO( ErrorCodes.SERVER_ERROR, diff --git a/src/backend/server.ts b/src/backend/server.ts index 382330e6..9c0a9213 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -51,7 +51,7 @@ export class Server { 'Running in DEBUG mode, set env variable NODE_ENV=production to disable ' ); } - this.init(listen).catch(console.error); + this.init(listen).catch(Logger.error); } get Server(): HttpServer { From b0c2f8da0446c13302b9e7274ffc789f54afd55c Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Fri, 29 Mar 2024 17:45:08 +1100 Subject: [PATCH 6/9] Always use Logger class and try to log once per event This unifies logging across the backend to always use the Logger class, always only logs to stdout (rather than an inconsistent mix of stdout and stderr, depending on whether console.error was used), and removes logging where two log events happened for the same message For example, this pattern: ```js Logger.error("Whoops, something went wrong:") console.error(err) ``` That causes two separate log events, and depending on the log transport used, could cause relevant log messages to get split across multiple events and therefore be harder (usually just more tedious) to connect and debug in production environments. --- src/backend/middlewares/RenderingMWs.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index a0f4b397..51d7292a 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -134,12 +134,11 @@ export class RenderingMWs { if (err.details) { const logFn = Logger.logLevelForError(err.code) - logFn('Handled error:'); - LoggerRouter.log(logFn, req, res); // use separate rendering for detailsStr const d = err.detailsStr; delete err.detailsStr; - logFn(err); + logFn('Handled error:\n', err); + LoggerRouter.log(logFn, req, res); err.detailsStr = d; delete err.details; // do not send back error object to the client side From fcdb2f2938d724f49eeeb7bba520219e98398f6e Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Sun, 31 Mar 2024 09:28:04 +1100 Subject: [PATCH 7/9] Revert "Always use Logger class and try to log once per event" This reverts commit a57a717717a676555b4e28d5746828477c2f9811. --- src/backend/middlewares/GalleryMWs.ts | 3 +-- src/backend/middlewares/RenderingMWs.ts | 6 ++---- src/backend/model/database/GalleryManager.ts | 2 +- src/backend/model/database/IndexingManager.ts | 4 ++-- src/backend/model/database/SQLConnection.ts | 3 ++- .../model/extension/ExtensionConfigWrapper.ts | 12 +++++++----- src/backend/model/fileaccess/DiskManager.ts | 4 ++-- src/backend/model/fileaccess/MetadataLoader.ts | 11 +++++++---- src/backend/model/fileaccess/TaskExecuter.ts | 3 +-- src/backend/model/jobs/JobProgressManager.ts | 5 ++--- src/backend/model/jobs/jobs/IndexingJob.ts | 3 ++- src/backend/model/jobs/jobs/Job.ts | 2 +- src/backend/model/messenger/StdoutMessenger.ts | 3 +-- src/backend/routes/ErrorRouter.ts | 3 ++- src/backend/server.ts | 2 +- 15 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/backend/middlewares/GalleryMWs.ts b/src/backend/middlewares/GalleryMWs.ts index 0b7dc1ec..da28a013 100644 --- a/src/backend/middlewares/GalleryMWs.ts +++ b/src/backend/middlewares/GalleryMWs.ts @@ -17,7 +17,6 @@ import {LocationLookupException} from '../exceptions/LocationLookupException'; import {SupportedFormats} from '../../common/SupportedFormats'; import {ServerTime} from './ServerTimingMWs'; import {SortByTypes} from '../../common/entities/SortingMethods'; -import {Logger} from '../Logger'; export class GalleryMWs { @ServerTime('1.db', 'List Directory') @@ -110,7 +109,7 @@ export class GalleryMWs { }); res.on('close', () => { - Logger.info('zip ' + archive.pointer() + ' bytes'); + console.log('zip ' + archive.pointer() + ' bytes'); }); archive.on('error', (err: Error) => { diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index 51d7292a..bb4ca0c1 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -132,13 +132,11 @@ export class RenderingMWs { ): void { if (err instanceof ErrorDTO) { if (err.details) { - const logFn = Logger.logLevelForError(err.code) - + LoggerRouter.log(logFn, req, res); // use separate rendering for detailsStr const d = err.detailsStr; delete err.detailsStr; - logFn('Handled error:\n', err); - LoggerRouter.log(logFn, req, res); + console.log(err); err.detailsStr = d; delete err.details; // do not send back error object to the client side diff --git a/src/backend/model/database/GalleryManager.ts b/src/backend/model/database/GalleryManager.ts index 06995371..7d551e1e 100644 --- a/src/backend/model/database/GalleryManager.ts +++ b/src/backend/model/database/GalleryManager.ts @@ -123,7 +123,7 @@ export class GalleryManager { ); ObjectManagers.getInstance() .IndexingManager.indexDirectory(relativeDirectoryName) - .catch(Logger.error); + .catch(console.error); } return await this.getParentDirFromId(connection, dir.id); } diff --git a/src/backend/model/database/IndexingManager.ts b/src/backend/model/database/IndexingManager.ts index fd09e679..a07bd083 100644 --- a/src/backend/model/database/IndexingManager.ts +++ b/src/backend/model/database/IndexingManager.ts @@ -100,13 +100,13 @@ export class IndexingManager { resolve(dirClone); // save directory to DB - this.queueForSave(scannedDirectory).catch(Logger.error); + this.queueForSave(scannedDirectory).catch(console.error); } catch (error) { NotificationManager.warning( 'Unknown indexing error for: ' + relativeDirectoryName, error.toString() ); - Logger.error(error); + console.error(error); return reject(error); } }); diff --git a/src/backend/model/database/SQLConnection.ts b/src/backend/model/database/SQLConnection.ts index 047ef7a3..07eb2ec9 100644 --- a/src/backend/model/database/SQLConnection.ts +++ b/src/backend/model/database/SQLConnection.ts @@ -155,7 +155,8 @@ export class SQLConnection { this.connection = null; } } catch (err) { - Logger.error('Error during closing sql db:\n', err); + console.error('Error during closing sql db:'); + console.error(err); } } diff --git a/src/backend/model/extension/ExtensionConfigWrapper.ts b/src/backend/model/extension/ExtensionConfigWrapper.ts index 5f5f248a..eaa7270f 100644 --- a/src/backend/model/extension/ExtensionConfigWrapper.ts +++ b/src/backend/model/extension/ExtensionConfigWrapper.ts @@ -3,7 +3,7 @@ import {PrivateConfigClass} from '../../../common/config/private/PrivateConfigCl import {ConfigClassBuilder} from 'typeconfig/node'; import {ExtensionConfigTemplateLoader} from './ExtensionConfigTemplateLoader'; import {NotificationManager} from '../NotifocationManager'; -import {Logger} from '../../Logger'; + const LOG_TAG = '[ExtensionConfigWrapper]'; @@ -19,9 +19,10 @@ export class ExtensionConfigWrapper { await pc.load(); // loading the basic configs, but we do not know the extension config hierarchy yet } catch (e) { - Logger.error(LOG_TAG, 'Error during loading config. Reverting to defaults.\n', e); + console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.'); + console.error(e); if (showDetailedError) { - Logger.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); + console.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); NotificationManager.error('Can\'t load config. Reverting to default. This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.', (e.toString ? e.toString() : JSON.stringify(e))); } } @@ -36,9 +37,10 @@ export class ExtensionConfigWrapper { pc.loadSync(); // loading the basic configs, but we do not know the extension config hierarchy yet } catch (e) { - Logger.error(LOG_TAG, 'Error during loading config. Reverting to defaults.\n', e); + console.error(LOG_TAG, 'Error during loading config. Reverting to defaults.'); + console.error(e); if (showDetailedError) { - Logger.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); + console.error(LOG_TAG, 'This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.'); NotificationManager.error('Ca\'nt load config. Reverting to default. This is most likely due to: 1) you added a bad configuration in the server.json OR 2) The configuration changed in the latest release.', (e.toString ? e.toString() : JSON.stringify(e))); } } diff --git a/src/backend/model/fileaccess/DiskManager.ts b/src/backend/model/fileaccess/DiskManager.ts index 5efaf424..e07ff2e3 100644 --- a/src/backend/model/fileaccess/DiskManager.ts +++ b/src/backend/model/fileaccess/DiskManager.ts @@ -179,7 +179,7 @@ export class DiskManager { 'Unknown directory reading error, skipping: ' + path.join(relativeDirectoryName, file), err.toString() ); - Logger.error(err); + console.error(err); } } else if (PhotoProcessing.isPhoto(fullFilePath)) { try { @@ -218,7 +218,7 @@ export class DiskManager { ', reason: ' + err.toString() ); - Logger.error(err); + console.error(err); } } else if (VideoProcessing.isVideo(fullFilePath)) { try { diff --git a/src/backend/model/fileaccess/MetadataLoader.ts b/src/backend/model/fileaccess/MetadataLoader.ts index 84ffa2be..5a61af21 100644 --- a/src/backend/model/fileaccess/MetadataLoader.ts +++ b/src/backend/model/fileaccess/MetadataLoader.ts @@ -41,7 +41,7 @@ export class MetadataLoader { metadata.fileSize = stat.size; metadata.creationDate = stat.mtime.getTime(); //Default date is file system time of last modification } catch (err) { - Logger.info(err); + console.log(err); // ignoring errors } try { @@ -214,7 +214,8 @@ export class MetadataLoader { try { await fileHandle.read(data, 0, bufferSize, 0); } catch (err) { - Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath + '\n', err); + Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath); + console.error(err); return MetadataLoader.EMPTY_METADATA; } finally { await fileHandle.close(); @@ -294,11 +295,13 @@ export class MetadataLoader { metadata.creationDate = 0; } } catch (err) { - Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath + '\n', err); + Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath); + console.error(err); return MetadataLoader.EMPTY_METADATA; } } catch (err) { - Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath + '\n', err); + Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath); + console.error(err); return MetadataLoader.EMPTY_METADATA; } return metadata; diff --git a/src/backend/model/fileaccess/TaskExecuter.ts b/src/backend/model/fileaccess/TaskExecuter.ts index 33ee75f3..4e203f03 100644 --- a/src/backend/model/fileaccess/TaskExecuter.ts +++ b/src/backend/model/fileaccess/TaskExecuter.ts @@ -1,6 +1,5 @@ import {TaskQue} from './TaskQue'; import {EventLoopHandler} from '../EventLoopHandler'; -import { Logger } from '../../Logger'; export interface ITaskExecuter { execute(input: I): Promise; @@ -31,7 +30,7 @@ export class TaskExecuter implements ITaskExecuter { execute(input: I): Promise { const promise = this.taskQue.add(input).promise.obj; - this.run().catch(Logger.error); + this.run().catch(console.error); return promise; } } diff --git a/src/backend/model/jobs/JobProgressManager.ts b/src/backend/model/jobs/JobProgressManager.ts index 4c637a2d..a1c46426 100644 --- a/src/backend/model/jobs/JobProgressManager.ts +++ b/src/backend/model/jobs/JobProgressManager.ts @@ -3,7 +3,6 @@ import * as path from 'path'; import {ProjectPath} from '../../ProjectPath'; import {Config} from '../../../common/config/private/Config'; import {JobProgressDTO, JobProgressStates,} from '../../../common/entities/job/JobProgressDTO'; -import { Logger } from '../../Logger'; export class JobProgressManager { private static readonly VERSION = 3; @@ -21,7 +20,7 @@ export class JobProgressManager { constructor() { this.dbPath = path.join(ProjectPath.DBFolder, 'jobs.db'); - this.loadDB().catch(Logger.error); + this.loadDB().catch(console.error); } get Progresses(): { [key: string]: JobProgressDTO } { @@ -90,7 +89,7 @@ export class JobProgressManager { return; } this.timer = setTimeout(async (): Promise => { - this.saveDB().catch(Logger.error); + this.saveDB().catch(console.error); this.timer = null; }, 5000); } diff --git a/src/backend/model/jobs/jobs/IndexingJob.ts b/src/backend/model/jobs/jobs/IndexingJob.ts index b85beb40..65973e85 100644 --- a/src/backend/model/jobs/jobs/IndexingJob.ts +++ b/src/backend/model/jobs/jobs/IndexingJob.ts @@ -93,7 +93,8 @@ export class IndexingJob< } catch (e) { this.Progress.log('Skipping. Indexing failed for: ' + directory); this.Progress.Skipped++; - Logger.warn(LOG_TAG, 'Skipping. Indexing failed for: ' + directory, + '\n', e); + Logger.warn(LOG_TAG, 'Skipping. Indexing failed for: ' + directory); + console.error(e); } if (this.Progress.State !== JobProgressStates.running) { return false; diff --git a/src/backend/model/jobs/jobs/Job.ts b/src/backend/model/jobs/jobs/Job.ts index ddf7c3f3..36a7a0cb 100644 --- a/src/backend/model/jobs/jobs/Job.ts +++ b/src/backend/model/jobs/jobs/Job.ts @@ -71,7 +71,7 @@ export abstract class Job = Record((resolve): void => { this.prResolve = resolve; }); - this.init().catch(Logger.error); + this.init().catch(console.error); this.run(); if (!this.IsInstant) { // if instant, wait for execution, otherwise, return right away diff --git a/src/backend/model/messenger/StdoutMessenger.ts b/src/backend/model/messenger/StdoutMessenger.ts index 86cabbb6..1edbf20a 100644 --- a/src/backend/model/messenger/StdoutMessenger.ts +++ b/src/backend/model/messenger/StdoutMessenger.ts @@ -1,7 +1,6 @@ import {MediaDTOWithThPath, Messenger} from './Messenger'; import {DynamicConfig} from '../../../common/entities/DynamicConfig'; import {DefaultMessengers} from '../../../common/entities/job/JobDTO'; -import { Logger } from '../../Logger'; export class StdoutMessenger extends Messenger { public readonly Name = DefaultMessengers[DefaultMessengers.Stdout]; @@ -13,6 +12,6 @@ export class StdoutMessenger extends Messenger { protected async sendMedia(config: never, media: MediaDTOWithThPath[]) { - Logger.info(media.map(m => m.thumbnailPath)); + console.log(media.map(m => m.thumbnailPath)); } } diff --git a/src/backend/routes/ErrorRouter.ts b/src/backend/routes/ErrorRouter.ts index 6fc76e96..3507e631 100644 --- a/src/backend/routes/ErrorRouter.ts +++ b/src/backend/routes/ErrorRouter.ts @@ -38,7 +38,8 @@ export class ErrorRouter { } // Flush out the stack to the console - Logger.error('Unexpected error:\n', err); + Logger.error('Unexpected error:'); + console.error(err); return next( new ErrorDTO( ErrorCodes.SERVER_ERROR, diff --git a/src/backend/server.ts b/src/backend/server.ts index 9c0a9213..382330e6 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -51,7 +51,7 @@ export class Server { 'Running in DEBUG mode, set env variable NODE_ENV=production to disable ' ); } - this.init(listen).catch(Logger.error); + this.init(listen).catch(console.error); } get Server(): HttpServer { From 2b26b3744be0fbbed4428c29715ee9e3fc379132 Mon Sep 17 00:00:00 2001 From: sarayourfriend Date: Sun, 31 Mar 2024 09:28:11 +1100 Subject: [PATCH 8/9] Revert "Remove duplicative logging" This reverts commit 25fb0ddb531a29dcc0fcefeedb986a1451d669b6. --- src/backend/middlewares/SharingMWs.ts | 1 + src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts | 2 ++ src/backend/model/jobs/jobs/FileJob.ts | 1 + src/backend/routes/ErrorRouter.ts | 2 ++ 4 files changed, 6 insertions(+) diff --git a/src/backend/middlewares/SharingMWs.ts b/src/backend/middlewares/SharingMWs.ts index 0776a032..e4552561 100644 --- a/src/backend/middlewares/SharingMWs.ts +++ b/src/backend/middlewares/SharingMWs.ts @@ -118,6 +118,7 @@ export class SharingMWs { ); return next(); } catch (err) { + console.warn(err); return next( new ErrorDTO( ErrorCodes.GENERAL_ERROR, diff --git a/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts b/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts index fc65efd0..a8943c09 100644 --- a/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts +++ b/src/backend/middlewares/thumbnail/ThumbnailGeneratorMWs.ts @@ -42,6 +42,7 @@ export class ThumbnailGeneratorMWs { ThumbnailGeneratorMWs.addThInfoToPhotos(cw.searchResult.media); } } catch (error) { + console.error(error); return next( new ErrorDTO( ErrorCodes.SERVER_ERROR, @@ -119,6 +120,7 @@ export class ThumbnailGeneratorMWs { req.resultPipe = await PhotoProcessing.generatePersonThumbnail(person); return next(); } catch (error) { + console.error(error); return next( new ErrorDTO( ErrorCodes.THUMBNAIL_GENERATION_ERROR, diff --git a/src/backend/model/jobs/jobs/FileJob.ts b/src/backend/model/jobs/jobs/FileJob.ts index bce773cc..2159973b 100644 --- a/src/backend/model/jobs/jobs/FileJob.ts +++ b/src/backend/model/jobs/jobs/FileJob.ts @@ -110,6 +110,7 @@ export abstract class FileJob Date: Sun, 31 Mar 2024 09:40:25 +1100 Subject: [PATCH 9/9] Narrow LoggerArgs types and fix bad revert --- src/backend/Logger.ts | 3 +-- src/backend/middlewares/RenderingMWs.ts | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/Logger.ts b/src/backend/Logger.ts index ddea11f3..b6ccb39e 100644 --- a/src/backend/Logger.ts +++ b/src/backend/Logger.ts @@ -11,8 +11,7 @@ if (forcedDebug === true) { ); } -// Match `console.log` inputs -export type LoggerArgs = Parameters[0] +export type LoggerArgs = (string | number | (() => string) | Record | Error); export type LoggerFunction = (...args: LoggerArgs[]) => void; export interface ILogger { diff --git a/src/backend/middlewares/RenderingMWs.ts b/src/backend/middlewares/RenderingMWs.ts index bb4ca0c1..12b26ad6 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -132,11 +132,12 @@ export class RenderingMWs { ): void { if (err instanceof ErrorDTO) { if (err.details) { + const logFn = Logger.logLevelForError(err.code) LoggerRouter.log(logFn, req, res); // use separate rendering for detailsStr const d = err.detailsStr; delete err.detailsStr; - console.log(err); + logFn(err); err.detailsStr = d; delete err.details; // do not send back error object to the client side