1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-23 01:27:14 +02:00

Merge pull request #863 from sarayourfriend/fix/unify-logging

Use separate log levels for relevant error types
This commit is contained in:
Patrik J. Braun 2024-04-01 13:10:58 +02:00 committed by GitHub
commit 6c53aca7e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 14 deletions

View File

@ -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<any, unknown> | 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
}
}
}

View File

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

View File

@ -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;
}