2010-10-07 24 views
9

Il Python logging module è scomodo da usare. C'è un'alternativa più elegante? L'integrazione con le notifiche desktop sarebbe un vantaggio.alternative di registrazione python

+3

sembra essere molto promettente: http : //packages.python.org/Logbook – hoju

+1

In che modo il modulo di registrazione è scomodo? Cosa trovi carente nelle sue capacità? –

+4

hai usato? Non è pitonico. – hoju

risposta

7

È possibile esaminare Twiggy, è un tentativo iniziale di costruire un'alternativa più pitoni al modulo di registrazione.

+9

ha! log -> twig – hoju

+1

Concordo! Ha! Mi ci è voluto un secondo per averlo. – synthesizerpatel

+1

sembra morto ... l'ultimo commit era più di un anno fa – Tshepang

0
#!/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 

si può config file di log con console e di file, non credo tavolo notication è una buona idea, è possibile visualizzare le informazioni del registro da console e file di log

3

Si potrebbe desiderare di avere uno sguardo a pysimplelog. E 'pura pitone, molto semplice da usare, pip installabile e offre quello che serve

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 

e con quei parametri, saranno creati e aggiornati automaticamente per voi un file 'simplelog.log'

+0

+1 Ehi, che bello! Una nota tuttavia: non raccomanderei l'uso della registrazione * al di fuori della definizione della funzione * prima che sia noto [indipendentemente dal fatto che sia sicuro] (https://stackoverflow.com/questions/46356672/). –

Problemi correlati