xtekky / gpt4free

The official gpt4free repository | various collection of powerful language models
https://g4f.ai
GNU General Public License v3.0
62.35k stars 13.4k forks source link

Improve logging implementation #2347

Closed foxfire52 closed 2 weeks ago

foxfire52 commented 2 weeks ago

Current logging implementation needs improvement.

Issues

  1. Code should avoid usage of basicConfig() to set up logging since subsequent calls by library users might fail silently. The function basicConfig() sets up the root Logger with a StreamHandler. Since basicConfig() function "does nothing if the root logger already has handlers configured", after g4f code calls it library users will always experience silent failure when calling it again (issue #2274). Note that configuration of the root logger should be avoided by libraries (see Configuring Logging for a Library - Python Docs).

  2. Logging functions are called from root logger. This is not a recommended practice and can interfere with custom logging setups (log duplication issue #2173) and third-party libraries.

  3. There is leftover unused code in g4f/api/_logging.py, which sets up handlers with basicConfig(). If called it might fail silently, since there's another basicConfig() call in g4f/Provider/you/har_file.py#L14 (see issue 1). Module Loguru is only imported here, so it's unused as well.

Proposed solutions

  1. Replace call to basicConfig() with setLevel()
  2. Create a named Logger for the library.
  3. Remove or comment out unused code and remove Loguru dependency.

Info: Current code sets logging error level to ERROR.

hlohaus commented 2 weeks ago

Thank you