Socket
Book a DemoInstallSign in
Socket

angular2-logger

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular2-logger

A Log4j inspired Logger for Angular 2.

0.1.2
Source
npmnpm
Version published
Weekly downloads
236
19.8%
Maintainers
1
Weekly downloads
 
Created
Source

Angular2-Logger

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 quite yet ready for production, use with care.

Usage

Quicktart

Install the npm module.

npm install --save angular2-logger

Setup the Provider.

import {Logger} from "angular2-logger/logger";

bootstrap( App, [ Logger ]); 

Add angular2-logger to your Systemjs configuration. If you are following the Angular 2's Quickstart Guide it should be something like this:

System.config({
    map: { 'angular2-logger': 'node_modules/angular2-logger' },
    packages: {
        app: {
            format: 'register',
            defaultExtension: 'js'
        },
        'angular2-logger': {
            format: 'register',
            defaultExtension: 'js'
        }
    }
});

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 {Logger} from "angular2-logger/logger";
     
      bootstrap( App, [ LOG_LOGGER_PROVIDERS ]);
    

    The available Providers are:

      ERROR_LOGGER_PROVIDERS
      WARN_LOGGER_PROVIDERS
      INFO_LOGGER_PROVIDERS
      DEBUG_LOGGER_PROVIDERS
      LOG_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/logger";

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:

    • 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.

  • 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
]);

Note: 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/logger";

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

TODO

  • Support different loaders and modes.

LICENSE

MIT

Keywords

angular2

FAQs

Package last updated on 08 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.