Skip to content

Commit

Permalink
5장-[mouse-pos-logger] Host 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
not-for-me authored and woojin.joe committed May 15, 2017
1 parent 3891f7f commit 401fea2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
15 changes: 15 additions & 0 deletions ch05/mouse-pos-logger/src/app/another-logger.service.spec.ts
@@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';

import { AnotherLoggerService } from './another-logger.service';

describe('AnotherLoggerService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AnotherLoggerService]
});
});

it('should ...', inject([AnotherLoggerService], (service: AnotherLoggerService) => {
expect(service).toBeTruthy();
}));
});
22 changes: 22 additions & 0 deletions ch05/mouse-pos-logger/src/app/another-logger.service.ts
@@ -0,0 +1,22 @@
import { Injectable, Inject } from '@angular/core';
import { LogLevel } from './log-level.enum';
import { LOG_LEVEL_TOKEN } from './app.tokens'
import { LoggerService } from './logger-service';

@Injectable()
export class AnotherLoggerService extends LoggerService {
constructor( @Inject(LOG_LEVEL_TOKEN) logLevel: LogLevel) {
super(logLevel);
}

log(logLevel: LogLevel, msg: string) {
const logMsg = this.getFormattedLogMsg(logLevel, msg);
if (this.isProperLogLevel(logLevel)) {
console.log(logMsg);
}
}

private getFormattedLogMsg(logLevel: LogLevel, msg: string) {
return `[${LogLevel[logLevel]}] - ${msg}`;
}
}
7 changes: 6 additions & 1 deletion ch05/mouse-pos-logger/src/app/app.module.ts
Expand Up @@ -7,6 +7,7 @@ import { AppComponent } from './app.component';
import { MouseTrackZoneComponent } from './mouse-track-zone/mouse-track-zone.component';

import { MySpecialLoggerService } from './my-special-logger.service';
import { AnotherLoggerService } from './another-logger.service';

import { LogLevel } from './log-level.enum';
import { LOG_LEVEL_TOKEN } from './app.tokens';
Expand All @@ -22,7 +23,11 @@ import { LOG_LEVEL_TOKEN } from './app.tokens';
FormsModule,
HttpModule
],
providers: [MySpecialLoggerService, { provide: LOG_LEVEL_TOKEN, useValue: LogLevel.INFO }],
providers: [
MySpecialLoggerService,
AnotherLoggerService,
{ provide: LOG_LEVEL_TOKEN, useValue: LogLevel.INFO }
],
bootstrap: [AppComponent]
})
export class AppModule { }
32 changes: 32 additions & 0 deletions ch05/mouse-pos-logger/src/app/logger-service.ts
@@ -0,0 +1,32 @@
import { LogLevel } from './log-level.enum';

export abstract class LoggerService {
logLevel: LogLevel;

constructor(logLevel: LogLevel) {
this.logLevel = logLevel;
}

debug(msg: string) {
this.log(LogLevel.DEBUG, msg);
}

info(msg: string) {
this.log(LogLevel.INFO, msg);
}

warn(msg: string) {
this.log(LogLevel.WARN, msg);
}

error(msg: string) {
this.log(LogLevel.ERROR, msg);
}

abstract log(logLevel: LogLevel, msg: string);

protected isProperLogLevel(logLevel: LogLevel): boolean {
if (this.logLevel === LogLevel.DEBUG) return true;
return logLevel >= this.logLevel;
}
}
@@ -1,5 +1,7 @@
import { Component, OnInit, Input } from '@angular/core';
import { Component, OnInit, Host, Optional } from '@angular/core';
import { LoggerService } from '../logger-service';
import { MySpecialLoggerService } from '../my-special-logger.service';
import { AnotherLoggerService } from '../another-logger.service';
import { LogLevel } from '../log-level.enum';
import { LOG_LEVEL_TOKEN } from '../app.tokens';

Expand All @@ -10,8 +12,15 @@ import { LOG_LEVEL_TOKEN } from '../app.tokens';
providers: [MySpecialLoggerService, { provide: LOG_LEVEL_TOKEN, useValue: LogLevel.DEBUG }]
})
export class MouseTrackZoneComponent implements OnInit {
logger: LoggerService;

constructor(
@Host() mySpecialLogger: MySpecialLoggerService,
anotherLogger: AnotherLoggerService
) {
this.logger = mySpecialLogger ? mySpecialLogger : anotherLogger;
}

constructor(private logger: MySpecialLoggerService) { }

ngOnInit() {
}
Expand Down
19 changes: 4 additions & 15 deletions ch05/mouse-pos-logger/src/app/my-special-logger.service.ts
Expand Up @@ -2,22 +2,17 @@ import { Injectable, Inject } from '@angular/core';
import { LogLevel } from './log-level.enum';
import * as format from 'date-fns/format';
import { LOG_LEVEL_TOKEN } from './app.tokens';
import { LoggerService } from './logger-service';

@Injectable()
export class MySpecialLoggerService {
logLevel: LogLevel;
export class MySpecialLoggerService extends LoggerService {
logs: string[] = [];
private readonly MAX_HISTORY_CNT: number = 100;
private readonly TIME_FORMATTER: string = "YYYY-MM-DD HH:mm:ss.SSS";

constructor( @Inject(LOG_LEVEL_TOKEN) logLevel: LogLevel) {
this.logLevel = logLevel
};

debug(msg: string) { this.log(LogLevel.DEBUG, msg); }
info(msg: string) { this.log(LogLevel.INFO, msg); }
warn(msg: string) { this.log(LogLevel.WARN, msg); }
error(msg: string) { this.log(LogLevel.ERROR, msg); }
super(logLevel);
}

log(logLevel: LogLevel, msg: string) {
const logMsg = this.getFormattedLogMsg(logLevel, msg);
Expand All @@ -38,10 +33,4 @@ export class MySpecialLoggerService {
const curTimestamp = format(new Date(), this.TIME_FORMATTER);
return `[${LogLevel[logLevel]}] ${curTimestamp} - ${msg}`;
}

private isProperLogLevel(logLevel: LogLevel): boolean {
if (this.logLevel === LogLevel.DEBUG) return true;
return logLevel >= this.logLevel;
}

}

0 comments on commit 401fea2

Please sign in to comment.