1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to configure ngx-logger in standalone components (Angular 18)

Discussão em 'Angular' iniciado por Anand, Setembro 29, 2024.

  1. Anand

    Anand Guest

    I have an Angular 18 application now built on standalone component concepts. I want to use ngx-logger for logging Errors, warnings and Inf to server. I have seen multiple articles of ng-logger in modular patterns and there are no references how to use them in Standalone components. Please guide me how I can do it.

    The code I had written is giving compilation issues when I added LoggerModule.forRoort({....}). For resolving errors I commented the .forRoot({..}) part.

    import { Component } from '@angular/core';
    import { RouterOutlet } from '@angular/router';
    import { LoggerModule, NGXLogger } from 'ngx-logger';

    @Component({
    selector: 'app-root',
    standalone: true,
    imports: [
    RouterOutlet,
    LoggerModule
    // .forRoot({
    // serverLoggingUrl: '/api/logs',
    // level: NgxLoggerLevel.DEBUG,
    // serverLogLevel: NgxLoggerLevel.ERROR
    // })
    ],
    providers: [
    ],
    templateUrl: './app.component.html',
    styleUrl: './app.component.css'
    })
    export class AppComponent {
    title = 'Hello';

    constructor(
    private logger: NGXLogger) {
    this.logger.debug("Debug message");
    this.logger.info("Info message");
    this.logger.log("Default log message");
    this.logger.warn("Warning message");
    this.logger.error("Error message");
    }
    }


    Output:

    R3InjectorError(Standalone[_AppComponent])[NGXLogger -> NGXLogger -> TOKEN_LOGGER_CONFIG -> TOKEN_LOGGER_CONFIG]:
    NullInjectorError: No provider for TOKEN_LOGGER_CONFIG!
    at NullInjector.get (eval at instantiateModule (file:///aaaaa/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:52905:24), <anonymous>:13862:21)
    at R3Injector.get (eval at instantiateModule (file://aaaaa/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:52905:24), <anonymous>:14755:27)
    at R3Injector.get (eval at instantiateModule (file://aaaaa/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:52905:24), <anonymous>:14755:27)
    at injectInjectorOnly (eval at instantiateModule (file://aaaaa/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:52905:24), <anonymous>:13595:36)
    at Module.ɵɵinject (eval at instantiateModule (file://aaaaa/node_modules/vite/dist/node/chunks/dep-DyBnyoVI.js:52905:24), <anonymous>:13601:59)
    at Object.NGXLogger_Factory [as factory] (eval at instantiateModule

    Continue reading...

Compartilhe esta Página