一、

import logging

logging.basicConfig(filename='...',level=logging.DEBUG)

#filename  Specifies that a FileHandler be created, using the specified filename, rather than a StreamHandler.
#filemode  Specifies the mode to open the file, if filename is specified (if filemode is unspecified, it defaults to 'a').
#format  Use the specified format string for the handler.
#datefmt  Use the specified date/time format.
#level  Set the root logger level to the specified level.(logging.NOTSET,DEBUG,INFO,WARNING,ERROR,CRITICAL(0,10,20,30,40,50)),getLevelName()
#stream  Use the specified stream to initialize the StreamHandler. Notethat this argument is incompatible with 'filename' - if bothare present, 'stream' is ignored.

 logging.debug("xxx")

#logging.info(),warning(),error(),critical()

二、

import logging

logger=logging.getLogger()

handler=logging.FileHandler('...') #(filename, mode='a', encoding=None, delay=0) filename=std.out?

#logging.StreamHandler(stream=std.err)

#logging.handlers.RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)

formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') #default "%(message)s"

handler.setFormatter(formatter)

logger.addHandler(handler)

logger.setLevel(logging.NOTSET)  #default warning(30)

logger.debug()

三、from flask doc

FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)

would print something like

2006-02-08 22:20:02,165 192.168.0.1 fbloggs  Protocol problem: connection reset
posted on 2013-11-27 11:12  perel  阅读(279)  评论(0编辑  收藏  举报