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

How to add logging filters to coloredlogs? #32

Closed
kingspp opened this issue Jun 13, 2017 · 2 comments
Closed

How to add logging filters to coloredlogs? #32

kingspp opened this issue Jun 13, 2017 · 2 comments

Comments

@kingspp
Copy link

kingspp commented Jun 13, 2017

How to add filters to coloredlogs.install()?

Scenario:

I am loading logging configuration using logging.dictConfig as a yaml file. I have defined respective filters in the config file, the filters are not added to colored logs.

Usecase:

Stream INFO, WARN and DEBUG to stdout and ERROR and CRITICAL to stderr.

Code:

import logging
logger = logging.getLogger(__name__)
logger.info('INFO')
logger.error('ERROR')

Output:

# Actual Output:
2017-06-13 13:40:40 - INFO - __main__(117) - INFO
2017-06-13 13:40:40 - ERROR - __main__(118) - ERROR
2017-06-13 13:40:40,642 - ERROR <PID 89531:MainProcess> __main__.<module>(): ERROR

# Expected Output:
2017-06-13 13:40:40 - INFO - __main__(117) - INFO
2017-06-13 13:40:40,642 - ERROR <PID 89531:MainProcess> __main__.<module>(): ERROR

Logging Config: in init.py

import logging
import yaml
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
coloredlogs.install(fmt=config['formatters']['standard']['format'], stream=sys.stdout,
                                    level=logging.INFO)

YAML Config

version: 1
disable_existing_loggers: true


filters:
    info_filter:
        () : lib.InfoFilter        
    error_filter:
        () : lib.ErrorFilter

formatters:
    standard:
        format: "%(asctime)s - %(levelname)s - %(name)s(%(lineno)d) - %(message)s"    
    error:
        format: "%(asctime)s - %(levelname)s <PID %(process)d:%(processName)s> %(name)s.%(funcName)s(): %(message)s"

handlers:
    console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: standard 
        filters: [info_filter]
        stream: ext://sys.stdout
        
    error_console:
        class: logging.StreamHandler
        level: ERROR
        formatter: error  
        filters: [error_filter]
        stream: ext://sys.stderr
        

    info_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: INFO
        formatter: standard
        filename: /tmp/info.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

    error_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: ERROR
        formatter: error
        filename: /tmp/errors.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

    debug_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: DEBUG
        formatter: standard
        filename: /tmp/debug.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

    critical_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: CRITICAL
        formatter: standard
        filename: /tmp/critical.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

    warn_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: WARN
        formatter: standard
        filename: /tmp/warn.log
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

root:
    level: NOTSET
    handlers: [console, error_console]
    propogate: no

loggers:
    lib:
        level: DEBUG
        handlers: [info_file_handler, error_file_handler, critical_file_handler, debug_file_handler, 
                          warn_file_handler]
        propogate: no
@prathyush-rzt
Copy link

@xolox Is this fixed in the latest build?

@xolox
Copy link
Owner

xolox commented Dec 10, 2020

Hi there, thanks for the feedback and sorry for never replying here before now. I've just merged #89 by @eblis which is said to resolve this issue, so please try out release 14.3.

@xolox xolox closed this as completed Dec 10, 2020
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

No branches or pull requests

3 participants