模塊 -logginghtml
一:在控制檯顯示:默認服務器
import logging app logging.debug("debug") socket logging.info("debug") this logging.warning("warning") spa logging.error("error") debug logging.critical("critical")日誌 |
二.輸入到文件,而且設置等級:orm
import logging htm
logging.basicConfig(filename="hello.log",level=logging.DEBUG, format='%(asctime)s %(name)s %(filename)s %(thread)d [%(levelno)s] : %(message)s',) logging.debug("debug") logging.info("debug") logging.warning("warning") logging.error("error") logging.critical("critical") |
使用到logging.basicConfig()
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. style If a format string is specified, use this to specify the type of format string (possible values '%', '{', '$', for %-formatting, :meth:`str.format` and :class:`string.Template` - defaults to '%'). level Set the root logger level to the specified level. stream Use the specified stream to initialize the StreamHandler. Note that this argument is incompatible with 'filename' - if both are present, 'stream' is ignored. handlers If specified, this should be an iterable of already created handlers, which will be added to the root handler. Any handler in the list which does not have a formatter assigned will be assigned the formatter created in this function. |
Format的使用:
使用help( logging.Formatter)查看使用方法 |
| %(name)s Name of the logger (logging channel) | %(levelno)s Numeric logging level for the message (DEBUG, INFO, | WARNING, ERROR, CRITICAL) | %(levelname)s Text logging level for the message ("DEBUG", "INFO", | "WARNING", "ERROR", "CRITICAL") | %(pathname)s Full pathname of the source file where the logging | call was issued (if available) | %(filename)s Filename portion of pathname | %(module)s Module (name portion of filename) | %(lineno)d Source line number where the logging call was issued | (if available) | %(funcName)s Function name | %(created)f Time when the LogRecord was created (time.time() | return value) | %(asctime)s Textual time when the LogRecord was created | %(msecs)d Millisecond portion of the creation time | %(relativeCreated)d Time in milliseconds when the LogRecord was created, | relative to the time the logging module was loaded | (typically at application startup time) | %(thread)d Thread ID (if available) | %(threadName)s Thread name (if available) | %(process)d Process ID (if available) | %(message)s The result of record.getMessage(), computed just as | the record is emitted |
三.既想在控制檯輸出,也想輸入文件,甚至使用udp發出.
寫法一:
import logging logger = logging.getLogger() fh = logging.FileHandler("test1",encoding="utf-8") sh = logging .StreamHandler() fm = logging.Formatter( '%(asctime)s %(name)s %(filename)s %(thread)d [%(levelno)s] : %(message)s', "%Y-%m-%d" ) sh.setFormatter(fm) fh.setFormatter(fm) logger.setLevel(logging.DEBUG) logger.debug("debug") logger.info("debug") logger.warning("warning") logger.error("error") logger.critical("critical") |
方法二:
import logging logger = logging.Logger("test", level=logging.DEBUG) sh = logging.StreamHandler() fh = logging.FileHandler("test1", encoding="utf-8") fm = logging.Formatter( '%(asctime)s %(name)s %(filename)s %(thread)d [%(levelno)s] : %(message)s', ) sh.setFormatter(fm) logger.addHandler(sh) logger.addHandler(fh) logger.debug("debug") logger.info("debug") logger.warning("warning") logger.error("error") logger.critical("critical") |
明天補充udp
-----------------------------------2017年10月27日11:30:09-----------------------更新---------------------------------------------
udp,,,,2333333333
方法三:
import logging.handlers # 建立handlers rfh = logging.handlers.RotatingFileHandler("ttt", mode='a', maxBytes=1024*10*10, backupCount=3, encoding="utf-8", delay=False) slh=logging.handlers.SysLogHandler(address=('192.168.1.5', 1234)) sh=logging.StreamHandler() # 設置basicConfig logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(name)s %(filename)s %(thread)d [%(lineno)d] : %(message)s', handlers=[rfh,slh,sh] ) logging.debug("debug") logging.info("debug") logging.warning("warning") logging.error("error") logging.critical("critical") |
handlers
具體:
http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html logging.StreamHandler: 日誌輸出到流,能夠是sys.stderr、sys.stdout或者文件 logging.FileHandler: 日誌輸出到文件 日誌回滾方式,實際使用時用RotatingFileHandler和TimedRotatingFileHandler logging.handlers.BaseRotatingHandler logging.handlers.RotatingFileHandler logging.handlers.TimedRotatingFileHandler logging.handlers.SocketHandler: 遠程輸出日誌到TCP/IP sockets logging.handlers.DatagramHandler: 遠程輸出日誌到UDP sockets logging.handlers.SMTPHandler: 遠程輸出日誌到郵件地址 logging.handlers.SysLogHandler: 日誌輸出到syslog logging.handlers.NTEventLogHandler: 遠程輸出日誌到Windows NT/2000/XP的事件日誌 logging.handlers.MemoryHandler: 日誌輸出到內存中的制定buffer logging.handlers.HTTPHandler: 經過"GET"或"POST"遠程輸出到HTTP服務器 |