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

Feature: add an option for a custom formatter in logging setup #10

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

Yannick-Dayer
Copy link
Collaborator

@Yannick-Dayer Yannick-Dayer commented Sep 25, 2024

Allow users to provide their own formatter instead of just a format string, allowing further customization of the logs.

Fixes #8.


📚 Documentation preview 📚: https://clapper--10.org.readthedocs.build/en/10/

Copy link

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/clapper
  logging.py
Project Total  

This report was generated by python-coverage-comment-action

@Yannick-Dayer
Copy link
Collaborator Author

Usage example with colorlog:

# app.py
import clapper.click
import clapper.logging
import click
import colorlog

main_logger = clapper.logging.setup(
    logger_name="awesome",
    formatter=colorlog.ColoredFormatter(
        fmt="%(log_color)s[%(levelname)s] %(message)s (%(name)s, %(asctime)s)"
    ),
)
main_logger.setLevel("ERROR")


@click.command(
    entry_point_group="cli_group",
    cls=clapper.click.ConfigCommand,
)
@click.option(
    "--my-config",
    "-c",
    "my_config",
    entry_point_group="config_group",
    cls=clapper.click.ResourceOption,
)
@clapper.click.verbosity_option(
    main_logger,
    expose_value=False,
)  # The logger level is set here if -v is given
def awesome_cli(my_config: list[str] | None, **kwargs):
    """My Awesome CLI."""
    main_logger.error("App error sample (all is well).")
    main_logger.warning("App warning sample (all is well).")
    main_logger.info(f"App info sample: {my_config=}")
    main_logger.debug("App debug sample: Now exiting CLI app.")


if __name__ == "__main__":
    awesome_cli()

colored logs

@anjos
Copy link
Member

anjos commented Sep 26, 2024

Beautiful! Looking great, thanks!

Allow the user to provide a custom formatter class to replace the
default `logging.Formatter`. This should allow more flexibility to
customize the logs output.
The `format` argument is kept for backward compatibility.
Test that the user can set a custom formatter in `clapper.logging.setup`
@Yannick-Dayer Yannick-Dayer marked this pull request as ready for review September 26, 2024 19:32
@Yannick-Dayer
Copy link
Collaborator Author

We can include this in the upcoming release, too.

@Yannick-Dayer Yannick-Dayer requested a review from anjos September 26, 2024 19:34
Copy link
Member

@anjos anjos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@anjos anjos merged commit 92c483e into main Sep 26, 2024
10 checks passed
@anjos anjos deleted the feat/log-formatter branch September 26, 2024 19:50
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

Successfully merging this pull request may close these issues.

Adding a formatter option
2 participants