<commit_before>"""Logging related classes and functions.

Classes
-------
-   IndentFormatter          - Sets the log-message indentation level based on the stack depth.

Functions
---------
-   getLogger                - Create a logger object which logs to file and or stdout stream.
-   defaultLogger            - Create a ``logging.Logger`` object which logs to the out stream.

"""
from __future__ import absolute_import, division, print_function, unicode_literals

import logging
import inspect
import numpy as np

__all__ = ['IndentFormatter', 'getLogger', 'defaultLogger']


class IndentFormatter(logging.Formatter):
    """Logging formatter where the depth of the stack sets the message indentation level.
    """

    def __init__(self, fmt=None, datefmt=None):
        logging.Formatter.__init__(self, fmt, datefmt)
        self.baseline = None

    def format(self, rec):
        stack = inspect.stack()
        if self.baseline is None: self.baseline = len(stack)
        indent = (len(stack)-self.baseline)
        addSpace = ((indent > 0) & (not rec.msg.startswith(" -")))
        rec.indent = ' -'*indent + ' '*addSpace
        out = logging.Formatter.format(self, rec)
        del rec.indent
        return out


def getLogger(name, strFmt=None, fileFmt=None, dateFmt=None,
              strLevel=logging.WARNING, fileLevel=logging.DEBUG,
              tofile=None, tostr=True):
    """Create a standard logger object which logs to file and or stdout stream.

    If logging to output stream (stdout) is enabled, an `IndentFormatter` object is used.

    Arguments
    ---------
    name : str,
        Handle for this logger, must be distinct for a distinct logger.
    strFmt : str or `None`,
        Format of log messages to stream (stdout).  If `None`, default settings are used.
    fileFmt : str or `None`,
        Format of log messages to file.  If `None`, default settings are used.
    dateFmt : str or `None`
        Format of time stamps to stream and/or file.  If `None`, default settings are used.
    strLevel : int,
        Logging level for stream.
    fileLevel : int,
        Logging level for file.
    tofile : str or `None`,
        Filename to log to (turned off if `None`).
    tostr : bool,
        Log to stdout stream.

    Returns
    -------
    logger : ``logging.Logger`` object,
        Logger object to use for logging.

    """

    if tofile is None and not tostr: raise ValueError("Must log to something!")

    logger = logging.getLogger(name)
    # Make sure handlers don't get duplicated (ipython issue)
    while len(logger.handlers) > 0: logger.handlers.pop()
    # Prevents duplication or something something...
    logger.propagate = 0

    # Determine and Set Logging Levels
    if fileLevel is None: fileLevel = logging.DEBUG
    if strLevel is None: strLevel = logging.WARNING
    #     Logger object must be at minimum level
    logger.setLevel(np.min([fileLevel, strLevel]))

    if dateFmt is None: dateFmt = '%Y/%m/%d %H:%M:%S'

    # Log to file
    # -----------
    if tofile is not None:
        if fileFmt is None:
            fileFmt = "%(asctime)s %(levelname)8.8s [%(filename)20.20s:"
            fileFmt += "%(funcName)-20.20s]%(indent)s%(message)s"
<commit_msg>BUG: need to use normal 'int' type object in logger level.<commit_after>"""Logging related classes and functions.

Classes
-------
-   IndentFormatter          - Sets the log-message indentation level based on the stack depth.

Functions
---------
-   getLogger                - Create a logger object which logs to file and or stdout stream.
-   defaultLogger            - Create a ``logging.Logger`` object which logs to the out stream.

"""
from __future__ import absolute_import, division, print_function, unicode_literals

import logging
import inspect
import numpy as np

__all__ = ['IndentFormatter', 'getLogger', 'defaultLogger']


class IndentFormatter(logging.Formatter):
    """Logging formatter where the depth of the stack sets the message indentation level.
    """

    def __init__(self, fmt=None, datefmt=None):
        logging.Formatter.__init__(self, fmt, datefmt)
        self.baseline = None

    def format(self, rec):
        stack = inspect.stack()
        if self.baseline is None: self.baseline = len(stack)
        indent = (len(stack)-self.baseline)
        addSpace = ((indent > 0) & (not rec.msg.startswith(" -")))
        rec.indent = ' -'*indent + ' '*addSpace
        out = logging.Formatter.format(self, rec)
        del rec.indent
        return out


def getLogger(name, strFmt=None, fileFmt=None, dateFmt=None,
              strLevel=logging.WARNING, fileLevel=logging.DEBUG,
              tofile=None, tostr=True):
    """Create a standard logger object which logs to file and or stdout stream.

    If logging to output stream (stdout) is enabled, an `IndentFormatter` object is used.

    Arguments
    ---------
    name : str,
        Handle for this logger, must be distinct for a distinct logger.
    strFmt : str or `None`,
        Format of log messages to stream (stdout).  If `None`, default settings are used.
    fileFmt : str or `None`,
        Format of log messages to file.  If `None`, default settings are used.
    dateFmt : str or `None`
        Format of time stamps to stream and/or file.  If `None`, default settings are used.
    strLevel : int,
        Logging level for stream.
    fileLevel : int,
        Logging level for file.
    tofile : str or `None`,
        Filename to log to (turned off if `None`).
    tostr : bool,
        Log to stdout stream.

    Returns
    -------
    logger : ``logging.Logger`` object,
        Logger object to use for logging.

    """

    if tofile is None and not tostr: raise ValueError("Must log to something!")

    logger = logging.getLogger(name)
    # Make sure handlers don't get duplicated (ipython issue)
    while len(logger.handlers) > 0: logger.handlers.pop()
    # Prevents duplication or something something...
    logger.propagate = 0

    # Determine and Set Logging Levels
    if fileLevel is None: fileLevel = logging.DEBUG
    if strLevel is None: strLevel = logging.WARNING
    #     Logger object must be at minimum level (`np` int doesnt work, need regular int)
    logger.setLevel(int(np.min([fileLevel, strLevel]).astype(int)))

    if dateFmt is None: dateFmt = '%Y/%m/%d %H:%M:%S'

    # Log to file
    # -----------
    if tofile is not None:
        if fileFmt is None:
            fileFmt = "%(asctime)s %(levelname)8.8s [%(filename)20.20s:"
            fileFmt += "%(funcName)-20.20s]%(indent)s%(message)s"

{'fraction_non_alphanumeric': 0.10819024464367118, 'fraction_numerical': 0.003950767360583498, 'mean_word_length': 2.5501618122977345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2085776', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1849, 'n_words': 691}