Commit af245f9e authored by jojohoch's avatar jojohoch
Browse files

[player] Add LogService with tests

- default log level is 1 (logs are not displayed,
but info, warn and error)
- uses its own logging module
- add path to logging module in tsconfig
parent 302bceff
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
declarations: [],
imports: [
CommonModule
]
})
export class LoggerModule { }
import { TestBed } from '@angular/core/testing';
import { LogLevels, LogService } from './log.service';
describe('LoggerService', () => {
let service: LogService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(LogService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should call LogService.log', () => {
spyOn(LogService, 'log')
.withArgs('test');
LogService.log('test');
expect(LogService.log).toHaveBeenCalled();
expect(LogService.log).toHaveBeenCalledWith('test');
});
it('should call LogService.info', () => {
spyOn(LogService, 'info')
.withArgs('test');
LogService.info('test');
expect(LogService.info).toHaveBeenCalled();
expect(LogService.info).toHaveBeenCalledWith('test');
});
it('should call LogService.warn', () => {
spyOn(LogService, 'warn')
.withArgs('test');
LogService.warn('test');
expect(LogService.warn).toHaveBeenCalled();
expect(LogService.warn).toHaveBeenCalledWith('test');
});
it('should call LogService.error', () => {
spyOn(LogService, 'error')
.withArgs('test');
LogService.error('test');
expect(LogService.error).toHaveBeenCalled();
expect(LogService.error).toHaveBeenCalledWith('test');
});
it('should set LogService.level to "LOG"', () => {
LogService.level = 0;
expect(LogService.level).toEqual(LogLevels.LOG);
});
it('should set LogService.level to "INFO"', () => {
LogService.level = 1;
expect(LogService.level).toEqual(LogLevels.INFO);
});
it('should set LogService.level to "WARN"', () => {
LogService.level = 2;
expect(LogService.level).toEqual(LogLevels.WARN);
});
it('should set LogService.level to "ERROR"', () => {
LogService.level = 3;
expect(LogService.level).toEqual(LogLevels.ERROR);
});
it('should set LogService.level to "NONE"', () => {
LogService.level = 4;
expect(LogService.level).toEqual(LogLevels.NONE);
});
});
import { Injectable } from '@angular/core';
export enum LogLevels { LOG = 0, INFO = 1, WARN = 2, ERROR = 3, NONE = 4}
@Injectable({
providedIn: 'root'
})
export class LogService {
static level: LogLevels = 0;
static error(...args: any[]): void {
if (LogService.level >= LogLevels.ERROR) {
window.console.error.apply( console, args );
}
}
static warn(...args: any[]): void {
if (LogService.level >= LogLevels.WARN) {
window.console.warn.apply( console, args );
}
}
static info(...args: any[]): void {
if (LogService.level >= LogLevels.INFO) {
window.console.info.apply( console, args );
}
}
static log(...args: any[]): void {
if (LogService.level >= LogLevels.LOG) {
window.console.info.apply( console, args );
}
}
}
......@@ -26,7 +26,8 @@
"common/*": ["./projects/common/*"],
"unit-definition-menu/*": ["./projects/player/modules/unit-definition-menu/*"],
"verona/*": ["./projects/player/modules/verona/*"],
"key-input/*": ["./projects/player/modules/key-input/*"]
"key-input/*": ["./projects/player/modules/key-input/*"],
"logging/*": ["./projects/player/modules/logging/*"]
}
},
"angularCompilerOptions": {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment