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

This commit is contained in:
2025-01-18 01:05:00 +08:00
parent 276d9fbda8
commit 983254164b
201 changed files with 35689 additions and 0 deletions

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

View 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
View File

@@ -0,0 +1,6 @@
export enum LogLevel {
None,
Debug,
Warn,
Error,
}

View 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();
});
});
});

View 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]';
}
}