Skip to content

Log File Handlers Do not Respect logging.raiseExceptions = False #135683

Open
@devtomschi

Description

@devtomschi

Bug report

Bug description:

According to the Python docs setting logging.raiseExceptions = False should cause "exceptions get silently ignored" while logging. In the following snippet a WatchedFileHandler is used on a link to a directory, which gets removed at runtime, causing a FileNotFoundException in the last logging command (see traceback for details below).

In my understanding the problem is that while StreamHandler.emit() correctly calls Handler.handleError() on exceptions, the derived handler classes do not handle exceptions the same way. Thus the FileNotFoundError is raised from the WatchedFileHandler implementation as reported below.

IMHO standard handler classes should exhibit behavior consistent with StreamHandler and raise no exceptions while logging if logging.raiseExceptions = False is set.

Reproducer tested with Python 3.13

import logging
import logging.handlers
import pathlib
import shutil
import tempfile

logging.raiseExceptions = False

with tempfile.TemporaryDirectory() as tmp:
    tmp_dir = pathlib.Path(tmp)
    dir_path = tmp_dir / "dir"
    link_path = tmp_dir / "link"
    dir_path.mkdir()
    link_path.symlink_to(dir_path)

    logging.getLogger().addHandler(logging.handlers.WatchedFileHandler(link_path / "file.log"))

    logging.warning("warning msg")

    shutil.rmtree(dir_path)
    logging.warning("warning msg2")

Traceback on Ubuntu 25.04

Traceback (most recent call last):
  File "logging-error.py", line 22, in <module>
    logging.warning("warning msg2")
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 2180, in warning
    root.warning(msg, *args, **kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 1531, in warning
    self._log(WARNING, msg, args, **kwargs)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 1664, in _log
    self.handle(record)
    ~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 1680, in handle
    self.callHandlers(record)
    ~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 1736, in callHandlers
    hdlr.handle(record)
    ~~~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/logging/__init__.py", line 1026, in handle
    self.emit(record)
    ~~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.13/logging/handlers.py", line 539, in emit
    self.reopenIfNeeded()
    ~~~~~~~~~~~~~~~~~~~^^
  File "/usr/lib/python3.13/logging/handlers.py", line 529, in reopenIfNeeded
    self.stream = self._open()
                  ~~~~~~~~~~^^
  File "/usr/lib/python3.13/logging/__init__.py", line 1247, in _open
    return open_func(self.baseFilename, self.mode,
                     encoding=self.encoding, errors=self.errors)
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpm9g_s9xw/link/file.log'

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions