npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular2-log4ts

v0.6.0

Published

A Log4j inspired Logger for Angular 2.

Downloads

30

Readme

angular2-log4ts

What is it?

A simpler Log4j inspired logger module for Angular 2. Think of it as 'Log4ng'

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-log4ts

  2. Add the angular2-log4ts 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-log4ts':            'node_modules/angular2-log4ts' // 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-log4ts':            { 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-log4ts/src/app/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...');
     	}
     }

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-log4ts/src/app/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

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 } from 'angular2-log4ts/src/app/core';

@NgModule({
    ...
    providers:    [ 
        { provide: Options, useValue: { store: false } },
        Logger
    ]
})
export class AppModule { }

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 app.module.ts
...
@NgModule({
    ...
    providers:    [ 
        { 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-log4ts/src/app/core';

@NgModule({
    ...
    providers:    [ 
        { 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/digimax/angular2-log4ts.git

cd angular2-log4ts

npm i

Done.

TODOs

  • [x] Add a Level.OFF that turns off the logger.

  • [x] Support different loaders and modes.

  • [x] Add a basic demo.

  • [x] 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.

  • [ ] Automatize definition files. Waiting for https://github.com/Microsoft/TypeScript/issues/4433 .

  • Quickstart guide now follows the pattern in Angular 2's Quickstart to add the references to other libs in systemjs.config.js. However if you still want to do it the old way by adding the system bundle, you can still do so, except now its called bundles/angular2-logger.sys.min.js.

LICENSE

MIT