<commit_before>from .formatter import SagaFormatter
import logging
import os
import sys


def get_logger(module):
    logger = logging.getLogger(module)

    def log(level, event, data=None, meta=None):
        if not isinstance(event, str):
            event = event.__repr__()
        if (data is not None and isinstance(data, str)):
            data = {'message': data}
        if (meta is not None and isinstance(meta, str)):
            meta = {'message': meta}
        logger.log(
            level,
            {
                'event': event,
                'data': data,
                'meta': meta
            }
        )

    for name, level in LOG_LEVELS.items():
        logger.__setattr__(
            name,
            lambda event, data=None, meta=None, level=level:
                log(level, event, data, meta))

    return logger

# Bunyan log levels are slightly different cfr:
# https://docs.python.org/2/library/logging.html#levels
LOG_LEVELS = {
    'fatal': 50,
    'error': 40,
    'warn': 30,
    'info': 20,
    'debug': 10,
    'trace': 0,
}


def get_log_level():
    log_level = os.environ.get('LOG_LEVEL')
    if log_level is None:
        return LOG_LEVELS['info']
    else:
        return LOG_LEVELS[log_level.lower()]

rootLogger = logging.getLogger()
logHandler = logging.StreamHandler(stream=sys.stdout)
formatter = SagaFormatter()
logHandler.setFormatter(formatter)
rootLogger.addHandler(logHandler)
rootLogger.setLevel(get_log_level())<commit_msg>fix(root): only on trace<commit_after>from .formatter import SagaFormatter
import logging
import os
import sys
from functools import partial

# Bunyan log levels are slightly different cfr:
# https://docs.python.org/2/library/logging.html#levels
LOG_LEVELS = {
    'fatal': 50,
    'error': 40,
    'warn': 30,
    'info': 20,
    'debug': 10,
    'trace': 0,
}

log_handler = logging.StreamHandler(stream=sys.stdout)
formatter = SagaFormatter()
log_handler.setFormatter(formatter)

log_level_name = os.environ.get('LOG_LEVEL')

if log_level_name is None:
    log_level = LOG_LEVELS['info']
else:
    log_level = LOG_LEVELS[log_level.lower()]

if log_level == LOG_LEVELS['trace']:
    rootLogger = logging.getLogger()
    rootLogger.addHandler(log_handler)
    rootLogger.setLevel(log_level)


def get_logger(module):
    logger = logging.getLogger(module)
    if log_level > 0:
        logger.addHandler(log_handler)
        logger.setLevel(log_level)

    def log(event, data=None, meta=None, level=0):
        if not isinstance(event, str):
            event = event.__repr__()
        if (data is not None and isinstance(data, str)):
            data = {'message': data}
        if (meta is not None and isinstance(meta, str)):
            meta = {'message': meta}
        logger.log(
            level,
            {
                'event': event,
                'data': data,
                'meta': meta
            }
        )

    for name, level in LOG_LEVELS.items():
        logger.__setattr__(name, partial(log, level=level))

    return logger
{'fraction_non_alphanumeric': 0.11683623235969806, 'fraction_numerical': 0.00853298326222514, 'mean_word_length': 2.5318655851680187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '2043281', 'n_tokens_mistral': 962, 'n_tokens_neox': 920, 'n_words': 236}