2011-10-27 22 views
15

Cố gắng sử dụng tệp cấu hình ghi nhật ký để triển khai TimedRotatinigFileHandler.Python 2.4.3: ConfigParser.NoSectionError: Không có phần: 'formatters'

Chỉ cần không lấy tệp cấu hình vì một số lý do.

Bất kỳ đề xuất nào được đánh giá cao.


x.py:

import logging 
import logging.config 
import logging.handlers 

logging.config.fileConfig("x.ini") 

MyLog = logging.getLogger('x') 

MyLog.debug('Starting') 

x.ini:

[loggers] 
keys=root 

[logger_root] 
level=NOTSET 
handlers=trfhand 

[handlers] 
keys=trfhand 

[handler_trfhand] 
class=handlers.TimedRotatingFileHandler 
when=M 
interval=1 
backupCount=11 
formatter=generic 
level=DEBUG 
args=('/var/log/x.log',) 

[formatters] 
keys=generic 

[formatter_generic] 
class=logging.Formatter 
format=%(asctime)s %(levelname)s %(message)s 
datefmt= 

Traceback (most recent call last): 
    File "x.py", line 5, in ? 
    logging.config.fileConfig("x.ini") 
    File "/usr/lib/python2.4/logging/config.py", line 76, in fileConfig 
    flist = cp.get("formatters", "keys") 
    File "/usr/lib/python2.4/ConfigParser.py", line 511, in get 
    raise NoSectionError(section) 
ConfigParser.NoSectionError: No section: 'formatters' 

Cảm ơn

+0

Cảm ơn Adam! Điều đó là vậy đó. – user981163

+6

Sửa lỗi là gì? – ProNeticas

Trả lời

62

Thông báo lỗi là chính xác nhưng gây hiểu nhầm.

Lý do phần "định dạng" bị thiếu, là do mô-đun ghi nhật ký không thể tìm thấy tệp bạn đã chuyển đến logging.config.fileConfig.

Hãy thử sử dụng đường dẫn tệp tuyệt đối.

+1

Cảm ơn bạn rất nhiều! Thông báo ngoại lệ không hữu ích lắm. Tạo một tập tin gọi là logging.ini giải quyết vấn đề. –

Các vấn đề liên quan