axa-group/nlp.js

[QUESTION] How to change the NLP dock logger ?

dooznvcs opened this issue · 2 comments

I would like to change the dock logger so I can use my own
I use the example given here to register my own logger

import { defaultContainer, dockStart } from '@nlpjs/basic';
import { MyCustomLoggerClass } from './some-file';

defaultContainer.register('logger', new MyCustomLoggerClass());

const dock = await dockStart(this.options.dock);
const manager = dock.get('nlp');

manager.addCorpus(corpusFilePath);

await manager.train();
export class MyCustomLoggerClass {
    public constructor() {}

    public trace(...messages: string[]): void {
        console.trace('[TRACE]', ...messages);
    }

    public debug(...messages: string[]): void {
        console.debug('[DEBUG]', ...messages);
    }

    public info(...messages: string[]): void {
        console.info('[INFO]', ...messages);
    }

    public log(...messages: string[]): void {
        console.log('[LOG]', ...messages);
    }

    public warn(...messages: string[]): void {
        console.warn('[WARN]', ...messages);
    }

    public error(...messages: string[]): void {
        console.error('[ERROR]', ...messages);
    }

    public fatal(...messages: string[]): void {
        console.error('[FATAL]', ...messages);
    }
}

But the logs still remain unchanged

image

I reopen this issue because I still don’t have the solution... Even using the Adding your own logger to the container example, the instance of my logger is still not active. Even copying the example, the custom logger didn’t work.

I changed my code this way:

import { dockStart } from '@nlpjs/basic';
import { MyCustomLoggerClass } from './some-file';

const dock = await dockStart(this.options.dock);

const container = dock.getContainer();
const manager = dock.get('nlp');

container.register('logger', new MyCustomLoggerClass());
manager.addCorpus(corpusFilePath);

await manager.train();

Hello,
You're correctly changing the logger.
The problem is that the neural trainer does not use the logger from the container.
When the neural trainer is created it accepts a setting "log" that can be true to use the default console, false to don't log or a function to use this function to log.
https://github.com/axa-group/nlp.js/blob/master/packages/neural/src/neural-network.js#L40

You can change the function that logs the neural trainer:

const { dockStart } = require('@nlpjs/basic');

(async () => {
  const config = {
    settings: {
      nlp: {
        nlu: {
          log: (status, time) => console.log(`custom log: ${status.iterations} ${status.error} ${time}ms`),
        },
        corpora: [
          "./corpus-en.json"
        ]
      }
    },
    use: ["Basic"]
  }

  const dock = await dockStart(config);