Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate log levels for relevant error types #863

Merged
merged 9 commits into from
Apr 1, 2024
25 changes: 23 additions & 2 deletions src/backend/Logger.ts
Original file line number Diff line number Diff line change
@@ -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;
bpatrik marked this conversation as resolved.
Show resolved Hide resolved

const forcedDebug = process.env['NODE_ENV'] === 'debug';

Expand All @@ -11,7 +11,7 @@
);
}

export type LoggerArgs = (string | number | (() => string))
export type LoggerArgs = (string | number | (() => string) | Record<any, unknown> | Error);

Check warning on line 14 in src/backend/Logger.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Unexpected any. Specify a different type
export type LoggerFunction = (...args: LoggerArgs[]) => void;

export interface ILogger {
Expand Down Expand Up @@ -103,4 +103,25 @@
});
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
}
}
}
13 changes: 3 additions & 10 deletions src/backend/middlewares/RenderingMWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
bpatrik marked this conversation as resolved.
Show resolved Hide resolved
logFn(err);
err.detailsStr = d;
delete err.details; // do not send back error object to the client side

Expand Down
4 changes: 2 additions & 2 deletions src/backend/routes/LoggerRouter.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
Expand Down
Loading