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

refactor(logger): remove DefineLogger and InjectEnum #193

Merged
merged 5 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LoaderFactory, Manifest } from './loader';
import { Application, ApplicationInitOptions } from './types';
import Trigger from './trigger';
import ConfigurationHandler from './configuration';
import { ArtusLogger, Logger } from './logger';
import { Logger } from './logger';

export class ArtusApplication implements Application {
public manifest?: Manifest;
Expand Down Expand Up @@ -36,10 +36,6 @@ export class ArtusApplication implements Application {
return this.container.get(ArtusInjectEnum.Frameworks);
}

get logger(): Logger {
return this.container.get(ArtusInjectEnum.Logger);
}

get packages(): Record<string, any> {
return this.container.get(ArtusInjectEnum.Packages);
}
Expand All @@ -59,7 +55,7 @@ export class ArtusApplication implements Application {
this.container.set({ id: ArtusInjectEnum.LifecycleManager, value: this.lifecycleManager });

this.container.set({ type: ConfigurationHandler });
this.container.set({ type: ArtusLogger });
this.container.set({ type: Logger });
this.container.set({ type: Trigger });
this.container.set({ type: ExceptionHandler });
}
Expand Down
1 change: 0 additions & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export enum ArtusInjectEnum {
DefaultContainerName = 'artus#default_container',
Frameworks = 'artus#framework-config',
LifecycleManager = 'artus#lifecycle-manager',
Logger = 'artus#logger',
Packages = 'artus#packages',
}

Expand Down
56 changes: 0 additions & 56 deletions src/logger/base.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/logger/decorator.ts

This file was deleted.

35 changes: 29 additions & 6 deletions src/logger/impl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import { BaseLogger } from './base';
import { DefineLogger } from './decorator';
import { LoggerLevel } from './level';
import { LogOptions } from './types';
import { Container, Inject, Injectable, ScopeEnum } from '@artus/injection';
import { ArtusInjectEnum } from '../constant';
import { LoggerLevel, LOGGER_LEVEL_MAP } from './level';
import { LoggerOptions, LoggerType, LogOptions } from './types';

@Injectable({
scope: ScopeEnum.SINGLETON,
})
export default class Logger implements LoggerType {
@Inject()
protected container!: Container;

protected get loggerOpts(): LoggerOptions {
let appConfig: Record<string, any> = {};
try {
appConfig = this.container.get(ArtusInjectEnum.Config);
} catch(e) {
// do nothing
}
return appConfig?.logger ?? {};
}

protected checkLoggerLevel(level: LoggerLevel) {
const targetLevel = this.loggerOpts.level ?? LoggerLevel.INFO;
if (LOGGER_LEVEL_MAP[level] < LOGGER_LEVEL_MAP[targetLevel]) {
return false;
}
return true;
}

@DefineLogger()
export default class ArtusLogger extends BaseLogger {
public trace(message: string, ...args: any[]) {
if (!this.checkLoggerLevel(LoggerLevel.TRACE)) {
return;
Expand Down
6 changes: 2 additions & 4 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import ArtusLogger from './impl';
import Logger from './impl';

export * from './decorator';
export { Logger };
export * from './level';
export * from './types';

export { ArtusLogger };
2 changes: 1 addition & 1 deletion src/logger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface LogOptions {
splat?: any[];
}

export interface Logger {
export interface LoggerType {
trace(message: string, ...args: any[]): void;
debug(message: string, ...args: any[]): void;
info(message: string, ...args: any[]): void;
Expand Down
1 change: 0 additions & 1 deletion test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('test/app.test.ts', () => {
// Check Artus Default Class Inject to Contianer
expect(() => app.container.get(ArtusInjectEnum.Application)).not.toThrow();
expect(() => app.container.get(ArtusInjectEnum.LifecycleManager)).not.toThrow();
expect(() => app.container.get(ArtusInjectEnum.Logger)).not.toThrow();
expect(() => app.container.get(ExceptionHandler)).not.toThrow();
expect(() => app.container.get(ConfigurationHandler)).not.toThrow();

Expand Down
9 changes: 5 additions & 4 deletions test/fixtures/logger/src/custom_logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DefineLogger } from '../../../../src/logger';
import { BaseLogger } from '../../../../src/logger/base';
import { Injectable, Logger, ScopeEnum } from '../../../../src';

@DefineLogger()
export default class CustomLogger extends BaseLogger {
@Injectable({
scope: ScopeEnum.SINGLETON,
})
export default class CustomLogger extends Logger {
public info(message: string, ...args: any[]): void {
console.info('[Custom]', message, ...args);
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/logger/src/test_clazz.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Inject, Injectable, ScopeEnum } from '@artus/injection';
import { ArtusLogger, LoggerLevel } from '../../../../src/logger';
import { Logger, LoggerLevel } from '../../../../src/logger';

@Injectable({
scope: ScopeEnum.SINGLETON,
})
export default class TestLoggerClazz {
@Inject()
private logger!: ArtusLogger;
private logger!: Logger;

public testLog(level: LoggerLevel, message: string | Error, ...splat: any[]) {
this.logger.log({
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/logger/src/test_custom_clazz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import CustomLogger from './custom_logger';
})
export default class TestCustomLoggerClazz {
@Inject()
private logger!: CustomLogger;
private logger: CustomLogger;

public testInfo(message: string, ...args: any[]) {
this.logger.info(message, ...args);
Expand Down
Loading