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

@nsalaun/ng-logger

v12.0.0

Published

Angular logger service

Downloads

661

Readme

ng-logger

Build Status npm version devDependencies Status peerDependencies Status license

ng-logger is a simple Angular logger service that responds to two needs :

  • A log level system to be able to disable certain calls as needed. We do not want to see our debug trace on production.
  • A logger that keeps trace of the original log call. We do not want all our logs to be referenced in some logger.service.js all the time.

This package is compatible with Angular 12 and above, Angular AoT compiler and Angular CLI.

Installation

1 - Install the npm package.

npm install --save @nsalaun/ng-logger

2 - Import NgLoggerModule in your application and use forRoot(level: Level) to choose your log level :

import { NgModule }              from '@angular/core';
import { BrowserModule }         from '@angular/platform-browser';
import { AppComponent }          from './app.component';
import { NgLoggerModule, Level } from '@nsalaun/ng-logger';
     
@NgModule({
    imports:      [ BrowserModule, NgLoggerModule.forRoot(Level.LOG) ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ],
})
export class AppModule { } 

3 - Loading

Using SystemJS configuration

System.config({
    map: {
        '@nsalaun/ng-logger': 'node_modules/@nsalaun/ng-logger/bundles/nsalaun-ng-logger.umd.js'
    }
});

Angular-CLI

No need to set up anything, just import it in your code.

Rollup or webpack

No need to set up anything, just import it in your code.

Plain JavaScript

Include the umd bundle in your index.html:

<script src="node_modules/@nsalaun/ng-logger/bundles/nsalaun-ng-logger.umd.js"></script>

and use global nsalaun.ngLogger namespace.

AoT compilation

The library is compatible with AoT compilation. Because of the new metadata version with Angular 5, the library is not compatible with previous Angular version.

Usage

Basic Usage

Inject the Logger service anywhere you need it and use it like it's console :

@Component({})
export class MyComponent(){

    constructor(private logger: Logger){
    
        this.logger.log('Hello !', "It's working :)");
        
    }
}

The service offer a sub-list of window.console capacities :

  • Basics :
    • log(...args: any[]) - Outputs a message to the Web Console.
    • debug(...args: any[]) - Outputs a debugging message to the Web Console.
    • info(...args: any[]) - Outputs an informational message to the Web Console.
    • warn(...args: any[]) - Outputs a warning message to the Web Console.
    • error(...args: any[]) - Outputs an error message to the Web Console.
  • Groups :
    • group(groupTitle: string) - Creates a new inline group in the Web Console log.
    • groupCollapsed(groupTitle: string) - Creates a new inline group in the Web Console log that is initially collapsed.
    • groupEnd() - Exits the current inline group in the Web Console.
  • Time :
    • time(timerName: string) - Starts a timer you can use to track how long an operation takes. It works only with log Level equal or higher than DEBUG.
    • timeEnd(timerName: string) - Stops a timer that was previously started by calling Logger.time(). It works only with log Level equal or higher than DEBUG.

Using different log level on developpment or production

To set a different log level depending on environment, you can proceed as follows:

import { NgModule, isDevMode }   from '@angular/core';
import { BrowserModule }         from '@angular/platform-browser';
import { NgLoggerModule, Level } from '@nsalaun/ng-logger';
import { AppComponent }          from './app.component';

// Set different log level depending on environment.
const LOG_LEVEL = Level.LOG;
if (!isDevMode()) {
    const LOG_LEVEL = Level.ERROR;
}
 
@NgModule({
    imports     : [ BrowserModule, NgLoggerModule.forRoot(LOG_LEVEL) ],
    declarations: [ AppComponent ],
    bootstrap   : [ AppComponent ],
})
export class AppModule { } 

Please note this method is one among others. It may not suit your projects requirements/constraints

License

© 2017-2021 Noémi Salaün

MIT