Skip to content

Commit 8e1f921

Browse files
committed
try all suffix lengths
1 parent 35e847f commit 8e1f921

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

can/io/logger.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,19 @@ def _get_new_writer(self, filename: StringPathLike) -> FileIOMessageWriter:
259259
:return:
260260
An instance of a writer class.
261261
"""
262-
suffix = "".join(pathlib.Path(filename).suffixes[-2:]).lower()
263-
264-
if suffix in self._supported_formats:
262+
suffixes = pathlib.Path(filename).suffixes
263+
for suffix_length in range(len(suffixes), 0, -1):
264+
suffix = "".join(suffixes[-suffix_length:]).lower()
265+
if suffix not in self._supported_formats:
266+
continue
265267
logger = Logger(filename=filename, **self.writer_kwargs)
266268
if isinstance(logger, FileIOMessageWriter):
267269
return logger
268270
elif isinstance(logger, Printer) and logger.file is not None:
269271
return cast(FileIOMessageWriter, logger)
270272

271273
raise ValueError(
272-
f'The log format "{suffix}" '
274+
f'The log format of "{pathlib.Path(filename).name}" '
273275
f"is not supported by {self.__class__.__name__}. "
274276
f"{self.__class__.__name__} supports the following formats: "
275277
f"{', '.join(self._supported_formats)}"

test/test_rotating_loggers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ def test_on_message_received(self, tmp_path):
180180
do_rollover.assert_called()
181181
writers_on_message_received.assert_called_with(msg)
182182

183+
def test_issue_1792(self, tmp_path):
184+
with self._get_instance(tmp_path / "__unused.log") as logger_instance:
185+
writer = logger_instance._get_new_writer(
186+
tmp_path / "2017_Jeep_Grand_Cherokee_3.6L_V6.log"
187+
)
188+
assert isinstance(writer, can.CanutilsLogWriter)
189+
writer.stop()
190+
183191

184192
class TestSizedRotatingLogger:
185193
def test_import(self):

0 commit comments

Comments
 (0)