angular2-logger
A simpler Log4j inspired logger module for Angular 2.
1) Install the npm module.
npm install --save angular2-logger
2) Add the angular2-logger library to your app. If you are following the Angular 2's Quickstart Guide it should be something like this:
In systemjs.config.js:
// map tells the System loader where to look for things
var map = {
'app': 'app', // 'dist',
'@angular': 'node_modules/@angular',
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
'rxjs': 'node_modules/rxjs',
'angular2-logger': 'node_modules/angular2-logger' // ADD THIS
};
//packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.ts', defaultExtension: 'ts' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { defaultExtension: 'js' },
'angular2-logger': { defaultExtension: 'js' }, // AND THIS
};
3) Setup the Provider.
In app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { Logger } from "angular2-logger/core"; // ADD THIS
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [ Logger ] // AND THIS
})
export class AppModule { }
4) Inject the logger into your objects and use it.
@Component({
...
})
export class AppComponent(){
constructor( private _logger: Logger ){
this._logger.error('This is a priority level 1 error message...');
this._logger.warn('This is a priority level 2 warning message...');
this._logger.info('This is a priority level 3 warning message...');
this._logger.debug('This is a priority level 4 debug message...');
this._logger.log('This is a priority level 5 log message...');
}
}
In order to see all of the messages you just need to change the logger message hierarchy level, you can do so:
logger.level = logger.Level.LOG; // same as: logger.level = 5;
Or using one of the predefined configuration providers:
import {LOG_LOGGER_PROVIDERS} from "angular2-logger/core";
@NgModule({
...
providers: [ LOG_LOGGER_PROVIDERS ]
})
export class AppModule { }
The available Providers are:
ERROR_LOGGER_PROVIDERS
WARN_LOGGER_PROVIDERS
INFO_LOGGER_PROVIDERS
DEBUG_LOGGER_PROVIDERS
LOG_LOGGER_PROVIDERS
OFF_LOGGER_PROVIDERS