logger.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:FogLAMP 作者: foglamp 项目源码 文件源码
def setup(logger_name: str = None,
          destination: int = SYSLOG,
          level: int = logging.WARNING,
          propagate: bool = False) -> logging.Logger:
    r"""Configures a `logging.Logger`_ object

    Once configured, a logger can also be retrieved via
    `logging.getLogger`_

    It is inefficient to call this function more than once for the same
    logger name.

    Args:
        logger_name:
            The name of the logger to configure. Use None (the default)
            to configure the root logger.

        level:
            The `logging level`_ to use when filtering log entries.
            Defaults to logging.WARNING.

        propagate:
            Whether to send log entries to ancestor loggers. Defaults to False.

        destination:
            - SYSLOG: (the default) Send messages to syslog.
                - View with: ``tail -f /var/log/syslog | sed 's/#012/\n\t/g'``
            - CONSOLE: Send message to stdout

    Returns:
        A `logging.Logger`_ object

    .. _logging.Logger: https://docs.python.org/3/library/logging.html#logging.Logger

    .. _logging level: https://docs.python.org/3/library/logging.html#levels

    .. _logging.getLogger: https://docs.python.org/3/library/logging.html#logging.getLogger
    """

    logger = logging.getLogger(logger_name)

    if destination == SYSLOG:
        handler = SysLogHandler(address='/dev/log')
    elif destination == CONSOLE:
        handler = logging.StreamHandler(sys.stdout)
    else:
        raise ValueError("Invalid destination {}".format(destination))

    # TODO: Consider using %r with message when using syslog .. \n looks better than #
    formatter = logging.Formatter(
        fmt='[FOGLAMP] %(asctime)s - %(levelname)s :: %(module)s: %(name)s: %(message)s',
        datefmt='%m-%d-%Y %H:%M:%S')

    handler.setFormatter(formatter)

    logger.setLevel(level)
    logger.propagate = propagate
    logger.addHandler(handler)

    return logger
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号