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

Custom JSON logger and duplicate log messages #989

Open
wangjinbei opened this issue Jun 18, 2024 · 1 comment
Open

Custom JSON logger and duplicate log messages #989

wangjinbei opened this issue Jun 18, 2024 · 1 comment
Labels
❔ question Further information is requested

Comments

@wangjinbei
Copy link

wangjinbei commented Jun 18, 2024

Hi, when I use https://kdeldycke.github.io/click-extra/logging.html#custom-logger

My terminal outputs two logs, is there a problem where I am processing?
My intention: to format all log content in json format so that I can analyze and notify using the log collector .

Also what can I do to define the Load configuration matching foo.ini in json format as well?

demo.py:

import logging

from click import pass_context
from click_extra import extra_command, extra_basic_config, verbosity_option

extra_basic_config(
    logger_name="app_logger",
    format='{{"time": "{asctime}", "name": "{name}", "level": "{levelname}", "msg": "{message}"}}',
    force=True,
    level=logging.INFO,
)


@extra_command
@pass_context
@verbosity_option(default_logger="app_logger",)
def cli(config):
    logging.getLogger("app_logger").info("This is an info message from the root logger.")

output:

Load configuration matching foo.ini
{"time": "2024-06-18 10:35:50,018", "name": "app_logger", "level": "info", "msg": "This is an info message from the root logger."}
info: This is an info message from the root logger.
@kdeldycke
Copy link
Owner

I can reproduce your case with the latest Click Extra (v4.11.7), so nothing's wrong on your side. It seems that the duplicate log has always been there since I introduced unittests with the documentation in v4.3.0: ec4243c#diff-1a5e57d66b08bec5a71860d67f06912a004f03368c36910099d4ffae12970c7dR230-R231

And as you can see it is how that's being documented at https://kdeldycke.github.io/click-extra/logging.html#custom-logger

As for the Load configuration matching message, it seems there is a chicken and egg problem involved, as per the comments in the source code:

if explicit_conf:
# We have can't simply use logger.info() here as the defaults have not been
# loaded yet and the logger is stuck to its default WARNING level.
echo(message, err=True)

Still, your use-case of producing JSON for all logs is interesting. Let me dive into that. Maybe it's just a matter of setting up the right configuration.

@kdeldycke kdeldycke changed the title Log output does not match what is expected Custom JSON logger and duplicate log messages Dec 8, 2024
@kdeldycke kdeldycke added the ❔ question Further information is requested label Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❔ question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants