Giter Club home page Giter Club logo

angular2-logger's Introduction

Angular2-Logger

Build Status npm version Join the chat at https://gitter.im/code-chunks/angular2-logger MIT licensed Support

What is it?

A simpler Log4j inspired logger module for Angular 2. Think of it as "Log4ng" ... get it?

This is a work in progress and is not ready for production, use with care, the API can and will change.

Usage

Quickstart

  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:

     <!-- IE required polyfills, in this exact order -->
     ...
     <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
     <!-- Add the following line to the list of scripts: -->
     <script src="node_modules/angular2-logger/bundles/angular2-logger.js"></script>
     <!-- angular2-logger/bundles/angular2-logger.min.js` is also available for use in production. -->
    
  3. Setup the Provider.

     import {Logger} from "angular2-logger/core";
    
     bootstrap( App, [ Logger ]);
    
  4. Inject the logger into your objects and use it.

     @Component({
     	...
     })
     export class App(){
     	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...');
     	}
     }
    

By default the logger level will be set to Level.WARN, so you'll only see Warning and Error messages.

Going deeper...

In order to see all of the messages you just need to change the logger message hierarchy level, you can do so:

  • Dynamically using the console:

      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";
     
      bootstrap( App, [ LOG_LOGGER_PROVIDERS ]);
    

    The available Providers are:

      ERROR_LOGGER_PROVIDERS
      WARN_LOGGER_PROVIDERS
      INFO_LOGGER_PROVIDERS
      DEBUG_LOGGER_PROVIDERS
      LOG_LOGGER_PROVIDERS
      OFF_LOGGER_PROVIDERS
    

Note: If you change the level of the Logger dynamically, that setting will be lost upon refreshing the page and set back to its default configured setting. If you want the logger to keep this setting changed, store it in the localStorage by doing:

logger.store() // and logger.unstore() to undo.

Custom Configuration

If the Providers included don't meet your needs you can configure the default logger configuration by Providing custom properties:

import { Logger, Options, Level } from "angular2-logger/core";

bootstrap(AppComponent,[
    //<Options> casting is optional, it'll help you with type checking if using an IDE.
    provide( Options, { useValue: <Options>{ store: false } } ),
    Logger
]);

As you can see you don't have to specify all of them, just the ones you want to override.

The available configuration options are:

  • level:Level - How much detail you want to see in the logs; Level.ERROR (1) being the less detailed and Level.LOG (5) being the most. Defaults to Level.WARN (2).

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

    0.- Level.OFF

    1.- Level.ERROR

    2.- Level.WARN

    3.- Level.INFO

    4.- Level.DEBUG

    5.- Level.LOG

    The Logger will log everything with higher or equal priority to the current logger.level set.

  • global:boolean - Whether or not you want the created logger object to be exposed in the global scope. Defaults to true.

  • globalAs:string - The window's property name that will hold the logger object created. Defaults to 'logger'.

  • store:boolean - 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:string - The local storage key that will be used to save the level config if the store setting is true. Defaults to 'angular2.logger.level'.

You can also override the default configuration options by extending the Options and injecting yours instead:

// from custom-logger-options.ts
...
@Injectable() export class CustomLoggerOptions(){
    store: true
}
...

// from boot.ts/main.ts
...
bootstrap(AppComponent,[
    provide( Options,{ useClass: CustomLoggerOptions } ),
    Logger
]);

Class names like Options and Level might be too common, if you get a conflict you can rename them like this:

import { Logger, Options as LoggerOptions, Level as LoggerLevel } from "angular2-logger/core";

bootstrap(AppComponent,[
    provide( LoggerOptions,{ useValue: {
        level: LoggerLevel.WARN,
        ...

How you can help

Filing issues is helpful but pull requests are even better!

Instructions for dev environment

They are too long so try to keep up, here we go:

git clone https://github.com/code-chunks/angular2-logger.git

cd angular2-logger

npm i

Done.

TODOs

  • Add a Level.OFF that turns off the logger.
  • Support different loaders and modes.
  • Add a basic demo.
  • Minify bundle.
  • Ability to add logging time to the messages.
  • Lazy Logging.
  • Appenders.
  • Support named loggers.
  • Message Layout Feature.
  • No coding required Dashboard UI to handle loggers.

Breaking changes on 0.3.0

The codebase was updated to handle the breaking changes on Angular2's Release Candidate.

Make sure you don't upgrade to this version if you haven't upgraded Angular2 to at least 2.0.0-rc.0

Breaking changes on 0.2.0

The setup changed a bit to make it easier so make sure you follow up the new Quickstart.

LICENSE

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.