diff options
Diffstat (limited to 'vid-webpack-master/src/app/shared/utils/log')
-rw-r--r-- | vid-webpack-master/src/app/shared/utils/log/log.service.spec.ts | 29 | ||||
-rw-r--r-- | vid-webpack-master/src/app/shared/utils/log/log.service.ts | 58 |
2 files changed, 87 insertions, 0 deletions
diff --git a/vid-webpack-master/src/app/shared/utils/log/log.service.spec.ts b/vid-webpack-master/src/app/shared/utils/log/log.service.spec.ts new file mode 100644 index 00000000..ea0eb049 --- /dev/null +++ b/vid-webpack-master/src/app/shared/utils/log/log.service.spec.ts @@ -0,0 +1,29 @@ + + +import {LogService} from "./log.service"; + +describe('log service service', () => { + let logService : LogService; + + beforeEach(() => { + logService = new LogService(); + }); + + + it('check all ILogger function are defined', ()=>{ + expect(logService.log).toBeDefined(); + expect(logService.assert).toBeDefined(); + expect(logService.error).toBeDefined(); + expect(logService.group).toBeDefined(); + expect(logService.groupEnd).toBeDefined(); + expect(logService.info).toBeDefined(); + expect(logService.warn).toBeDefined(); + }); + + it('test getPrefixLog function', ()=> { + let args = ['message', [1,2,3,4,5]]; + let result = LogService.getPrefixLog(args); + expect(result).toBeDefined(); + }); + +}); diff --git a/vid-webpack-master/src/app/shared/utils/log/log.service.ts b/vid-webpack-master/src/app/shared/utils/log/log.service.ts new file mode 100644 index 00000000..422fe013 --- /dev/null +++ b/vid-webpack-master/src/app/shared/utils/log/log.service.ts @@ -0,0 +1,58 @@ +import {isDevMode} from "@angular/core"; +declare var console: any; + +export interface ILogger { + assert(...args: any[]): void; + error(...args: any[]): void; + group(...args: any[]): void; + groupEnd(...args: any[]): void; + info(...args: any[]): void; + log(...args: any[]): void; + warn(...args: any[]): void; +} + +export class LogService implements ILogger { + + isProduction : boolean = !isDevMode(); + public assert(...args: any[]): void { + console.assert(LogService.getPrefixLog(...args)); + } + + public error(...args: any[]): void { + console.error(LogService.getPrefixLog(...args)); + } + + public group(...args: any[]): void { + console.group(LogService.getPrefixLog(...args)); + } + + public groupEnd(...args: any[]): void { + console.groupEnd(LogService.getPrefixLog(...args)); + } + + public info(...args: any[]): void { + console.info(LogService.getPrefixLog(...args)); + } + + public log(...args: any[]): void { + if(!this.isProduction){ + console.log(LogService.getPrefixLog(...args)); + } + } + + public warn(...args: any[]): void { + console.warn(LogService.getPrefixLog(...args)); + } + + static getPrefixLog(...args :any[]){ + return { + time : new Date(), + message : args[0], + data : args.length > 0 ? args[1] : '' + }; + } +} + + + + |