feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
This commit is contained in:
13
src/logging/abstract-logger.service.ts
Normal file
13
src/logging/abstract-logger.service.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Injectable } from 'injection-js';
|
||||
|
||||
/**
|
||||
* Implement this class-interface to create a custom logger service.
|
||||
*/
|
||||
@Injectable()
|
||||
export abstract class AbstractLoggerService {
|
||||
abstract logError(message: string | object, ...args: any[]): void;
|
||||
|
||||
abstract logWarning(message: string | object, ...args: any[]): void;
|
||||
|
||||
abstract logDebug(message: string | object, ...args: any[]): void;
|
||||
}
|
||||
17
src/logging/console-logger.service.ts
Normal file
17
src/logging/console-logger.service.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { Injectable } from 'injection-js';
|
||||
import { AbstractLoggerService } from './abstract-logger.service';
|
||||
|
||||
@Injectable()
|
||||
export class ConsoleLoggerService implements AbstractLoggerService {
|
||||
logError(message: string | object, ...args: any[]): void {
|
||||
console.error(message, ...args);
|
||||
}
|
||||
|
||||
logWarning(message: string | object, ...args: any[]): void {
|
||||
console.warn(message, ...args);
|
||||
}
|
||||
|
||||
logDebug(message: string | object, ...args: any[]): void {
|
||||
console.debug(message, ...args);
|
||||
}
|
||||
}
|
||||
6
src/logging/log-level.ts
Normal file
6
src/logging/log-level.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export enum LogLevel {
|
||||
None,
|
||||
Debug,
|
||||
Warn,
|
||||
Error,
|
||||
}
|
||||
252
src/logging/logger.service.spec.ts
Normal file
252
src/logging/logger.service.spec.ts
Normal file
@@ -0,0 +1,252 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AbstractLoggerService } from './abstract-logger.service';
|
||||
import { ConsoleLoggerService } from './console-logger.service';
|
||||
import { LogLevel } from './log-level';
|
||||
import { LoggerService } from './logger.service';
|
||||
|
||||
describe('Logger Service', () => {
|
||||
let loggerService: LoggerService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
LoggerService,
|
||||
{ provide: AbstractLoggerService, useClass: ConsoleLoggerService },
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
loggerService = TestBed.inject(LoggerService);
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(loggerService).toBeTruthy();
|
||||
});
|
||||
|
||||
describe('logError', () => {
|
||||
it('should not log error if loglevel is None', () => {
|
||||
const spy = spyOn(console, 'error');
|
||||
|
||||
loggerService.logError(
|
||||
{ configId: 'configId1', logLevel: LogLevel.None },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should log error as default if error is string', () => {
|
||||
const spy = spyOn(console, 'error');
|
||||
|
||||
loggerService.logError({ configId: 'configId1' }, 'some message');
|
||||
expect(spy).toHaveBeenCalledOnceWith('[ERROR] configId1 - some message');
|
||||
});
|
||||
|
||||
it('should log error as default if error is object', () => {
|
||||
const spy = spyOn(console, 'error');
|
||||
|
||||
loggerService.logError({ configId: 'configId1' }, { some: 'message' });
|
||||
expect(spy).toHaveBeenCalledOnceWith(
|
||||
'[ERROR] configId1 - {"some":"message"}'
|
||||
);
|
||||
});
|
||||
|
||||
it('should always log error with args', () => {
|
||||
const spy = spyOn(console, 'error');
|
||||
|
||||
loggerService.logError(
|
||||
{ configId: 'configId1' },
|
||||
'some message',
|
||||
'arg1',
|
||||
'arg2'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith(
|
||||
'[ERROR] configId1 - some message',
|
||||
'arg1',
|
||||
'arg2'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logWarn', () => {
|
||||
it('should not log if no log level is set (null)', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: undefined },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not log if no config is given', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning({}, 'some message');
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not log if no log level is set (undefined)', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning({ configId: 'configId1' }, 'some message');
|
||||
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not log if log level is turned off', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: LogLevel.None },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should log warning when loglevel is Warn and message is string', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Warn },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith('[WARN] configId1 - some message');
|
||||
});
|
||||
|
||||
it('should log warning when loglevel is Warn and message is object', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Warn },
|
||||
{ some: 'message' }
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith(
|
||||
'[WARN] configId1 - {"some":"message"}'
|
||||
);
|
||||
});
|
||||
|
||||
it('should log warning when loglevel is Warn with args', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Warn },
|
||||
'some message',
|
||||
'arg1',
|
||||
'arg2'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith(
|
||||
'[WARN] configId1 - some message',
|
||||
'arg1',
|
||||
'arg2'
|
||||
);
|
||||
});
|
||||
|
||||
it('should log warning when loglevel is Debug', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Debug },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith('[WARN] configId1 - some message');
|
||||
});
|
||||
|
||||
it('should not log warning when loglevel is error', () => {
|
||||
const spy = spyOn(console, 'warn');
|
||||
|
||||
loggerService.logWarning(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Error },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('logDebug', () => {
|
||||
it('should not log if no log level is set (null)', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: undefined },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not log if no log level is set (undefined)', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug({ configId: 'configId1' }, 'some message');
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not log if log level is turned off', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: LogLevel.None },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should log when loglevel is Debug and value is string', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Debug },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith('[DEBUG] configId1 - some message');
|
||||
});
|
||||
|
||||
it('should log when loglevel is Debug and value is object', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Debug },
|
||||
{ some: 'message' }
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith(
|
||||
'[DEBUG] configId1 - {"some":"message"}'
|
||||
);
|
||||
});
|
||||
|
||||
it('should log when loglevel is Debug with args', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Debug },
|
||||
'some message',
|
||||
'arg1',
|
||||
'arg2'
|
||||
);
|
||||
expect(spy).toHaveBeenCalledOnceWith(
|
||||
'[DEBUG] configId1 - some message',
|
||||
'arg1',
|
||||
'arg2'
|
||||
);
|
||||
});
|
||||
|
||||
it('should not log when loglevel is Warn', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Warn },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not log when loglevel is error', () => {
|
||||
const spy = spyOn(console, 'debug');
|
||||
|
||||
loggerService.logDebug(
|
||||
{ configId: 'configId1', logLevel: LogLevel.Error },
|
||||
'some message'
|
||||
);
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
144
src/logging/logger.service.ts
Normal file
144
src/logging/logger.service.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import { inject, Injectable } from 'injection-js';
|
||||
import { OpenIdConfiguration } from '../config/openid-configuration';
|
||||
import { AbstractLoggerService } from './abstract-logger.service';
|
||||
import { LogLevel } from './log-level';
|
||||
|
||||
@Injectable()
|
||||
export class LoggerService {
|
||||
private readonly abstractLoggerService = inject(AbstractLoggerService);
|
||||
|
||||
logError(
|
||||
configuration: OpenIdConfiguration,
|
||||
message: string | object,
|
||||
...args: any[]
|
||||
): void {
|
||||
if (this.loggingIsTurnedOff(configuration)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { configId } = configuration;
|
||||
const messageToLog = this.isObject(message)
|
||||
? JSON.stringify(message)
|
||||
: message;
|
||||
|
||||
if (!!args && !!args.length) {
|
||||
this.abstractLoggerService.logError(
|
||||
`[ERROR] ${configId} - ${messageToLog}`,
|
||||
...args
|
||||
);
|
||||
} else {
|
||||
this.abstractLoggerService.logError(
|
||||
`[ERROR] ${configId} - ${messageToLog}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
logWarning(
|
||||
configuration: OpenIdConfiguration,
|
||||
message: string | object,
|
||||
...args: any[]
|
||||
): void {
|
||||
if (!this.logLevelIsSet(configuration)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.loggingIsTurnedOff(configuration)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!this.currentLogLevelIsEqualOrSmallerThan(configuration, LogLevel.Warn)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { configId } = configuration;
|
||||
const messageToLog = this.isObject(message)
|
||||
? JSON.stringify(message)
|
||||
: message;
|
||||
|
||||
if (!!args && !!args.length) {
|
||||
this.abstractLoggerService.logWarning(
|
||||
`[WARN] ${configId} - ${messageToLog}`,
|
||||
...args
|
||||
);
|
||||
} else {
|
||||
this.abstractLoggerService.logWarning(
|
||||
`[WARN] ${configId} - ${messageToLog}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
logDebug(
|
||||
configuration: OpenIdConfiguration | null,
|
||||
message: string | object,
|
||||
...args: any[]
|
||||
): void {
|
||||
if (!configuration) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.logLevelIsSet(configuration)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.loggingIsTurnedOff(configuration)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!this.currentLogLevelIsEqualOrSmallerThan(configuration, LogLevel.Debug)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { configId } = configuration;
|
||||
const messageToLog = this.isObject(message)
|
||||
? JSON.stringify(message)
|
||||
: message;
|
||||
|
||||
if (!!args && !!args.length) {
|
||||
this.abstractLoggerService.logDebug(
|
||||
`[DEBUG] ${configId} - ${messageToLog}`,
|
||||
...args
|
||||
);
|
||||
} else {
|
||||
this.abstractLoggerService.logDebug(
|
||||
`[DEBUG] ${configId} - ${messageToLog}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private currentLogLevelIsEqualOrSmallerThan(
|
||||
configuration: OpenIdConfiguration,
|
||||
logLevelToCompare: LogLevel
|
||||
): boolean {
|
||||
const { logLevel } = configuration || {};
|
||||
|
||||
if (!logLevel) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return logLevel <= logLevelToCompare;
|
||||
}
|
||||
|
||||
private logLevelIsSet(configuration: OpenIdConfiguration): boolean {
|
||||
const { logLevel } = configuration || {};
|
||||
|
||||
if (logLevel === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return logLevel !== undefined;
|
||||
}
|
||||
|
||||
private loggingIsTurnedOff(configuration: OpenIdConfiguration): boolean {
|
||||
const { logLevel } = configuration || {};
|
||||
|
||||
return logLevel === LogLevel.None;
|
||||
}
|
||||
|
||||
private isObject(possibleObject: any): boolean {
|
||||
return Object.prototype.toString.call(possibleObject) === '[object Object]';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user