0

I've written a remote logger for angular2 which works fine. I'd now like to publish it as its own npm library and struggle with making it available. I've oriented myself at the setup of the ngx-translate module.

The general setup of the logger I am using internally is similar to the one described in Ben Nadel's Custom Error Handler Post:

import {
  LOG4NG_SERVICE_HANDLER_PROVIDERS,
  LOG4NG_SERVICE_CONFIG,
  LOG4NG_ERROR_HANDLER_PROVIDERS,
  LOG4NG_ERROR_HANDLER_CONFIG } from './log4ng.service';
import * as message from './log4ng.service/message';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [
    LOG4NG_SERVICE_HANDLER_PROVIDERS,
    {
      provide: LOG4NG_SERVICE_CONFIG,
      useValue: {
        recipients: [
          {
            classname: 'ConsoleWriter',
            params: {
              name: 'Angular2 Test App::Console Logger'
            }
          }
        ],
        filters: [ /** filter definition **/ ]
      }
    },

    LOG4NG_ERROR_HANDLER_PROVIDERS,
    {
      provide: LOG4NG_ERROR_HANDLER_CONFIG,
      useValue: {
        rethrowError: false,
        unwrapError: false
      }
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

I've tried to adapt this approach to it's own module like so:

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
  Log4ngService,
  ILog4ngServiceConfig,
  LOG4NG_SERVICE_CONFIG,
  LOG4NG_SERVICE_HANDLER_PROVIDERS,
  ErrorHandlerLoggingService,
  LOG4NG_ERROR_HANDLER_CONFIG,
  LOG4NG_ERROR_HANDLER_PROVIDERS
} from './src';

export * from './src';

@NgModule({
  imports: [
    CommonModule
  ],
  providers: [
    Log4ngService,
    ErrorHandlerLoggingService
  ]
})
export class Log4ngModule {
    static forRoot(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
        return {
            ngModule: Log4ngModule,
            providers: LOG4NG_SERVICE_HANDLER_PROVIDERS
        };
    }

    static forChild(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
        return {
            ngModule: Log4ngModule,
            providers: LOG4NG_SERVICE_HANDLER_PROVIDERS
        };
    }
}

I proceeded to create a demo-app to use this module and used npm link to locally use the libary. While it builds completely, when trying to use it in the demo app, I get the runtime error:

Error: Can't resolve all parameters for Log4ngService: (?)

which I do not understand, since for my understanding, all necessary DI tokens are correctly set.

You can find the code in the dev-branch of this git-repository: https://github.com/LarsKumbier/log4ng/tree/dev

Any help and hints appreciated - thanks in advance!

Lars
  • 5,757
  • 4
  • 25
  • 55

1 Answers1

0

since the problem is in Log4ngService you can use a Factory like this

  /** Log4ngService factory */
    export function Log4ngServiceFactory(config: ILog4ngServiceConfig ) {
        return new Log4ngService (config);
    }  

    @NgModule({
      imports: [
        CommonModule
      ],
      providers: [
        ErrorHandlerLoggingService
      ]
    })
    export class Log4ngModule {
         static forRoot(config: ILog4ngServiceConfig = LOG4NG_SERVICE_CONFIG): ModuleWithProviders {
                return {
                    ngModule: Log4ngModule,
                    providers: [
                    { 
                       provide: Log4ngService,
                       useFactory: Log4ngServiceFactory,
                       deps : [config]
                    } 
                   ]
                };
            }
  }
El houcine bougarfaoui
  • 35,805
  • 9
  • 37
  • 37