2016-07-29 166 views
0

我想將我當前的項目從python 2.7轉換爲3.5。首要任務之一是配置日誌記錄。我使用靈活的配置文件,日期是文件名的一部分。 以下是設置了文件處理程序,在2.7Python 3.4日誌記錄配置

[handler_fileHandler] 
class=FileHandler 
level=DEBUG 
formatter=simpleFormatter 
# Only one log per day will be created. All messages will be appended to it. 
args=("D:\\Logs\\PyLogs\\" + time.strftime("%Y%m%d%H%M%S")+'.log', 'a') 

工作正常在3.5出現以下錯誤代碼:

configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%Y%m%d%H%M%S")+\'.log\', \'a\')'

有沒有人經歷這件事?有沒有更好的方式來格式化配置文件中的日期?

+0

替代我很驚訝這個工作都在2.7。爲什麼不使用['TimedRotatingFileHandler'](https://docs.python.org/3/library/logging.handlers.html#timedrotatingfilehandler)? 'class = TimedRotatingFileHandler',用'when ='D'。 –

+0

我沒有使用TimeRotatingFileHandler,因爲它沒有做我想要的,但謝謝你的建議。 –

回答

1

主題類似這個帖子: Configparser and string with %

我想你可能需要%

+0

我看了之前,它沒有提供任何幫助 –

+0

加倍'%'沒有解決它? –

+0

其實它修正了它。 我沒有嘗試過,因爲我認爲它只是將問題從configparser轉換爲time.strftime。 –