Skip to content

Commit b8f8d1d

Browse files
authored
Merge pull request freqtrade#10831 from xzmeng/fix-log
feat: auto-create logs dir if it's absent
2 parents c9ae5e1 + c4cbf6d commit b8f8d1d

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

freqtrade/loggers/__init__.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from logging import Formatter
33
from logging.handlers import RotatingFileHandler, SysLogHandler
4+
from pathlib import Path
45

56
from freqtrade.constants import Config
67
from freqtrade.exceptions import OperationalException
@@ -86,11 +87,23 @@ def setup_logging(config: Config) -> None:
8687
handler_rf = get_existing_handlers(RotatingFileHandler)
8788
if handler_rf:
8889
logging.root.removeHandler(handler_rf)
89-
handler_rf = RotatingFileHandler(
90-
logfile,
91-
maxBytes=1024 * 1024 * 10, # 10Mb
92-
backupCount=10,
93-
)
90+
try:
91+
logfile_path = Path(logfile)
92+
logfile_path.parent.mkdir(parents=True, exist_ok=True)
93+
handler_rf = RotatingFileHandler(
94+
logfile_path,
95+
maxBytes=1024 * 1024 * 10, # 10Mb
96+
backupCount=10,
97+
)
98+
except PermissionError:
99+
raise OperationalException(
100+
f'Failed to create or access log file "{logfile_path.absolute()}". '
101+
"Please make sure you have the write permission to the log file or its parent "
102+
"directories. If you're running freqtrade using docker, you see this error "
103+
"message probably because you've logged in as the root user, please switch to "
104+
"non-root user, delete and recreate the directories you need, and then try "
105+
"again."
106+
)
94107
handler_rf.setFormatter(Formatter(LOGFORMAT))
95108
logging.root.addHandler(handler_rf)
96109

tests/test_log_setup.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_set_loggers_Filehandler(tmp_path):
8686
logger = logging.getLogger()
8787
orig_handlers = logger.handlers
8888
logger.handlers = []
89-
logfile = tmp_path / "ft_logfile.log"
89+
logfile = tmp_path / "logs/ft_logfile.log"
9090
config = {
9191
"verbosity": 2,
9292
"logfile": str(logfile),
@@ -107,6 +107,29 @@ def test_set_loggers_Filehandler(tmp_path):
107107
logger.handlers = orig_handlers
108108

109109

110+
@pytest.mark.skipif(sys.platform == "win32", reason="does not run on windows")
111+
def test_set_loggers_Filehandler_without_permission(tmp_path):
112+
logger = logging.getLogger()
113+
orig_handlers = logger.handlers
114+
logger.handlers = []
115+
116+
try:
117+
tmp_path.chmod(0o400)
118+
logfile = tmp_path / "logs/ft_logfile.log"
119+
config = {
120+
"verbosity": 2,
121+
"logfile": str(logfile),
122+
}
123+
124+
setup_logging_pre()
125+
with pytest.raises(OperationalException):
126+
setup_logging(config)
127+
128+
logger.handlers = orig_handlers
129+
finally:
130+
tmp_path.chmod(0o700)
131+
132+
110133
@pytest.mark.skip(reason="systemd is not installed on every system, so we're not testing this.")
111134
def test_set_loggers_journald(mocker):
112135
logger = logging.getLogger()

0 commit comments

Comments
 (0)