Sonsino, Ofir (os0695) | ff76b5e | 2018-07-10 15:57:37 +0300 | [diff] [blame] | 1 | import {isDevMode} from "@angular/core"; |
| 2 | declare var console: any; |
| 3 | |
| 4 | export interface ILogger { |
| 5 | assert(...args: any[]): void; |
| 6 | error(...args: any[]): void; |
| 7 | group(...args: any[]): void; |
| 8 | groupEnd(...args: any[]): void; |
| 9 | info(...args: any[]): void; |
| 10 | log(...args: any[]): void; |
| 11 | warn(...args: any[]): void; |
| 12 | } |
| 13 | |
| 14 | export class LogService implements ILogger { |
| 15 | |
| 16 | isProduction : boolean = !isDevMode(); |
| 17 | public assert(...args: any[]): void { |
| 18 | console.assert(LogService.getPrefixLog(...args)); |
| 19 | } |
| 20 | |
| 21 | public error(...args: any[]): void { |
| 22 | console.error(LogService.getPrefixLog(...args)); |
| 23 | } |
| 24 | |
| 25 | public group(...args: any[]): void { |
| 26 | console.group(LogService.getPrefixLog(...args)); |
| 27 | } |
| 28 | |
| 29 | public groupEnd(...args: any[]): void { |
| 30 | console.groupEnd(LogService.getPrefixLog(...args)); |
| 31 | } |
| 32 | |
| 33 | public info(...args: any[]): void { |
| 34 | console.info(LogService.getPrefixLog(...args)); |
| 35 | } |
| 36 | |
| 37 | public log(...args: any[]): void { |
| 38 | if(!this.isProduction){ |
| 39 | console.log(LogService.getPrefixLog(...args)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public warn(...args: any[]): void { |
| 44 | console.warn(LogService.getPrefixLog(...args)); |
| 45 | } |
| 46 | |
| 47 | static getPrefixLog(...args :any[]){ |
| 48 | return { |
| 49 | time : new Date(), |
| 50 | message : args[0], |
| 51 | data : args.length > 0 ? args[1] : '' |
| 52 | }; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | |