mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 09:04:44 -04:00
Utils: Add needToLog function
This functions makes it possible to make a partial order of the Loglevel enum. This simplifies the if statements in ConsoleLogger. This is done, because the Loglevel enum already has a string backing for easy conversion from the config environmental variables and therefore can't also have a ordinal number assigned… Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
0269b5e87a
commit
a039b85ff4
2 changed files with 70 additions and 0 deletions
|
@ -11,9 +11,11 @@
|
||||||
@typescript-eslint/require-await */
|
@typescript-eslint/require-await */
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
needToLog,
|
||||||
replaceAuthErrorsWithEnvironmentVariables,
|
replaceAuthErrorsWithEnvironmentVariables,
|
||||||
toArrayConfig,
|
toArrayConfig,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
import { Loglevel } from './loglevel.enum';
|
||||||
|
|
||||||
describe('config utils', () => {
|
describe('config utils', () => {
|
||||||
describe('toArrayConfig', () => {
|
describe('toArrayConfig', () => {
|
||||||
|
@ -46,4 +48,46 @@ describe('config utils', () => {
|
||||||
).toEqual('"HD_AUTH_GITLAB_test_SCOPE');
|
).toEqual('"HD_AUTH_GITLAB_test_SCOPE');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('needToLog', () => {
|
||||||
|
it('currentLevel ERROR', () => {
|
||||||
|
const currentLevel = Loglevel.ERROR;
|
||||||
|
expect(needToLog(currentLevel, Loglevel.ERROR)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.WARN)).toBeFalsy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.INFO)).toBeFalsy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.DEBUG)).toBeFalsy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeFalsy();
|
||||||
|
});
|
||||||
|
it('currentLevel WARN', () => {
|
||||||
|
const currentLevel = Loglevel.WARN;
|
||||||
|
expect(needToLog(currentLevel, Loglevel.ERROR)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.WARN)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.INFO)).toBeFalsy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.DEBUG)).toBeFalsy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeFalsy();
|
||||||
|
});
|
||||||
|
it('currentLevel INFO', () => {
|
||||||
|
const currentLevel = Loglevel.INFO;
|
||||||
|
expect(needToLog(currentLevel, Loglevel.ERROR)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.WARN)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.INFO)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.DEBUG)).toBeFalsy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeFalsy();
|
||||||
|
});
|
||||||
|
it('currentLevel DEBUG', () => {
|
||||||
|
const currentLevel = Loglevel.DEBUG;
|
||||||
|
expect(needToLog(currentLevel, Loglevel.ERROR)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.WARN)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.INFO)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.DEBUG)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeFalsy();
|
||||||
|
});
|
||||||
|
it('currentLevel TRACE', () => {
|
||||||
|
const currentLevel = Loglevel.TRACE;
|
||||||
|
expect(needToLog(currentLevel, Loglevel.ERROR)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.WARN)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.INFO)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.DEBUG)).toBeTruthy();
|
||||||
|
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
* SPDX-License-Identifier: AGPL-3.0-only
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Loglevel } from './loglevel.enum';
|
||||||
|
|
||||||
export function toArrayConfig(configValue: string, separator = ','): string[] {
|
export function toArrayConfig(configValue: string, separator = ','): string[] {
|
||||||
if (!configValue) {
|
if (!configValue) {
|
||||||
return [];
|
return [];
|
||||||
|
@ -87,3 +89,27 @@ export function replaceAuthErrorsWithEnvironmentVariables(
|
||||||
message = message.replace('.accessRole', '_ACCESS_ROLE');
|
message = message.replace('.accessRole', '_ACCESS_ROLE');
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function needToLog(
|
||||||
|
currentLoglevel: Loglevel,
|
||||||
|
requestedLoglevel: Loglevel,
|
||||||
|
): boolean {
|
||||||
|
const current = transformLoglevelToInt(currentLoglevel);
|
||||||
|
const requested = transformLoglevelToInt(requestedLoglevel);
|
||||||
|
return current >= requested;
|
||||||
|
}
|
||||||
|
|
||||||
|
function transformLoglevelToInt(loglevel: Loglevel): number {
|
||||||
|
switch (loglevel) {
|
||||||
|
case Loglevel.TRACE:
|
||||||
|
return 5;
|
||||||
|
case Loglevel.DEBUG:
|
||||||
|
return 4;
|
||||||
|
case Loglevel.INFO:
|
||||||
|
return 3;
|
||||||
|
case Loglevel.WARN:
|
||||||
|
return 2;
|
||||||
|
case Loglevel.ERROR:
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue