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

le-ngx-logger

v1.0.2

Published

In Angular projects, automatically send logs to the specificized Back-End endpoint. Logger can be configured to log any combination of HTTP Errors, JavaScript Errors, as well as calls to selected commands (such as `console.error()`).

Downloads

4

Readme

LeNgxLogger

In Angular projects, automatically send logs to the specificized Back-End endpoint. Logger can be configured to log any combination of HTTP Errors, JavaScript Errors, as well as calls to selected commands (such as console.error()).

Each of these options can be separately enabled for development and production environments. Similarly, Debug mode can be enabled separately for each environment. (see Format of the Configuration Object for details).

Contents

GitHub Home

See the source code on GitHub

Installation

npm install le-ngx-logger

Usage

Importing Into The Application

LeNgxLoggerModule should be imported into the main app.module.ts with the configuration object.

See Format of the Configuration Object for details how to build the Configuration Object.

Once created, the object can be supplied into the Logger using one of the two options:

Format of the Configuration Object

The Configuration Object will override default values baked into the Logger. All properties are optional. However, at least the endpoint should be provided so that the logs are correctly sent to the endpoint that records the logs.

import { LeNgxLoggerConfig } from 'le-ngx-logger';

const leNgxLoggerConfig: LeNgxLoggerConfig = {
    endpoint: 'https://www.report-errors.xe/le-error',
    report: {
        monkeypatch: [
            {
                parent: console,
                methods: {
                    log: { on: true, name: 'console.log' },
                    error: { on: true, name: 'console.error' },
                    warn: true,
                    info: { on: true, name: 'console.info' },
                    assert: true,
                    table: { on: true, name: 'console.table' },
                }
            },
            {
                parent: window,
                methods: {
                    alert: { on: true, name: 'alert' },
                }
            }
        ]
    },
    delay: 1000, // Milliseconds - Delay report after error

    // HTTP Headers will be added to the API call carrying an error report
    httpHeaders: {
        'Logger-Report': 'Le-Ngx-Logger'
    },

    reportingFailureMessage: 'Could not send log to server',

    debug: {
        onDev: true,
        onProd: false,
    }
}

Supplying The Configuration Object

See Configuration Object Format for information on creating the Configuration Object.

Once Configuration Object is created, it can be supplied into LeNgxLoggerModule by either of the following two methods:

  1. By using the .forRoot() pattern
  2. By providing LE_NGX_LOGGER_CONFIG

.forRoot() Option

Here is an example of importing LeNgxLoggerModule directly into AppModule using .forRoot() pattern:

import { LeNgxLoggerModule } from 'le-ngx-logger';

// Create or import `leNgxLoggerConfig`
const leNgxLoggerConfig: LeNgxLoggerConfig = { /* ... */ }

@NgModule({
  imports: [
    LeNgxLoggerModule.forRoot(leNgxLoggerConfig),
  ]
})
export class AppModule { }

Alternatively, the details of configuring LeNgxLoggerModule can be sequestered into a separate ngModule, e.g. app-logger.module.ts.

Excerpt from a sample file app-logger.module.ts:

import { NgModule } from "@angular/core";
import { LeNgxLoggerConfig, LeNgxLoggerModule } from "le-ngx-logger";

const leNgxLoggerConfig: LeNgxLoggerConfig = { /* ... */ }

@NgModule({
    imports: [LeNgxLoggerModule.forRoot(leNgxLoggerConfig)],
    exports: [LeNgxLoggerModule],
})
export class AppLoggerModule { }

This module can now be imported into AppModule:

Excerpt from the sample file app.module.ts:

import { AppLoggerModule } from './app-logger.module'

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

Providing LE_NGX_LOGGER_CONFIG

Contents of sample file app.module.ts:

import { LeNgxLoggerModule, LE_NGX_LOGGER_CONFIG } from 'le-ngx-logger';

// Create or import `leNgxLoggerConfig`
const leNgxLoggerConfig: LeNgxLoggerConfig = { /* ... */ }

@NgModule({
  imports: [
    LeNgxLoggerModule,
  ],
  providers: [
    {provide: LE_NGX_LOGGER_CONFIG, useValue: leNgxLoggerConfig}
  ]
})
export class AppModule { }

Logging Data Explicitly

The logger can be configured to function automatically, without the need for any explicit intervention. But, if required, it can be invoked explicitly.

To achieve that, import the LeNgxLoggerService and call its .log(event) method.

The method takes a single parameter of type Partial<leNgxLoggerLoggableEvent>:

interface leNgxLoggerLoggableEvent {
    command: null | string;
    parameters: null | any[];
    result: any;
    error: null | leNgxLoggerLoggableError;
    timeZone: string;
    navigator: {
        userAgent: string;
        language: string;
    };
    data?: any;
}

To log arbitrary data, use the optional member data.

Here is an example.

Excerpt from a sample file app.component.ts:

import { Component } from '@angular/core';
import { LeNgxLoggerService } from 'le-ngx-logger';

@Component({
  selector: 'test-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  constructor(
    private loggerService: LeNgxLoggerService,
  ) {}

  logQuick(data: string) {
    this.loggerService.log({data})
  }

  logFull(event: leNgxLoggerLoggableEvent) {
    this.loggerService.log(event)
  }

}