Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to turn off or on debug logging dynamically - ¿possible bug? #1954

Closed
vituchon opened this issue May 5, 2023 · 14 comments
Closed

How to turn off or on debug logging dynamically - ¿possible bug? #1954

vituchon opened this issue May 5, 2023 · 14 comments

Comments

@vituchon
Copy link

vituchon commented May 5, 2023

Hi Folks! Hope you are doing fine.

Sorry to bother....

I'm having this situation. I don't know what door to knock, so I posted in stackoverflow and here.

Thanks in advance!

@adrai
Copy link
Member

adrai commented May 5, 2023

You can inject your custom logger plugin: https://www.i18next.com/misc/creating-own-plugins#logger

example: https://codesandbox.io/s/react-i18next-http-example-forked-kn1yuz?file=/src/i18n.js:188-363

@vituchon
Copy link
Author

vituchon commented May 5, 2023

@adrai thanks for the insight! It is working.... I have make a quick test...

var log = false;
const myLogger = {
  type: 'logger' as ModuleType,

  log: (args:any) => {
    if (log) {
      console.log(args)
    }
  },
  warn: (args:any) => console.warn(args),
  error: (args:any) => console.error(args)
}
window.i18next.use(myLogger).use().init(i18nextOptions, () => {
    log = true;
  }
})

and when var log is set to true then I see the logger log level lines in the console (and not before!) GREAT!. The motivation are to hide log lines that ng-i18next angularjs filter produces due to not having the i18next loaded yet.

If you can provime me context about what I was doing with
i18next.logger.setDebug(true)
vs
i18next.translator.logger.setDebug(true)

I will appreciate it very much as well!.

TD;DR;

The problem with ng-i18next, the commented lines were a proposal to fix them...

image

@adrai
Copy link
Member

adrai commented May 5, 2023

There's just 1 instance of the logger so I would use i18next.logger.setDebug(false or true)

@vituchon
Copy link
Author

vituchon commented May 5, 2023

I'm afraid i18next.logger.setDebug(false or true) doesn't makes any visible effect on the log IF the log starts with debug false then in keep "muted" besides doing i18next.logger.setDebug(true) ¿That this have any sense?

My version of i18next.js is 21.6.16

@adrai
Copy link
Member

adrai commented May 5, 2023

currently afk, will check later

@vituchon vituchon changed the title How to turn off or on debug logging dynamically How to turn off or on debug logging dynamically - ¿possible bug? May 5, 2023
@adrai
Copy link
Member

adrai commented May 5, 2023

Sorry, I was wrong... there is not a single instance of the logger... each sub-module creates its own logger instance...
So currently I suggest you create your own logger plugin and do not use the setDebug function at all.
An alternative way to workaround that and make use of the setDebug function would be to change the logger like that: #1957
But I don't know if this in-line with the original design of @jamuhl

@jamuhl
Copy link
Member

jamuhl commented May 6, 2023

yes there are "sub-loggers" but as far as I know only for the translator module...

I never saw a use case beside having the logger turned on/off once using options on init -> debug is only set to true in development env anyway....so who cares about getting those warnings as they are not logged during production

enabling both loggers (main instance and the one on translator should be sufficient). I still wouldn't change the setDebug on an instance to toggle behaviour on all. If that's needed a copy of the base logger should do.

@adrai
Copy link
Member

adrai commented May 6, 2023

only for the translator module

fyi: there are 6 sub-loggers
image

@jamuhl
Copy link
Member

jamuhl commented May 6, 2023

still...i'm against bloating more unneeded kb into the module...just because one case of wanting selective warnings.

the case in ng-i18next is solved better like:
236502935-b7e143a5-c3f4-467c-b2c0-67612f2853be

-> the issue is there in ng-i18next...i18next.t gets used before init -> so the warning is correct.

It's not something this module has to solve...

@jamuhl
Copy link
Member

jamuhl commented May 6, 2023

beside that - what comes next. Turn off warnings if a namespace was not loaded as the implementation for the UI framework has no useTranslation like react-i18next?

@adrai
Copy link
Member

adrai commented May 6, 2023

since the setDebug function is not used at all we maybe should remove it completely

@jamuhl
Copy link
Member

jamuhl commented May 6, 2023

would be breaking change...as it could be used in userland (just not used internal)...in the end...all can be done...it's just not one global toggle...

// turn all on
i18next.logger.setDebug(true);
i18next.translator.logger.setDebug(true);
Object.values(i18next.services).forEach(s => s.logger && s.logger.setDebug(true));

@adrai
Copy link
Member

adrai commented May 6, 2023

@vituchon we'll probably remove the setDebug function completely in the next major version... so like @jamuhl already suggested. the warning log is correct and ng-i18next should address this. And alternatively you can inject your own logger.

@vituchon
Copy link
Author

vituchon commented May 8, 2023

@adrai @jamuhl thanks for adressing this issue.

It is not something crucial, but desirable to have it working. It happens on specific scenario due to some code of ng-i18next that try to translate prior of having i18next initialized. And I agree that the problem shall be fixed at ng-i18next level, and not here. But the ng-i18next project looks quite abandoned.... I mean, I have filled a bug some months ago and there was no reply.

Leaving that aside and if you may allow a personal opinion => I think that the setDebug(true or false) shall be working as expected. Right now is a no op, so, you may document it well or you may fix it. There can be other cases in the future where devs would like to turn on or off this feature.

Using a custom logger works like a charm, so that was a good workaround for this.

Thanks for taking into consideration this affair, and keep up the good work!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants