【发布时间】:2018-08-20 16:36:35
【问题描述】:
我正在尝试在虚拟机(运行 Ubuntu-16.04)上为我的烧瓶项目实现日志记录。我有以下创建新目录的功能。
def mkdir_p(path):
try:
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
except FileExistsError as exc:
raise
以及继承自 RotatingFileHandler 的以下文件处理程序。
class MyRotatingFileHandler(RotatingFileHandler):
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
mkdir_p(os.path.dirname(filename))
RotatingFileHandler.__init__(self, filename, mode, maxBytes, backupCount, encoding, delay)
在运行时在我的本地计算机上注册新记录器时,这一切正常,但是当我尝试在 azure 实例上运行相同的代码时,我传入的路径和文件('log/error.log' ),未创建。
我已确保运行代码的用户在目录上设置了写权限。不过,我真的想不出任何其他可能发生这种情况的原因。
【问题讨论】:
-
你能确定 MyRotatingFileHandler.__init__() 确实被调用了吗?
-
为什么不使用
os.makedirs(os.path.dirname(filename))以及 mkdir_p 中多余的 raise 是怎么回事? -
Exception
TypeError: mkdir() got an unexpected keyword argument 'exist_ok'在 Python3.4.4 上引发 -
@JoshuaNixon 该参数是 3.5 的新参数。见docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir
-
因此,验证 azure 至少运行 3.5 可能是个好主意。
标签: python logging mkdir pathlib