The Python logging module is cumbersome to use. Is there a more elegant alternative? Integration with desktop notifications would be a plus.
            Asked
            
        
        
            Active
            
        
            Viewed 8,197 times
        
    20
            
            
        - 
                    5Looks promising: http://packages.python.org/Logbook – hoju Oct 07 '10 at 02:41
 - 
                    3In what ways do you find the logging module to be cumbersome? What do you find lacking in its capabilities? – Nathan Davis Oct 07 '10 at 03:33
 - 
                    @NathanDavis well just to get started you need to write about 6 lines... want to add to stdout too - thats about another 4, if you can get it to work. – dreab Jun 30 '20 at 08:09
 - 
                    1Anyways, for people who are here, just like me, structlog, loguru are two good alternatives – rawwar Jul 27 '22 at 03:06
 - 
                    1@InAFlash isn't it weird that there is a library for everything in python BUT logging LOL. There's no single decent one. However, structlog looks quite promissing. – t3chb0t Aug 03 '22 at 09:30
 - 
                    i mean, why to worry when print("-----> I'm here <-------") works ? – rawwar Aug 03 '22 at 09:32
 - 
                    1@InAFlash it sure does... but not so much if you want to send the logs into a database to let another app analze them and send alarms. – t3chb0t Aug 03 '22 at 09:46
 - 
                    i found loggin so slow , my code became 10 time slower – urek mazino Mar 12 '23 at 20:59
 
4 Answers
8
            You can look into Twiggy, it's an early stage attempt to build a more pythonic alternative to the logging module.
        erikbstack
        
- 12,878
 - 21
 - 81
 - 115
 
        AndrewF
        
- 6,852
 - 7
 - 29
 - 27
 
- 
                    1
 - 
                    2Not dead, but moved. The documentation is at https://twiggy.readthedocs.org and the development is on Github at https://github.com/wearpants/twiggy/ – Ian E Aug 06 '13 at 00:09
 - 
                    As of today it seems not to be available for Python 3.x - and also not in a proper state for 2.x, the github readme says: "master is currently in flux and may be broken." – SHernandez May 10 '15 at 11:18
 - 
                    @SHernandez: I tried it today on 3.6 and it works fine. Not sure how robust this is but at least the example ran OK. – WoJ Feb 28 '19 at 21:06
 
6
            
            
        Checkout logbook, it is much nicer to work with.
Logbook was mentioned in a comment but it deserves its own answer.
        derchambers
        
- 904
 - 13
 - 19
 
- 
                    Tried it. It has the same problem as stdlib logging: The documentation is not good, in my opinion. And there are a lot less users, so no Google results for questions. I went back to stdlib logging, and found https://stackoverflow.com/questions/7507825/where-is-a-complete-example-of-logging-config-dictconfig most helpful. – Andreas Haferburg Feb 25 '20 at 13:55
 
5
            
            
        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
- 
                    +1 Hey that looks cool! One note however: I would not recommend using logging *outside of a function defintion* before it is known [whether that is safe](https://stackoverflow.com/questions/46356672/). – personal_cloud Sep 23 '17 at 05:22
 
-2
            
            
        #!/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
        wcc526
        
- 3,915
 - 2
 - 31
 - 29