Skip to content

Commit

Permalink
small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
karaposu committed Oct 16, 2024
1 parent ac1385a commit c1d0db5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,9 @@ Contributions are welcome! If you'd like to contribute, please fork the reposito

---

Thank you for using `indented_logger`! We hope it enhances your logging experience and makes debugging and monitoring your applications more efficient.
Thank you for using `indented_logger`! We hope it enhances your logging experience and makes debugging and monitoring your applications more efficient.





20 changes: 20 additions & 0 deletions best_practice_logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,24 @@ Setting up logging correctly in a multi-module application ensures that logs are

---


## 1. Best Practices for Logging in Libraries (PyPI Packages)

When writing a library or package intended for others to use, it's important to follow logging best practices to avoid interfering with the user's application logging configuration.

### **A. Do Not Configure Logging Handlers or Levels in Libraries**

- **Avoid Adding Handlers:** Libraries should not add handlers (like `StreamHandler` or `FileHandler`) to loggers. Adding handlers at the library level can lead to duplicate log messages or unexpected logging behavior in the user's application.

- **Avoid Setting Logger Levels:** Libraries should not set logging levels for loggers (`logger.setLevel`). This should be left to the user's application to configure.

**Your module-level logging configuration should be removed or limited to the `if __name__ == "__main__":` block.**

### **B. Use Module-Level Loggers Without Configuration**

In your library code, you should obtain a logger using `logging.getLogger(__name__)` and use it without adding handlers or setting levels.


## Further Reading and Resources

- **Python Logging Documentation**: [https://docs.python.org/3/library/logging.html](https://docs.python.org/3/library/logging.html)
Expand All @@ -369,3 +387,5 @@ Setting up logging correctly in a multi-module application ensures that logs are





45 changes: 27 additions & 18 deletions indented_logger/formatter.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
# here is formatter.py

import logging
from .indent import get_indent_level

class IndentFormatter(logging.Formatter):
def __init__(self, include_func=False, include_module=False, func_module_format='{funcName}',
def __init__(self, include_func=False, include_module=False, func_module_format=None,
truncate_messages=False, min_func_name_col=80, use_logger_hierarchy=False,
datefmt=None, indent_spaces=4):
self.include_func = include_func
self.include_module = include_module
self.func_module_format = func_module_format
self.truncate_messages = truncate_messages
self.min_func_name_col = min_func_name_col
self.use_logger_hierarchy = use_logger_hierarchy
self.indent_spaces = indent_spaces

# Dynamically build the func_module_format based on include flags
if func_module_format is None:
# Default format based on inclusion flags
placeholders = []
if self.include_module:
placeholders.append('{moduleName}')
if self.include_func:
placeholders.append('{funcName}')
if placeholders:
self.func_module_format = ':'.join(placeholders)
else:
self.func_module_format = ''
else:
# Use the provided func_module_format
self.func_module_format = func_module_format

# Build the format string dynamically
if self.include_func or self.include_module:
fmt = '%(asctime)s - %(levelname)-8s - %(message)s%(padding)s%(func_module_info)s'
if self.func_module_format:
fmt = '%(asctime)s - %(levelname)-8s - %(message)s%(padding)s{%(func_module_info)s}'
else:
fmt = '%(asctime)s - %(levelname)-8s - %(message)s'

Expand Down Expand Up @@ -53,20 +70,13 @@ def format(self, record):
# Build the base log line without func_module_info
base_log = f"{asctime} - {levelname} - {message}"

# Initialize func_module_info
record.func_module_info = ''
if self.include_func or self.include_module:
func_name = record.funcName
module_name = record.name # This is the logger's name, typically the module name

# Build the func_module_info string based on the format provided
# Build the func_module_info string based on the format provided
if self.func_module_format:
func_module_info = self.func_module_format.format(
funcName=func_name,
moduleName=module_name
funcName=record.funcName,
moduleName=record.name # This is the logger's name, typically the module name
)

# Add braces around func_module_info
func_module_info = f"{{{func_module_info}}}"
record.func_module_info = func_module_info

# Calculate padding to align func_module_info at min_func_name_col
desired_column = self.min_func_name_col
Expand All @@ -79,7 +89,6 @@ def format(self, record):
else:
padding = ' '
record.padding = padding
record.func_module_info = func_module_info
else:
record.padding = ''
record.func_module_info = ''
Expand All @@ -95,4 +104,4 @@ def format(self, record):
finally:
# Restore the original message and arguments
record.msg = original_msg
record.args = original_args
record.args = original_args
16 changes: 0 additions & 16 deletions indented_logger/indent.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
# indented_logger/indent.py

# import threading
#
# # Thread-local storage for indentation levels
# _thread_local = threading.local()
#
# def get_indent_level():
# return getattr(_thread_local, 'indent_level', 0)
#
# def increase_indent():
# _thread_local.indent_level = get_indent_level() + 1
#
# def decrease_indent():
# current = get_indent_level()
# _thread_local.indent_level = max(current - 1, 0)
#


import threading

Expand Down
2 changes: 2 additions & 0 deletions indented_logger/logging_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# here is logging_config.py

import logging
from .formatter import IndentFormatter

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='indented_logger', # Package name
version='0.2.1', # Version of your package
version='0.2.2', # Version of your package
author='Enes Kuzucu', # Your name

description='A module to use common logger module with indentation support ', # Short description
Expand Down

0 comments on commit c1d0db5

Please sign in to comment.