logging.config.fileConfig : SyntaxError: (unicode error)
logging.config.fileConfig : SyntaxError: (unicode error)
当我运行以下Python3代码在CMD或Powershell :
# -*- coding: utf-8 -*-
import time
logging.config.fileConfig('log_config.ini',
defaults={'logfilename': time.strftime(str(__file__.split(".")[0].split("/")[-1]) + "_%Y%m%d-%H%M%S.log", time.localtime())})
我收到以下错误:
Traceback (most recent call last):
File "C:\Users\test.py", line 6, in <module>
defaults={'logfilename': time.strftime(str(__file__.split(".")[0].split("/")[-1]) + "_%Y%m%d-%H%M%S.log", time.localtime())})
File "C:\Python37-32\lib\logging\config.py", line 79, in fileConfig
handlers = _install_handlers(cp, formatters)
File "C:\Python37-32\lib\logging\config.py", line 142, in _install_handlers
args = eval(args, vars(logging))
File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \UXXXXXXXX escape
但是,我 PyCharm 没有遇到这个问题。
这是我的日志配置文件:
# Configuration
[logger_root]
level = DEBUG
handlers = consoleHandler, fileHandler
# ========================================= DECLARATIONS
[loggers]
keys = root
[handlers]
keys = consoleHandler,fileHandler
[formatters]
keys = simpleFormatter
# ========================================= DEFINITION DES HANDLERS
# Le handler par défaut écrit dans la console...
[handler_consoleHandler]
class = StreamHandler
level = DEBUG
formatter = simpleFormatter
args = (sys.stdout,)
# ...et un handler plus axé production qui écrit dans un fichier
[handler_fileHandler]
class = handlers.TimedRotatingFileHandler
level = INFO
formatter = simpleFormatter
args = ('logs/%(logfilename)s', 'D', 1, 30)
# ========================================= DEFINITION DES FORMATTERS
# Le formatter utilisé de façon standard
[formatter_simpleFormatter]
format = [%(asctime)s] - [%(levelname)s] - [%(name)s] - [%(filename)s:%(lineno)s] - %(message)s
如果有人遇到同样的问题有解决方法。
在PyCharm中,__file__
returns一个带“/”的字符串。
在 CMD 和 powershell 中,__file__
returns 带有“\”的字符串。
我们只需在 __file__
之后添加 .replace("\", "/")
:
# -*- coding: utf-8 -*-
import time
logging.config.fileConfig('log_config.ini',
defaults={'logfilename': time.strftime(str(__file__.replace("\", "/").split(".")[0].split("/")[-1]) + "_%Y%m%d-%H%M%S.log", time.localtime())})
当我运行以下Python3代码在CMD或Powershell :
# -*- coding: utf-8 -*-
import time
logging.config.fileConfig('log_config.ini',
defaults={'logfilename': time.strftime(str(__file__.split(".")[0].split("/")[-1]) + "_%Y%m%d-%H%M%S.log", time.localtime())})
我收到以下错误:
Traceback (most recent call last):
File "C:\Users\test.py", line 6, in <module>
defaults={'logfilename': time.strftime(str(__file__.split(".")[0].split("/")[-1]) + "_%Y%m%d-%H%M%S.log", time.localtime())})
File "C:\Python37-32\lib\logging\config.py", line 79, in fileConfig
handlers = _install_handlers(cp, formatters)
File "C:\Python37-32\lib\logging\config.py", line 142, in _install_handlers
args = eval(args, vars(logging))
File "<string>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \UXXXXXXXX escape
但是,我 PyCharm 没有遇到这个问题。
这是我的日志配置文件:
# Configuration
[logger_root]
level = DEBUG
handlers = consoleHandler, fileHandler
# ========================================= DECLARATIONS
[loggers]
keys = root
[handlers]
keys = consoleHandler,fileHandler
[formatters]
keys = simpleFormatter
# ========================================= DEFINITION DES HANDLERS
# Le handler par défaut écrit dans la console...
[handler_consoleHandler]
class = StreamHandler
level = DEBUG
formatter = simpleFormatter
args = (sys.stdout,)
# ...et un handler plus axé production qui écrit dans un fichier
[handler_fileHandler]
class = handlers.TimedRotatingFileHandler
level = INFO
formatter = simpleFormatter
args = ('logs/%(logfilename)s', 'D', 1, 30)
# ========================================= DEFINITION DES FORMATTERS
# Le formatter utilisé de façon standard
[formatter_simpleFormatter]
format = [%(asctime)s] - [%(levelname)s] - [%(name)s] - [%(filename)s:%(lineno)s] - %(message)s
如果有人遇到同样的问题有解决方法。
在PyCharm中,__file__
returns一个带“/”的字符串。
在 CMD 和 powershell 中,__file__
returns 带有“\”的字符串。
我们只需在 __file__
之后添加 .replace("\", "/")
:
# -*- coding: utf-8 -*-
import time
logging.config.fileConfig('log_config.ini',
defaults={'logfilename': time.strftime(str(__file__.replace("\", "/").split(".")[0].split("/")[-1]) + "_%Y%m%d-%H%M%S.log", time.localtime())})