python logging alternatives

2019-03-15 16:23发布

The Python logging module is cumbersome to use. Is there a more elegant alternative? Integration with desktop notifications would be a plus.

4条回答
手持菜刀,她持情操
2楼-- · 2019-03-15 16:34

Checkout logbook, it is much nicer to work with.

Logbook was mentioned in a comment but it deserves its own answer.

查看更多
Ridiculous、
3楼-- · 2019-03-15 16:47

You can look into Twiggy, it's an early stage attempt to build a more pythonic alternative to the logging module.

查看更多
混吃等死
4楼-- · 2019-03-15 16:47

You might want to have a look at pysimplelog. It's pure python, very simple to use, pip installable and provides what you need

from pysimplelog import Logger
L=Logger()
print L
>>> Logger (Version 0.2.1)
>>> log type  |log name  |level     |std flag  |file flag |
>>> ----------|----------|----------|----------|----------|
>>> debug     |DEBUG     |0.0       |True      |True      |
>>> info      |INFO      |10.0      |True      |True      |
>>> warn      |WARNING   |20.0      |True      |True      |
>>> error     |ERROR     |30.0      |True      |True      |
>>> critical  |CRITICAL  |100.0     |True      |True      |

L.info('I am an info')
>>> 2016-09-26 15:01:17 - logger <INFO> I am an info

L.warn('I am a warning')
>>> 2016-09-26 15:01:17 - logger <WARNING> I am a warning

L.error('I am an error')
>>> 2016-09-26 15:01:17 - logger <ERROR> I am an error

and with those parameters, a 'simplelog.log' file will be created and updated automatically for you

查看更多
Deceive 欺骗
5楼-- · 2019-03-15 16:48
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import logging.handlers
from logging.config import dictConfig

logger = logging.getLogger(__name__)

DEFAULT_LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
}
def configure_logging(logfile_path):
    """
    Initialize logging defaults for Project.

    :param logfile_path: logfile used to the logfile
    :type logfile_path: string

    This function does:

    - Assign INFO and DEBUG level to logger file handler and console handler

    """
    dictConfig(DEFAULT_LOGGING)

    default_formatter = logging.Formatter(
        "[%(asctime)s] [%(levelname)s] [%(name)s] [%(funcName)s():%(lineno)s] [PID:%(process)d TID:%(thread)d] %(message)s",
        "%d/%m/%Y %H:%M:%S")

    file_handler = logging.handlers.RotatingFileHandler(logfile_path, maxBytes=10485760,backupCount=300, encoding='utf-8')
    file_handler.setLevel(logging.INFO)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)

    file_handler.setFormatter(default_formatter)
    console_handler.setFormatter(default_formatter)

    logging.root.setLevel(logging.DEBUG)
    logging.root.addHandler(file_handler)
    logging.root.addHandler(console_handler)



[31/10/2015 22:00:33] [DEBUG] [yourmodulename] [yourfunction_name():9] [PID:61314 TID:140735248744448] this is logger infomation from hello module

you can config logfile with console and file,I don't think desktop notication is a good idea,you can see the log information from console and logfiles

查看更多
登录 后发表回答