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

Allow specifying custom formatter and log level in init decorator #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tomers
Copy link

@tomers tomers commented Jun 26, 2017

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

@untitaker
Copy link
Collaborator

Inside your CLI function you can just use logger.getLogger('name') and modify it as you like! We should document this.

@tomers
Copy link
Author

tomers commented Jun 28, 2017

# -*- 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?

@untitaker
Copy link
Collaborator

I meant that you can do this within the function, but actually this doesn't cover all cases. I'll think about it.

@tomers
Copy link
Author

tomers commented Sep 12, 2017

Any news? :-)

@untitaker
Copy link
Collaborator

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?

@untitaker
Copy link
Collaborator

Check out #10 for info on migration to the new API

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.

2 participants