Skip to content

Custom JSON logger and duplicate log messages #989

Closed
@wangjinbei

Description

@wangjinbei

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    📚 documentationUpdate to non-code (readme, docstrings, typos, …)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions