|
| 1 | +import {Injectable, Optional} from "angular2/core"; |
| 2 | +import {Level} from "./level.ts"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Logger options. |
| 6 | + * See {@link Logger}. |
| 7 | + * |
| 8 | + * level - How much detail you want to see in the logs, 1 being the less detailed, 5 being the most. Defaults to WARN. |
| 9 | + * global - Whether you want the created logger object to be exposed in the global scope. Defaults to true. |
| 10 | + * globalAs - The window's property name that will hold the logger object created. Defaults to 'logger'. |
| 11 | + * store - Whether you want the level config to be saved in the local storage so it doesn't get lost when you refresh. Defaults to false. |
| 12 | + * storeAs - The local storage key that will be used to save the level config if the store setting is true. Defaults to 'angular2.logger.level'. |
| 13 | + * |
| 14 | + * Created by Langley on 3/23/2016. |
| 15 | + * |
| 16 | + */ |
| 17 | +export class Options { |
| 18 | + level: Level; |
| 19 | + global: boolean; |
| 20 | + globalAs: string; |
| 21 | + store: boolean; |
| 22 | + storeAs: string; |
| 23 | +} |
| 24 | + |
| 25 | +// Temporal until https://github.com/angular/angular/issues/7344 gets fixed. |
| 26 | +const DEFAULT_OPTIONS: Options = { |
| 27 | + level: Level.WARN, |
| 28 | + global: true, |
| 29 | + globalAs: "logger", |
| 30 | + store: false, |
| 31 | + storeAs: "angular2.logger.level" |
| 32 | +}; |
| 33 | + |
| 34 | +@Injectable() |
| 35 | +export class Logger { |
| 36 | + |
| 37 | + private _level: Level; |
| 38 | + private _globalAs: string; |
| 39 | + private _store: boolean; |
| 40 | + private _storeAs: string; |
| 41 | + |
| 42 | + Level = Level; |
| 43 | + |
| 44 | + constructor( @Optional() options?: Options ) { |
| 45 | + |
| 46 | + // Move this to the constructor definition when optional parameters are working with @Injectable: https://github.com/angular/angular/issues/7344 |
| 47 | + let { level, global, globalAs, store, storeAs } = Object.assign( {}, DEFAULT_OPTIONS, options ); |
| 48 | + |
| 49 | + this._level = level; |
| 50 | + this._globalAs = globalAs; |
| 51 | + this._storeAs = storeAs; |
| 52 | + |
| 53 | + global && this.global(); |
| 54 | + |
| 55 | + if ( store || this._loadLevel() ) this.store(); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + private _loadLevel = (): Level => localStorage.getItem( this._storeAs ); |
| 60 | + private _storeLevel(level: Level) { localStorage[ this._storeAs ] = level; } |
| 61 | + |
| 62 | + error(message?: any, ...optionalParams: any[]) { |
| 63 | + this.isErrorEnabled() && console.error( message, ...optionalParams ); |
| 64 | + } |
| 65 | + |
| 66 | + warn(message?: any, ...optionalParams: any[]) { |
| 67 | + this.isWarnEnabled() && console.warn( message, ...optionalParams ); |
| 68 | + } |
| 69 | + |
| 70 | + info(message?: any, ...optionalParams: any[]) { |
| 71 | + this.isInfoEnabled() && console.info( message, ...optionalParams ); |
| 72 | + } |
| 73 | + |
| 74 | + debug(message?: any, ...optionalParams: any[]) { |
| 75 | + this.isDebugEnabled() && console.debug( message, ...optionalParams ); |
| 76 | + } |
| 77 | + |
| 78 | + log(message?: any, ...optionalParams: any[]) { |
| 79 | + this.isLogEnabled() && console.log( message, ...optionalParams ); |
| 80 | + } |
| 81 | + |
| 82 | + global = () => window[this._globalAs] = this; |
| 83 | + |
| 84 | + store(): Logger { |
| 85 | + |
| 86 | + this._store = true; |
| 87 | + let storedLevel = this._loadLevel(); |
| 88 | + if ( storedLevel ) { this._level = storedLevel; } |
| 89 | + else { this._storeLevel( this.level ); } |
| 90 | + |
| 91 | + return this; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + unstore(): Logger { |
| 96 | + this._store = false; |
| 97 | + localStorage.removeItem( this._storeAs ); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + isErrorEnabled = (): boolean => this.level >= Level.ERROR; |
| 102 | + isWarnEnabled = (): boolean => this.level >= Level.WARN; |
| 103 | + isInfoEnabled = (): boolean => this.level >= Level.INFO; |
| 104 | + isDebugEnabled = (): boolean => this.level >= Level.DEBUG; |
| 105 | + isLogEnabled = (): boolean => this.level >= Level.LOG; |
| 106 | + |
| 107 | + get level(): Level { return this._level; } |
| 108 | + |
| 109 | + set level(level: Level) { |
| 110 | + this._store && this._storeLevel(level); |
| 111 | + this._level = level; |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments