cookiecutter.log   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 29
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A configure_logger() 0 33 2
1
"""Module for setting up logging."""
2
import logging
3
import sys
4
5
LOG_LEVELS = {
6
    'DEBUG': logging.DEBUG,
7
    'INFO': logging.INFO,
8
    'WARNING': logging.WARNING,
9
    'ERROR': logging.ERROR,
10
    'CRITICAL': logging.CRITICAL,
11
}
12
13
LOG_FORMATS = {
14
    'DEBUG': '%(levelname)s %(name)s: %(message)s',
15
    'INFO': '%(levelname)s: %(message)s',
16
}
17
18
19
def configure_logger(stream_level='DEBUG', debug_file=None):
20
    """Configure logging for cookiecutter.
21
22
    Set up logging to stdout with given level. If ``debug_file`` is given set
23
    up logging to file with DEBUG level.
24
    """
25
    # Set up 'cookiecutter' logger
26
    logger = logging.getLogger('cookiecutter')
27
    logger.setLevel(logging.DEBUG)
28
29
    # Remove all attached handlers, in case there was
30
    # a logger with using the name 'cookiecutter'
31
    del logger.handlers[:]
32
33
    # Create a file handler if a log file is provided
34
    if debug_file is not None:
35
        debug_formatter = logging.Formatter(LOG_FORMATS['DEBUG'])
36
        file_handler = logging.FileHandler(debug_file)
37
        file_handler.setLevel(LOG_LEVELS['DEBUG'])
38
        file_handler.setFormatter(debug_formatter)
39
        logger.addHandler(file_handler)
40
41
    # Get settings based on the given stream_level
42
    log_formatter = logging.Formatter(LOG_FORMATS[stream_level])
43
    log_level = LOG_LEVELS[stream_level]
44
45
    # Create a stream handler
46
    stream_handler = logging.StreamHandler(stream=sys.stdout)
47
    stream_handler.setLevel(log_level)
48
    stream_handler.setFormatter(log_formatter)
49
    logger.addHandler(stream_handler)
50
51
    return logger
52