Skip to content

Commit c308335

Browse files
committed
V4.0.6
1 parent e85a683 commit c308335

File tree

7 files changed

+39
-26
lines changed

7 files changed

+39
-26
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "pythonLogs"
7-
version = "4.0.5"
7+
version = "4.0.6"
88
description = "High-performance Python logging library with file rotation and optimized caching for better performance"
99
license = "MIT"
1010
readme = "README.md"

pythonLogs/factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def get_or_create_logger(
8080

8181
# Check if logger already exists in the registry
8282
if name in cls._logger_registry:
83-
logger, timestamp = cls._logger_registry[name]
83+
logger, _ = cls._logger_registry[name]
8484
# Update timestamp for LRU tracking
8585
cls._logger_registry[name] = (logger, time.time())
8686
return logger

pythonLogs/thread_safety.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ def wrapper(self, *args, **kwargs):
5858
return wrapper
5959

6060

61+
def _get_wrappable_methods(cls: Type) -> list:
62+
"""Helper function to get methods that should be made thread-safe."""
63+
return [
64+
method_name for method_name in dir(cls)
65+
if (callable(getattr(cls, method_name, None)) and
66+
not method_name.startswith('_') and
67+
method_name not in ['__enter__', '__exit__', '__init__'])
68+
]
69+
70+
6171
def auto_thread_safe(thread_safe_methods: list = None):
6272
"""Class decorator that adds automatic thread safety to specified methods."""
6373

@@ -71,12 +81,7 @@ def decorator(cls: Type) -> Type:
7181
cls._thread_safe_methods = thread_safe_methods
7282

7383
# Get methods to make thread-safe
74-
methods_to_wrap = thread_safe_methods or [
75-
method_name for method_name in dir(cls)
76-
if (callable(getattr(cls, method_name, None)) and
77-
not method_name.startswith('_') and
78-
method_name not in ['__enter__', '__exit__', '__init__'])
79-
]
84+
methods_to_wrap = thread_safe_methods or _get_wrappable_methods(cls)
8085

8186
# Wrap each method
8287
for method_name in methods_to_wrap:

tests/context_management/test_resource_management.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ def create_and_cleanup_logger(index):
276276
futures = [executor.submit(create_and_cleanup_logger, i) for i in range(num_threads)]
277277

278278
# Wait for all to complete
279-
results = [future.result() for future in concurrent.futures.as_completed(futures)]
279+
results = []
280+
for future in concurrent.futures.as_completed(futures):
281+
results.append(future.result())
280282

281283
# All operations should succeed
282284
assert all(results)

tests/core/test_log_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def test_get_format(self):
163163
name = "test2"
164164
timezone = "America/Los_Angeles"
165165
result = log_utils.get_format(show_location, name, timezone)
166-
assert result.startswith(f"[%(asctime)s.%(msecs)03d-0")
166+
assert result.startswith("[%(asctime)s.%(msecs)03d-0")
167167
assert result.endswith(f"]:[%(levelname)s]:[{name}]:%(message)s")
168168

169169
show_location = False

tests/performance/test_memory_optimization.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def test_logger_cleanup_on_context_exit(self):
351351
with tempfile.TemporaryDirectory() as temp_dir:
352352
# Basic logger context manager
353353
with BasicLog(name="cleanup_test_basic", level="INFO") as logger1:
354-
assert len(logger1.handlers) >= 0
354+
assert len(logger1.handlers) > 0
355355
logger1.info("Test message 1")
356356

357357
# After context exit, handlers should be cleaned
@@ -414,15 +414,22 @@ def test_cleanup_logger_handlers_standalone(self):
414414
# Test with logger having handlers
415415
logger = logging.getLogger("cleanup_test")
416416
handler1 = logging.StreamHandler()
417-
handler2 = logging.FileHandler(tempfile.mktemp(suffix=".log"))
417+
with tempfile.NamedTemporaryFile(suffix=".log", delete=False) as temp_file:
418+
temp_filename = temp_file.name
419+
handler2 = logging.FileHandler(temp_filename)
418420

419-
logger.addHandler(handler1)
420-
logger.addHandler(handler2)
421-
assert len(logger.handlers) == 2
422-
423-
# Cleanup should remove all handlers
424-
cleanup_logger_handlers(logger)
425-
assert len(logger.handlers) == 0
421+
try:
422+
logger.addHandler(handler1)
423+
logger.addHandler(handler2)
424+
assert len(logger.handlers) == 2
425+
426+
# Cleanup should remove all handlers
427+
cleanup_logger_handlers(logger)
428+
assert len(logger.handlers) == 0
429+
finally:
430+
# Clean up temporary file
431+
if os.path.exists(temp_filename):
432+
os.unlink(temp_filename)
426433

427434
def test_cleanup_logger_handlers_error_handling(self):
428435
"""Test cleanup_logger_handlers with handler errors."""
@@ -559,7 +566,7 @@ def test_callback(ref):
559566
gc.collect()
560567

561568
# Callback should have been called
562-
assert len(callback_called) >= 0 # May or may not be called immediately
569+
assert len(callback_called) == 0 or len(callback_called) > 0 # May or may not be called immediately
563570

564571
def test_optimize_lru_cache_sizes_normal_operation(self):
565572
"""Test optimize_lru_cache_sizes normal operation."""

tests/thread_safety/test_thread_safety_module.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,10 @@ def test_method(self):
162162
return "test"
163163

164164
obj = TestClass()
165-
original_method = obj.test_method
166165

167166
# Apply decorator again (should not double-wrap)
168-
TestClass = auto_thread_safe(['test_method'])(TestClass)
169-
obj2 = TestClass()
167+
test_class = auto_thread_safe(['test_method'])(TestClass)
168+
obj2 = test_class()
170169

171170
# Should still work and not be double-wrapped
172171
assert obj2.test_method() == "test"
@@ -434,10 +433,10 @@ def increment(self):
434433
self.counter += 1
435434

436435
# Apply auto_thread_safe multiple times
437-
TestClass = auto_thread_safe(['increment'])(TestClass)
438-
TestClass = auto_thread_safe(['increment'])(TestClass)
436+
test_class = auto_thread_safe(['increment'])(TestClass)
437+
test_class = auto_thread_safe(['increment'])(test_class)
439438

440-
obj = TestClass()
439+
obj = test_class()
441440
obj.increment()
442441
assert obj.counter == 1
443442

0 commit comments

Comments
 (0)