Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3891f7f
commit 401fea2
Showing
6 changed files
with
90 additions
and
18 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
ch05/mouse-pos-logger/src/app/another-logger.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters