import { Injectable , Optional } from "@angular/core" ; import { Level } from "./level" ; /**
 * Logger options.
 * See  Logger .
 *
 * level - How much detail you want to see in the logs, 0 being off, 1 being the less detailed, 5 being the most. Defaults to WARN.
 * global - Whether you want the created logger object to be exposed in the global scope. Defaults to true.
 * globalAs - The window's property name that will hold the logger object created. Defaults to 'logger'.
 * 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.
 * 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'.
 *
 * Created by Langley on 3/23/2016.
 *
  export class Options { level : Level ; global : boolean ; globalAs : string ; store : boolean ; storeAs : string ; } const CONSOLE_DEBUG_METHOD = console [ "debug" ] ? "debug" : "log" ; const DEFAULT_OPTIONS = { level : Level . WARN , global : true , globalAs : "logger" , store : false , storeAs : "angular2.logger.level" } ; @ Injectable ( ) export class Logger { private _level : Level ; private _globalAs : string ; private _store : boolean ; private _storeAs : string ; public Level : any = Level ; constructor ( @ Optional ( ) options ? ) { let { level , global , globalAs , store , storeAs } = Object . assign ( { } , DEFAULT_OPTIONS , options ) ; this . _level = level ; this . _globalAs = globalAs ; this . _storeAs = storeAs ; global && this . global ( ) ; if ( store || this . _loadLevel ( ) ) this . store ( ) ; } private _loadLevel = ( ) : Level => Number ( localStorage . getItem ( this . _storeAs ) ) ; private _storeLevel ( level ) { localStorage [ this . _storeAs ] = level ; } error ( message ? , ... optionalParams ) { this . isErrorEnabled ( ) && console . error . apply ( console , arguments ) ; } warn ( message ? , ... optionalParams ) { this . isWarnEnabled ( ) && console . warn . apply ( console , arguments ) ; } info ( message ? , ... optionalParams ) { this . isInfoEnabled ( ) && console . info . apply ( console , arguments ) ; } debug ( message ? , ... optionalParams ) { this . isDebugEnabled ( ) && ( < any > console ) [ CONSOLE_DEBUG_METHOD ] . apply ( console , arguments ) ; } log ( message ? , ... optionalParams ) { this . isLogEnabled ( ) && console . log . apply ( console , arguments ) ; } global = ( ) => ( < any > window ) [ this . _globalAs ] = this ; store ( ) { this . _store = true ; let storedLevel = this . _loadLevel ( ) ; if ( storedLevel ) { this . _level = storedLevel ; } else { this . _storeLevel ( this . level ) ; } return this ; } unstore ( ) { this . _store = false ; localStorage . removeItem ( this . _storeAs ) ; return this ; } isErrorEnabled = ( ) : boolean => this . level >= Level . ERROR ; isWarnEnabled = ( ) : boolean => this . level >= Level . WARN ; isInfoEnabled = ( ) : boolean => this . level >= Level . INFO ; isDebugEnabled = ( ) : boolean => this . level >= Level . DEBUG ; isLogEnabled = ( ) : boolean => this . level >= Level . LOG ; get level ( ) : Level { return this . _level ; } set level ( level ) { this . _store && this . _storeLevel ( level ) ; this . _level = level ; } }