Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2001-2022 by Vinay Sajip. All Rights Reserved.
# Copyright 2001-2026 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
Expand All @@ -18,7 +18,7 @@
Logging package for Python. Based on PEP 282 and comments thereto in
comp.lang.python.

Copyright (C) 2001-2022 Vinay Sajip. All Rights Reserved.
Copyright (C) 2001-2026 Vinay Sajip. All Rights Reserved.

To use, simply 'import logging' and log away!
"""
Expand Down Expand Up @@ -1257,7 +1257,10 @@ def emit(self, record):
"""
if self.stream is None:
if self.mode != 'w' or not self._closed:
self.stream = self._open()
try:
self.stream = self._open()
except Exception:
self.handleError(record)
if self.stream:
StreamHandler.emit(self, record)

Expand Down
9 changes: 6 additions & 3 deletions Lib/logging/handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2001-2021 by Vinay Sajip. All Rights Reserved.
# Copyright 2001-2026 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
Expand All @@ -18,7 +18,7 @@
Additional handlers for the logging package for Python. The core package is
based on PEP 282 and comments thereto in comp.lang.python.

Copyright (C) 2001-2021 Vinay Sajip. All Rights Reserved.
Copyright (C) 2001-2026 Vinay Sajip. All Rights Reserved.

To use, simply 'import logging.handlers' and log away!
"""
Expand Down Expand Up @@ -540,7 +540,10 @@ def emit(self, record):
If underlying file has changed, reopen the file before emitting the
record to it.
"""
self.reopenIfNeeded()
try:
self.reopenIfNeeded()
except Exception:
self.handleError(record)
logging.FileHandler.emit(self, record)


Expand Down
36 changes: 33 additions & 3 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2001-2022 by Vinay Sajip. All Rights Reserved.
# Copyright 2001-2026 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
Expand All @@ -16,7 +16,7 @@

"""Test harness for the logging module. Run all tests.

Copyright (C) 2001-2022 Vinay Sajip. All Rights Reserved.
Copyright (C) 2001-2026 Vinay Sajip. All Rights Reserved.
"""
import logging
import logging.handlers
Expand Down Expand Up @@ -800,6 +800,37 @@ def lock_holder_thread_fn():

support.wait_process(pid, exitcode=0)

@unittest.skipIf(support.is_wasi or support.MS_WINDOWS, "Platform does not support symlinks.")
def test_135683(self):
# See gh-135683

def tester():
with tempfile.TemporaryDirectory(prefix='test_logging_') 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)
r = logging.makeLogRecord({'msg': 'Test'})
h = logging.handlers.WatchedFileHandler(link_path / 'file.log')
m = Mock()
h.handleError = m
try:
h.handle(r)
m.assert_not_called()
shutil.rmtree(dir_path)
h.handle(r)
m.assert_called()
finally:
h.close()

old_raiseExceptions = logging.raiseExceptions
try:
for test_value in (False, True):
logging.raiseExceptions = test_value
tester()
finally:
logging.raiseExceptions = old_raiseExceptions

class BadStream(object):
def write(self, data):
Expand Down Expand Up @@ -4439,7 +4470,6 @@ def test_queue_listener_with_multiple_handlers(self):

if hasattr(logging.handlers, 'QueueListener'):
import multiprocessing
from unittest.mock import patch

@skip_if_tsan_fork
@threading_helper.requires_working_threading()
Expand Down
Loading