-
Notifications
You must be signed in to change notification settings - Fork 24
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
Allow specifying custom formatter and log level in init decorator #8
base: master
Are you sure you want to change the base?
Conversation
Inside your CLI function you can just use |
# -*- coding: utf-8 -*-
import logging
import click
from click.testing import CliRunner
import click_log
import pytest
logging.basicConfig(level=logging.WARNING, format='CUSTOM_FORMATTER | %(levelname)s | %(message)s')
# Now the handler of the root logger has a custom formatter
test_logger = logging.getLogger(__name__)
# The formatter of test_logger is the root logger's formatter, which is the custom formatter
def test_level_with_formatter_inside(runner):
"""
The following test fails, since @click_log.init() overwrites the custom format set by logging.basicConfig.
There is no way to restore the original formatter which is overwritten by ColorFormatter
"""
@click.command()
@click_log.init() # this decorator overwrites the root logger's formatter, replacing it with ColorFormatter
def cli():
# The formatter of test_logger is still the root logger's formatter, but now it is ColorFormatter.
# The custom formatter defined above is now gone, and cannot be retrieved.
# A workaround is to create a new formatter here with the same properties, but sometimes the basicConfig
# is set by the application logic and we cannot reproduce it easily
test_logger.debug('hey')
test_logger.info('yo')
test_logger.warning('oh')
test_logger.error('damn')
result = runner.invoke(cli, catch_exceptions=False)
assert not result.exception
assert result.output == 'CUSTOM_FORMATTER | WARNING | oh\nCUSTOM_FORMATTER | ERROR | damn\n' BTW I considered adding some overwrite_formatter boolean parameter (defaults to True) in the init decorator. |
I meant that you can do this within the function, but actually this doesn't cover all cases. I'll think about it. |
Any news? :-) |
Recently |
Check out #10 for info on migration to the new API |
Hi,
I wanted to add support for explicitly defining formatter and logging level during declaration of the logger in the @init decorator.
I added unit-tests for this new feature.
I wanted to update documentation (of http://click.pocoo.org/dev/api/) accordingly, but unfortunately it seems that the documentation code is not part of the GitHub project.
I would appreciate if you could merge this code.
Thanks