axa-group / nlp.js

An NLP library for building bots, with entity extraction, sentiment analysis, automatic language identify, and so more
MIT License
6.29k stars 621 forks source link

[QUESTION] How to change the NLP dock logger ? #1323

Open dooznvcs opened 1 year ago

dooznvcs commented 1 year ago

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

dooznvcs commented 1 year ago

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();
jesus-seijas-sp commented 1 year ago

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);