<commit_before># -*- coding: utf-8 -*-
"""Watchmaker logger module."""
import logging
import os


def prepare_logging(log_dir, log_level):
    """
    Prepare the logger for handling messages to a file and/or to stdout.

    Args:
        log_dir (:obj:`str`):
            Path to a directory. If set, Watchmaker logs to the
            ``watchmaker.log`` file in the specified directory. Both the
            directory and the file will be created if necessary. If the file
            already exists, Watchmaker appends to it rather than overwriting
            it. If this argument evaluates to ``False``, then logging to a file
            is disabled. Watchmaker will always output to stdout/stderr.
        log_level (int):
            Level to log at. Any value other than the integers below will
            enable DEBUG logging.

            .. code-block:: yaml

                0: WARNING
                1: INFO
                *: DEBUG
    """
    logformat = (
        '%(asctime)s [%(name)s][%(levelname)-5s][%(process)s]: %(message)s'
    )
    if log_level == 0:
        level = logging.WARNING
    elif log_level == 1:
        level = logging.INFO
    else:
        level = logging.DEBUG

    logging.basicConfig(format=logformat, level=level)

    if not log_dir:
        logging.warning(
            'Watchmaker will not be logging to a file!'
        )
    else:
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        log_filename = os.sep.join((log_dir, 'watchmaker.log'))
        hdlr = logging.FileHandler(log_filename)
        hdlr.setLevel(level)
        hdlr.setFormatter(logging.Formatter(logformat))
        logging.getLogger().addHandler(hdlr)<commit_msg>Uses a map to lookup the log level<commit_after># -*- coding: utf-8 -*-
"""Watchmaker logger module."""
import collections
import logging
import os

LOG_LEVELS = collections.defaultdict(
    lambda: logging.DEBUG,  # log level if key is not in this dict
    {
        0: logging.WARNING,
        1: logging.INFO
    }
)


def prepare_logging(log_dir, log_level):
    """
    Prepare the logger for handling messages to a file and/or to stdout.

    Args:
        log_dir (:obj:`str`):
            Path to a directory. If set, Watchmaker logs to the
            ``watchmaker.log`` file in the specified directory. Both the
            directory and the file will be created if necessary. If the file
            already exists, Watchmaker appends to it rather than overwriting
            it. If this argument evaluates to ``False``, then logging to a file
            is disabled. Watchmaker will always output to stdout/stderr.
        log_level (int):
            Level to log at. Any value other than the integers below will
            enable DEBUG logging.

            .. code-block:: yaml

                0: WARNING
                1: INFO
                *: DEBUG
    """
    logformat = (
        '%(asctime)s [%(name)s][%(levelname)-5s][%(process)s]: %(message)s'
    )
    level = LOG_LEVELS[log_level]

    logging.basicConfig(format=logformat, level=level)

    if not log_dir:
        logging.warning(
            'Watchmaker will not be logging to a file!'
        )
    else:
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        log_filename = os.sep.join((log_dir, 'watchmaker.log'))
        hdlr = logging.FileHandler(log_filename)
        hdlr.setLevel(level)
        hdlr.setFormatter(logging.Formatter(logformat))
        logging.getLogger().addHandler(hdlr)
{'fraction_non_alphanumeric': 0.0951974992895709, 'fraction_numerical': 0.0034100596760443308, 'mean_word_length': 2.1150442477876106, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '344650', 'n_tokens_mistral': 1006, 'n_tokens_neox': 974, 'n_words': 324}