Angular 2+ Logger Using ng2.logger

In this article, I am going to show you the process to log messages in Angular 2+ applications with examples. Ng2 Logger is a simple logging module for angular 2+ applications. This allows you to log messages based on the log level setup.

To use Ng2 Logger module, first, we have to set up and configure in our Angular 2+ application. Follow the below steps to setup and configure Ng2 Logger module.

Step #1 Ng2 Logger Installation

Execute the below command in the npm console window.

npm install ng2.logger

Once you execute the above command, you can see the Ng2 Logger package in package.json file.

package.json

  1. {  
  2.   "name""office-front-end2",  
  3.   "version""0.0.0",  
  4.   "license""MIT",  
  5.   "scripts": {  
  6.     --------------  
  7.     --------------  
  8.     --------------  
  9.   },  
  10.   "private"true,  
  11.   "dependencies": {  
  12.     --------------  
  13.     --------------  
  14.     --------------  
  15.     "angular2-logger""^0.6.0",  
  16.     --------------  
  17.     --------------  
  18.     --------------  
  19.   },  
  20.   "devDependencies": {  
  21.     ----------  
  22.     ----------  
  23.     ----------  
  24.   }  

Step #2 Configuration Options

The available Ng2 Logger configuration options are shown below.

Level - This tells you, how much detail you want to see in the console logs.

  • ERROR (1) being the less detailed
  • LOG (5) being the most.
  • Default optioins is Level.WARN (2).

The Hierarchy of the messages is as follows from highest to lowest priority,

  • - Level.OFF
  • - Level.ERROR
  • - Level.WARN
  • - Level.INFO
  • - Level.DEBUG
  • - Level.LOG

The Logger will log everything with higher or equal priority to the current logger.level set. The app will only log message for that level or higher (OFF disables the logger).

Create one environment file and set it to default logger level based on your need so, that it will be applied throughout the application, which should work like a global configuration file.

Step #3 Configure the default option in the environment file

environment.local.ts

  1. /* File content for the current environment will overwrite these during build. The build system defaults to the dev environment which uses 'environment.local.ts' file, but if you do 'ng build  
  2. --env=prod' then 'environment.prod.ts' will be used instead. The list of which environment maps to which file can be found in '.angular-cli.json' file. */  
  3.   
  4. /* 
  5. Logger Levels 
  6. 0-Level.OFF, 1-Level.ERROR, 2-Level.WARN, 3-Level.INFO, 4-Level.DEBUG, 5-Level.LOG 
  7. */  
  8.   
  9. import {Level} from 'angular2-logger/core';  
  10.   
  11. export const environment = {  
  12.     production: false,  
  13.     sessionStorageKey: 'test',  
  14.     sessionTokenName: 'token',  
  15.     apiEndpoint: 'http://localhost:12345/',  
  16.     apiPrefix: 'api/',  
  17.     basePath: '/test/',  
  18.     logger: {  
  19.         level: Level.LOG  
  20.     }  
  21. }; 

Step #4 Configure the Ng2 Logger provider in app.module.ts file

Once the environment file gets created, the next job is to register the Ng2 Logger provider in the app.module.ts file. Here is the sample code.

app.module.ts

  1. ---------------  
  2. ---------------  
  3. ---------------  
  4. import {environment} from "../environments/environment";  
  5. import {Logger, Options} from "angular2-logger/core";  
  6.   
  7.   
  8. @NgModule({  
  9.     declarations: [  
  10.         AppComponent  
  11.     ],  
  12.     imports: [  
  13.         ---------------  
  14.         ---------------  
  15.         ---------------  
  16.     ],  
  17.   
  18.     providers: [{ provide: Options, useValue:{ level:environment.logger.level,store: false }      }, Logger],  
  19.     bootstrap: [AppComponent]  
  20. })  
  21. export class AppModule {}  

Step #5 Create component file

Create component file by importing Ng2 Logger and use the logger. Once you log the message, you can see in the console window.

logger-example.component.ts

  1. import { Component, OnInit } from '@angular/core';  
  2. import {Logger} from "angular2-logger/core";  
  3. @Component({  
  4.   selector: 'sis-logger-example',  
  5.   templateUrl: './logger-example.component.html',  
  6. })  
  7. export class LoggerExampleComponent implements OnInit {  
  8.     constructor( private _logger: Logger ){  
  9.         this._logger.unstore();  
  10.         this._logger.error("Priority level #1 error msg..");  
  11.         this._logger.warn("Priority level #2 warning msg..");  
  12.         this._logger.info("Priority level #3 warning msg..");  
  13.         this._logger.debug("Priority level #4 debug msg..");  
  14.         this._logger.log("Priority level #5 log msg.");  
  15.     }  
  16.   ngOnInit() {  
  17.   }  

logger-example.component.html

  1. <header>  
  2.         <h1>Logger Examples and Usage</h1>  
  3. </header>  
  4. <section id="logger">  
  5. </section> 

Output

Default Ng2 Level setting

 Output

Level with Debug off

 Output

Level with Debug and Info off

 Output

Level with Debug and Info and Warn off

 Output

Level with Verbose (Level 4) off

 Output

Highest Level that is Level 5 set

 Output

Level completely off

 Output

I appreciate your valuable feedback. Thank you.


Similar Articles