华子的代码空间

逆水行舟,不进则退。 关注系统编程、网络编程、并发、分布式。

python logging 模块完整使用示例

支持文件、屏幕打印、电子邮件、TCP、UDP、syslog本地、syslog远程、windows事件、http server方式记录日志:

  1 #!/usr/bin/python
  2 import os
  3 import sys
  4 import logging
  5 import logging.config
  6 from logging import handlers
  7 
  8 DEBUG = True
  9 
 10 SYSLOG_HANDLER_HOST = 'localhost'
 11 
 12 LOG_PATH = '../server.log'
 13 
 14 MAIL_HANDLER_HOST = 'smtp.qq.com'
 15 MAIL_HANDLER_FROMADDR = 'user@qq.com'
 16 MAIL_HANDLER_TOADDRS = ['user1@qq.com','user2@gmail.com']
 17 MAIL_HANDLER_SUBJECT = 'Logging from python app'
 18 MAIL_HANDLER_CREDENTIALS = ('user@qq.com','password')
 19 
 20 TCPSOCKET_HANDLER_HOST = 'localhost'
 21 TCPSOCKET_HANDLER_PORT = 9022
 22 
 23 UDPSOCKET_HANDLER_HOST = 'localhost'
 24 UDPSOCKET_HANDLER_PORT = 9021
 25 
 26 NTEVENT_HANDLER_APPNAME = 'Python Application'
 27 NTEVENT_HANDLER_LOGTYPE = 'Application'
 28 
 29 HTTP_HANDLER_HOST = 'localhost:8000'
 30 HTTP_HANDLER_URL = '/logging'
 31 HTTP_HANDLER_METHOD = 'GET'
 32 
 33 
 34 LOGGING = {
 35     'version': 1,
 36     'disable_existing_loggers': True,
 37     'formatters': {
 38        'detail': {
 39             'format': '%(name)s %(levelname)s %(asctime)s %(module)s %(process)d %(thread)d [%(pathname)s:%(lineno)d] %(message)s'
 40         },
 41         'verbose': {
 42             'format': '%(name)s %(levelname)s %(asctime)s [%(pathname)s:%(lineno)d] %(message)s'
 43         },
 44         'simple': {
 45             'format': '%(name)s %(levelname)s %(message)s'
 46         },
 47     },
 48     'handlers': {
 49        'console':{
 50             'level':'NOTSET',
 51             'class':'logging.StreamHandler',
 52             'stream':sys.stderr,
 53             'formatter': 'verbose' #'simple'
 54         },
 55         'file':{
 56             'level':'DEBUG',
 57             'class':'logging.handlers.RotatingFileHandler',
 58             'filename': os.path.join(os.getcwd(), LOG_PATH),
 59             'formatter': 'verbose',
 60             'maxBytes': 1024*1024*20,  # 20MB
 61             'backupCount': 5,
 62         },
 63         'syslog.remote':{
 64             'level':'DEBUG',
 65             'class':'logging.handlers.SysLogHandler',
 66             'address':(SYSLOG_HANDLER_HOST,handlers.SYSLOG_UDP_PORT), # log to syslog or rsyslog server
 67             'formatter': 'verbose',
 68         },
 69         'mail.handler':{
 70             'level':'DEBUG',
 71             'class':'logging.handlers.SMTPHandler', # log to mailbox
 72             'mailhost':MAIL_HANDLER_HOST,
 73             'fromaddr':MAIL_HANDLER_FROMADDR,
 74             'toaddrs':MAIL_HANDLER_TOADDRS,
 75             'subject':MAIL_HANDLER_SUBJECT,
 76             'credentials':MAIL_HANDLER_CREDENTIALS,
 77             'formatter': 'detail',
 78         },
 79         'socket.tcp.handler':{
 80             'level':'DEBUG',
 81             'class':'logging.handlers.SocketHandler', # log to tcp socket
 82             'host':TCPSOCKET_HANDLER_HOST,
 83             'port':TCPSOCKET_HANDLER_PORT,
 84             'formatter': 'verbose',
 85         },
 86         'socket.udp.handler':{
 87             'level':'DEBUG',
 88             'class':'logging.handlers.DatagramHandler', # log to udp socket
 89             'host':UDPSOCKET_HANDLER_HOST,
 90             'port':UDPSOCKET_HANDLER_PORT,
 91             'formatter': 'verbose',
 92         },
 93         'http.handler':{
 94             'level':'DEBUG',
 95             'class':'logging.handlers.HTTPHandler', # log to http server
 96             'host':HTTP_HANDLER_HOST,
 97             'url':HTTP_HANDLER_URL,
 98             'method':HTTP_HANDLER_METHOD,
 99             'formatter': 'verbose',
100         }
101     },
102     'loggers': {
103         'CommonLogger': {
104             'handlers': ['console', 'file'] if DEBUG else ['file'],
105             'level': 'DEBUG' if DEBUG else 'DEBUG', #'INFO'
106             'propagate': False,
107             # very important in multithread environment, means disable propagation from current logger to the *root* logger.
108         },
109     }
110 }
111 
112 syslog_local = {
113             'level':'DEBUG',
114             'class':'logging.handlers.SysLogHandler',
115             'address':'/dev/log', # log to local syslog file
116             'formatter': 'verbose',
117         }
118 
119 ntevent_handler = {
120             'level':'DEBUG',
121             'class':'logging.handlers.NTEventLogHandler', # log to windows event log
122             'appname':NTEVENT_HANDLER_APPNAME,
123             'logtype':NTEVENT_HANDLER_LOGTYPE,
124             'formatter': 'verbose',
125         }
126 
127 common_logger = {
128             'handlers': ['console', 'file'] if DEBUG else ['file'],
129             'level': 'DEBUG' if DEBUG else 'DEBUG', #'INFO'
130             'propagate': False,
131             # very important in multithread environment, means disable propagation from current logger to the *root* logger.
132         }
133 
134 
135 if sys.platform == 'linux2':
136     LOGGING['handlers']['syslog.local'] = syslog_local
137 if sys.platform == 'win32':
138     LOGGING['handlers']['ntevent.handler'] = ntevent_handler
139 
140 def getlogger(logger_name=None):
141     if isinstance(logger_name,str) or isinstance(logger_name,unicode):
142         LOGGING['loggers'][logger_name] = common_logger
143         logging.config.dictConfig(LOGGING)
144         logger = logging.getLogger(logger_name)
145     else:
146         logging.config.dictConfig(LOGGING)
147         logger = logging.getLogger("CommonLogger")
148         
149     return logger

 

另附上一个接收tcp方式日志的服务器:

 1 import cPickle
 2 import logging
 3 import logging.handlers
 4 import SocketServer
 5 import struct
 6 
 7 
 8 class LogRecordStreamHandler(SocketServer.StreamRequestHandler):
 9     """Handler for a streaming logging request.
10 
11     This basically logs the record using whatever logging policy is
12     configured locally.
13     """
14 
15     def handle(self):
16         """
17         Handle multiple requests - each expected to be a 4-byte length,
18         followed by the LogRecord in pickle format. Logs the record
19         according to whatever policy is configured locally.
20         """
21         while 1:
22             chunk = self.connection.recv(4)
23             if len(chunk) < 4:
24                 break
25             slen = struct.unpack(">L", chunk)[0]
26             chunk = self.connection.recv(slen)
27             while len(chunk) < slen:
28                 chunk = chunk + self.connection.recv(slen - len(chunk))
29             obj = self.unPickle(chunk)
30             record = logging.makeLogRecord(obj)
31             self.handleLogRecord(record)
32 
33     def unPickle(self, data):
34         return cPickle.loads(data)
35 
36     def handleLogRecord(self, record):
37         # if a name is specified, we use the named logger rather than the one
38         # implied by the record.
39         if self.server.logname is not None:
40             name = self.server.logname
41         else:
42             name = record.name
43         logger = logging.getLogger(name)
44         # N.B. EVERY record gets logged. This is because Logger.handle
45         # is normally called AFTER logger-level filtering. If you want
46         # to do filtering, do it at the client end to save wasting
47         # cycles and network bandwidth!
48         logger.handle(record)
49 
50 class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer):
51     """simple TCP socket-based logging receiver suitable for testing.
52     """
53 
54     allow_reuse_address = 1
55 
56     def __init__(self, host='localhost',
57                  port=9022,
58                  handler=LogRecordStreamHandler):
59         SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
60         self.abort = 0
61         self.timeout = 1
62         self.logname = None
63 
64     def serve_until_stopped(self):
65         import select
66         abort = 0
67         while not abort:
68             rd, wr, ex = select.select([self.socket.fileno()],
69                                        [], [],
70                                        self.timeout)
71             if rd:
72                 self.handle_request()
73             abort = self.abort
74 
75 def main():
76     logging.basicConfig(
77         format="%(levelname)s %(name)s %(asctime)s [%(pathname)s:%(lineno)d] %(message)s")
78     tcpserver = LogRecordSocketReceiver()
79     print "About to start TCP server..."
80     tcpserver.serve_until_stopped()
81 
82 if __name__ == "__main__":
83     main()

 

见原文:http://docs.python.org/release/2.4.4/lib/network-logging.html

posted on 2012-09-05 00:14  华子的代码空间  阅读(3140)  评论(0编辑  收藏  举报

导航