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

ng-logr

v1.0.1

Published

Angular logger that is designed to be simple and configurable. Ships with the following features. - Two log handlers are provided. - ConsoleLogHandlder (enabled by default) - Prints logs to the console - HttpLogHandler - Posts logs to an http endp

Downloads

24

Readme

Ng-logr

Angular logger that is designed to be simple and configurable. Ships with the following features.

  • Two log handlers are provided.
    • ConsoleLogHandlder (enabled by default) - Prints logs to the console
    • HttpLogHandler - Posts logs to an http endpoint. Handles circular references and removes Angular debugging objects.
  • Uncaught exception handling
    • Provides an exception handler by default that extends Angular's built in ErrorHandler but uses NgLog to log the errors.
  • Cascading configuration.
  • Ability to provide custom log handlers or extend existing ones.

Quick Start

Install the library

npm install --save ng-logr

Import NgLogModule into your app's root module.

import {NgModule} from '@angular/core'
import {NgLogModule} from 'ng-logr'

@NgModule({
  imports: [
    NgLogModule,
  ],
})
export class AppModule {
}

Use the NgLog service in your app to log stuff

import {Injectable} from '@angular/core'
import {NgLog} from 'ng-logr'

@Injectable()
export class MyService {

  constructor(nglog: NgLog) { 
    nglog.log('I made a log!')
  }
}

The NgLog interface is simple and provides only 5 logging methods.

interface NgLog {
  debug(message?: any, ...params: any[]): void
  info(message?: any, ...params: any[]): void
  log(message?: any, ...params: any[]): void
  warn(message?: any, ...params: any[]): void
  error(message?: any, ...params: any[]): void
}

Configuration

The default configuration for NgLog is below. The default global log level is debug and the only log handler is the ConsoleLogHandler, which logs to the console.

{
  logLevel: NgLogLevel.debug,
  logHandlers: [new ConsoleLogHandler()]
}

To override the configuration, provide NgLogOptions when declaring your root module.

import {NgModule} from '@angular/core'
import {INgLogOptions, NgLogLevel, NgLogModule, NgLogOptions,} from 'ng-logr'
import {ConsoleLogHandler} from 'ng-logr'

const options: INgLogOptions = {
  logLevel: NgLogLevel.error,
  logHandlers: [new ConsoleLogHandler()]
}

@NgModule({
  imports: [
    NgLogModule,
  ],
  providers: [{provide: NgLogOptions, useValue: options}]
})

export class AppModule {
}

Use Cases

Send logs to the server

Include the HttpLogHandler when defining your log handlers in your configuration options. This log handler posts logs to your sever at the endpoint /log. You can override the log post route by defining the option httpPostRoute as shown in the example below.

import {ConsoleLogHandler, HttpLogHandler} from 'ng-logr'

const httpHandler = new HttpLogHandler({
  httpPostRoute: 'serverLogRoute'
})
const options: INgLogOptions = {
  logHandlers: [new ConsoleLogHandler(), httpHandler]
}

Restricing the log level

The default log level for NgLog is debug. You can override this globally by providing the option logLevel in the NgLogOptions. You can also provide the logLevel option to a handler to override the log level for that particular handler.

import {ConsoleLogHandler, HttpLogHandler} from 'ng-logr'

const httpHandler = new HttpLogHandler({
  logLevel: NgLogLevel.error
})
const options: INgLogOptions = {
  logLevel: NgLogLevel.debug,
  logHandlers: [new ConsoleLogHandler(), httpHandler]
}

Creating a log handler

Just create a class that extends NgLogHandler and include it as a logHandler in your configuration.

class AwesomeHandler extends NgLogHandler {
  async handleLog(logLevel: NgLogLevel, ...logParams: any[]) {
    const logLevelName = NgLogLevel[logLevel]

    console[logLevelName]('AWESOME', ...logParams)
  }
}

const options: INgLogOptions = {
  logHandlers: [new AwesomeHandler()]
}

Extending a log handler

Extend any included log handler

import {ConsoleLogHandler} from 'ng-logr'

class TimestampConsoleLogHandler extends ConsoleLogHandler {
  async handleLog(level: NgLogLevel, ...params: any[]) {
    super.handleLog(level, (new Date()).toLocaleString(), ...params)
  }
}

const options: INgLogOptions = {
  logHandlers: [new TimestampConsoleLogHandler()]
}

Contributing

This library is still in beta. Contributions are more than welcome. Some things to focus on...

  • Bug fixes.
  • Add support for other Angular versions (currently supports 6 - 8)
  • Make the logs fail silently (or log debug statements to the console if not in production mode).
  • Add a way to include contextual information posting logs from HttpLogHandler (i.e. browser version, device, os version, etc.)
  • Additional log handlers that are useful.