Skip to content

Commit 5ccbf0f

Browse files
CLI crashes with 'Operation not permitted' when log file is not writable (#3538)
* CLI crashes with 'Operation not permitted' when log file is not writable #3537 * Handling the exception properly; plus improved the error message
1 parent 93bb51c commit 5ccbf0f

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/dstack/_internal/cli/utils/common.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,25 @@ def configure_logging():
7777
dstack_logger = logging.getLogger("dstack")
7878
dstack_logger.handlers.clear()
7979

80-
log_file = _get_cli_log_file()
81-
8280
stdout_handler = DstackRichHandler(console=console)
8381
stdout_handler.setFormatter(logging.Formatter(fmt="%(message)s", datefmt="[%X]"))
8482
stdout_handler.setLevel(settings.CLI_LOG_LEVEL)
8583
dstack_logger.addHandler(stdout_handler)
8684

87-
file_handler = logging.FileHandler(log_file)
88-
file_handler.setFormatter(
89-
logging.Formatter(
90-
fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
85+
log_file = get_dstack_dir() / "logs" / "cli" / "latest.log"
86+
try:
87+
log_file = _get_cli_log_file()
88+
file_handler = logging.FileHandler(log_file)
89+
file_handler.setFormatter(
90+
logging.Formatter(
91+
fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
92+
datefmt="%Y-%m-%d %H:%M:%S",
93+
)
9194
)
92-
)
93-
file_handler.setLevel(settings.CLI_FILE_LOG_LEVEL)
94-
dstack_logger.addHandler(file_handler)
95+
file_handler.setLevel(settings.CLI_FILE_LOG_LEVEL)
96+
dstack_logger.addHandler(file_handler)
97+
except PermissionError:
98+
console.print(f"[warning]Couldn't write to {log_file} due to a permissions problem.[/]")
9599

96100
# the logger allows all messages, filtering is done by the handlers
97101
dstack_logger.setLevel(logging.DEBUG)

0 commit comments

Comments
 (0)