diff --git a/src/backend/Logger.ts b/src/backend/Logger.ts index 3cb26270..b6ccb39e 100644 --- a/src/backend/Logger.ts +++ b/src/backend/Logger.ts @@ -1,7 +1,7 @@ import {Config} from '../common/config/private/Config'; import {LogLevel} from '../common/config/private/PrivateConfig'; +import {ErrorCodes} from '../common/entities/Error'; -export type logFN = (...args: (string | number | (() => string))[]) => void; const forcedDebug = process.env['NODE_ENV'] === 'debug'; @@ -11,7 +11,7 @@ if (forcedDebug === true) { ); } -export type LoggerArgs = (string | number | (() => string)) +export type LoggerArgs = (string | number | (() => string) | Record | Error); export type LoggerFunction = (...args: LoggerArgs[]) => void; export interface ILogger { @@ -103,4 +103,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 f4022dab..12b26ad6 100644 --- a/src/backend/middlewares/RenderingMWs.ts +++ b/src/backend/middlewares/RenderingMWs.ts @@ -132,19 +132,12 @@ export class RenderingMWs { ): void { if (err instanceof ErrorDTO) { if (err.details) { - Logger.warn('Handled error:'); - LoggerRouter.log(Logger.warn, req, res); + 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); - if (err.detailsStr) { - try { - console.log('details:', JSON.stringify(err.detailsStr)); - } catch (_) { - console.log(err.detailsStr); - } - } + logFn(err); err.detailsStr = d; delete err.details; // do not send back error object to the client side 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; }