click-log
click-log copied to clipboard
Allow specifying custom formatter and log level in init decorator
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
Inside your CLI function you can just use logger.getLogger('name')
and modify it as you like! We should document this.
# -*- 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. What do you think should be the solution to this problem?
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 click_log.init
vanished and click_log.basic_config
can be called anytime you like. Therefore you can first call basic_config
and then use your own code to modify the logger. Does that cover your usecase?
Check out #10 for info on migration to the new API