我想将自己应用程序的日志文件放在正确的位置。我不确定,但我认为在unixoid系统上它是/var/log
。
但是权限存在问题。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging, logging.handlers
_LOG_FILENAME = '/var/log/myapp.log'
log = logging.getLogger('')
fh = logging.handlers.RotatingFileHandler(_LOG_FILENAME, maxBytes=10240, backupCount=3)
log.addHandler(fh)
log.info('log message')
导致此错误:
Traceback (most recent call last):
File "./log.py", line 8, in <module>
fh = logging.handlers.RotatingFileHandler(_LOG_FILENAME, maxBytes=10240, backupCount=3)
File "/usr/lib/python3.4/logging/handlers.py", line 150, in __init__
BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
File "/usr/lib/python3.4/logging/handlers.py", line 57, in __init__
logging.FileHandler.__init__(self, filename, mode, encoding, delay)
File "/usr/lib/python3.4/logging/__init__.py", line 992, in __init__
StreamHandler.__init__(self, self._open())
File "/usr/lib/python3.4/logging/__init__.py", line 1016, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: '/var/log/myapp.log'
答案 0 :(得分:2)
默认情况下,要将日志文件放在/ var / log中,您的应用程序必须以root身份运行。
提示:创建子目录并设置好主人
mkdir /var/log/myapp
chown myuser /var/log/myapp
最好的考虑