我對Web服務器有以下代碼。我有一個問題,httpd.log
沒有打印任何東西 - 我無能爲力。該文件被創建,如果我將一個print()
語句添加到log_message()
它將被打印出來,但是沒有任何內容被寫入該文件。當我在`SimpleHTTPRequestHandler`中重寫`log_message()`時,沒有什麼會被寫入文件中
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
import logging
from logging.handlers import RotatingFileHandler
class MyHandler(SimpleHTTPRequestHandler):
def __init__(self, *args):
self.logger = logging.getLogger("httpd")
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh = RotatingFileHandler("httpd.log", mode='wa',
maxBytes=1 << 20, backupCount=2)
fh.setFormatter(formatter)
fh.setLevel(logging.INFO)
self.logger.addHandler(fh)
self.logger.info("Creating HTTP Request Handler")
super(SimpleHTTPRequestHandler, self).__init__(*args)
def log_message(self, format, *args):
self.logger.info("%s - - [%s] %s\n" %
(self.address_string(),
self.log_date_time_string(),
format%args))
def main():
server = HTTPServer(('', 80), MyHandler)
server.serve_forever()
if __name__ == '__main__':
main()
這是關於Python 3.1.3
您可以通過調用logger.getEffectiveLevel()來找出記錄器正在使用的級別。 – 2012-02-02 13:42:32