Skip to content

Commit 6c53aca

Browse files
authored
Merge pull request #863 from sarayourfriend/fix/unify-logging
Use separate log levels for relevant error types
2 parents 086bc47 + 409af38 commit 6c53aca

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/backend/Logger.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Config} from '../common/config/private/Config';
22
import {LogLevel} from '../common/config/private/PrivateConfig';
3+
import {ErrorCodes} from '../common/entities/Error';
34

4-
export type logFN = (...args: (string | number | (() => string))[]) => void;
55

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

@@ -11,7 +11,7 @@ if (forcedDebug === true) {
1111
);
1212
}
1313

14-
export type LoggerArgs = (string | number | (() => string))
14+
export type LoggerArgs = (string | number | (() => string) | Record<any, unknown> | Error);
1515
export type LoggerFunction = (...args: LoggerArgs[]) => void;
1616

1717
export interface ILogger {
@@ -103,4 +103,25 @@ export class Logger {
103103
});
104104
console.log(date + tag + LOG_TAG, ...args);
105105
}
106+
107+
public static logLevelForError(e: ErrorCodes): LoggerFunction {
108+
switch (e) {
109+
case ErrorCodes.INPUT_ERROR:
110+
case ErrorCodes.NOT_AUTHENTICATED:
111+
case ErrorCodes.ALREADY_AUTHENTICATED:
112+
case ErrorCodes.NOT_AUTHORISED:
113+
case ErrorCodes.PERMISSION_DENIED:
114+
case ErrorCodes.CREDENTIAL_NOT_FOUND:
115+
return Logger.debug
116+
case ErrorCodes.SETTINGS_ERROR:
117+
case ErrorCodes.TASK_ERROR:
118+
case ErrorCodes.JOB_ERROR:
119+
case ErrorCodes.THUMBNAIL_GENERATION_ERROR:
120+
case ErrorCodes.PHOTO_GENERATION_ERROR:
121+
case ErrorCodes.SERVER_ERROR:
122+
return Logger.error
123+
default:
124+
return Logger.warn
125+
}
126+
}
106127
}

src/backend/middlewares/RenderingMWs.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,12 @@ export class RenderingMWs {
132132
): void {
133133
if (err instanceof ErrorDTO) {
134134
if (err.details) {
135-
Logger.warn('Handled error:');
136-
LoggerRouter.log(Logger.warn, req, res);
135+
const logFn = Logger.logLevelForError(err.code)
136+
LoggerRouter.log(logFn, req, res);
137137
// use separate rendering for detailsStr
138138
const d = err.detailsStr;
139139
delete err.detailsStr;
140-
console.log(err);
141-
if (err.detailsStr) {
142-
try {
143-
console.log('details:', JSON.stringify(err.detailsStr));
144-
} catch (_) {
145-
console.log(err.detailsStr);
146-
}
147-
}
140+
logFn(err);
148141
err.detailsStr = d;
149142
delete err.details; // do not send back error object to the client side
150143

src/backend/routes/LoggerRouter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Express, NextFunction, Request, Response} from 'express';
2-
import {logFN, Logger} from '../Logger';
2+
import {LoggerFunction, Logger} from '../Logger';
33
import {Config} from '../../common/config/private/Config';
44

55
declare global {
@@ -16,7 +16,7 @@ declare global {
1616
* Adds logging to express
1717
*/
1818
export class LoggerRouter {
19-
public static log(loggerFn: logFN, req: Request, res: Response): void {
19+
public static log(loggerFn: LoggerFunction, req: Request, res: Response): void {
2020
if (req.logged === true) {
2121
return;
2222
}

0 commit comments

Comments
 (0)