This is a code library designed to improve code re-usability and standardize access to APIs, RDS, and logging functionalities
Project description
Wrench Code Library
Description
This is a code library designed to improve code reusability and standardize access to APIs, RDS, and logging functionalities.
PyPI Link: WrenchCL on PyPI
Installation
To install the package, simply run the following command:
pip install WrenchCL
User Guides
Wrench Logger
Instantiation
WrenchCL can be imported using either of the following methods:
from WrenchCL import wrench_logger
# or
from WrenchCL.Utility import wrench_logger
It utilizes a singleton instance. Upon import, the wrench_logger
automatically:
- Generates a run ID.
- Checks the environment it's running on and sets the configuration accordingly.
- Sets the default logging format.
- Sets the logging level as specified.
- Configures the file logger if file logging is enabled.
- Configures the console logger.
Since the class itself is not imported but the instance is, configuration is achieved through methods rather than arguments.
Logging Format Details
Each log message encompasses the following information:
- Log Level: Severity of the log (e.g., INFO, DEBUG).
- Run ID: Unique identifier for the current run, aiding in distinguishing logs from different execution phases.
- File Name: The Python file where the log call occurs.
- Function Name: Function containing the log call.
- Line Number: Exact line number of the log call.
- Timestamp: Date and time when the log message was generated.
- Message: Content of the log message.
Format
[Log Level] : [RunID] FileName:Method:Lineno | DateTime | Message
INFO : [R-2P19UD-6573] WrenchLogger.py:_log:156 | 2024-02-21 01:30:11 | RDS: Getting Entity Data
Main Functional Methods
Logging Methods
info
Method
wrench_logger.info(self, text: str, stack_info: Optional[bool] = False) -> None
-
Usage:
# Input wrench_logger.info("Test info message.") # Output INFO : [R-HH7GH3-9471] WrenchLogger.py:info:192 | 2024-02-22 12:15:04 | Test info message.
-
Color: Green
-
Use Case: For printing information.
context
Method
wrench_logger.context(self, text: str, stack_info: Optional[bool] = False) -> None
-
Usage:
# Input wrench_logger.context("Test context message.") # Output INFO : [R-HH7GH3-9471] WrenchLogger.py:context:195 | 2024-02-22 12:15:04 | Test context message.
-
Color: Purple
-
Use Case: For printing large volumes of context, making it easily distinguishable during testing.
warning
Method
wrench_logger.warning(self, text: str, stack_info: Optional[bool] = False) -> None
-
Usage:
# Input wrench_logger.warning("Test warning message.") # Output WARNING : [R-HH7GH3-9471] WrenchLogger.py:warning:198 | 2024-02-22 12:15:04 | Test warning message.
-
Color: Yellow
-
Use Case: For a handled exception.
error
Method
wrench_logger.error(self, text: str, stack_info: Optional[bool] = False) -> None
-
Usage:
# Input wrench_logger.error("Test error message.") # Output ERROR : [R-HH7GH3-9471] WrenchLogger.py:error:201 | 2024-02-22 12:15:04 | Test error message.
-
Color: Red
-
Use Case: For handled or unhandled errors.
critical
Method
wrench_logger.critical(self, text: str, stack_info: Optional[bool] = False) -> None
-
Usage:
# Input wrench_logger.critical("Test critical message.") # Output CRITICAL: [R-HH7GH3-9471] WrenchLogger.py:critical:204 | 2024-02-22 12:15:04 | Test critical message.
-
Color: Light-red
-
Use Case: For high priority errors that significantly impact outputs or dependencies.
debug
Method
wrench_logger.debug(self, text: str, stack_info: Optional[bool] = False) -> None
-
Usage:
# Input wrench_logger.debug("Test debug message.") # Output DEBUG : [R-HH7GH3-1289] WrenchLogger.py:debug:207 | 2024-02-22 12:30:48 | Test debug message.
-
Color: Light-blue
-
Use Case: For debugging information.
header
Method
wrench_logger.header(self, text: str, size: int = 80, newline: bool = True) -> None
-
Usage:
# Input wrench_logger.header("Test Header") # Output ----------------------------------Test Header-----------------------------------
-
Use Case: To create sections in logs.
-
newline
adds a white line above the header. -
size
determines the string's total length, filled with-
.
Configuration Methods
Level and Configuration methods
Set Level
wrench_logger.setLevel(self, level: str) -> None
- Purpose: Adjusts the logging level to control what severity of logs are emitted.
- Valid levels include
"INFO", "DEBUG", "WARNING", "ERROR", and "CRITICAL".
Example:
wrench_logger.setLevel("DEBUG")
This sets the logging level to DEBUG, ensuring that all debug messages and above are logged.
Revert Logging Level
wrench_logger.revertLoggingLevel(self) -> None
- Purpose: Reverts the logging level to the previous setting. Useful for temporary adjustments of log verbosity.
Example:
# Initial Setting
wrench_logger.setLevel("DEBUG")
# -- non verbose code
wrench_logger.setLevel("INFO") # Switch to Info level
# -- verbose_function
wrench_logger.revertLoggingLevel() # Switch back to last level before info; debug
Overwrite lambda mode
wrench_logger.overwrite_lambda_mode(self, setting: bool) -> None
- Purpose: Overrides the default behavior for color logging in environments like AWS Lambda, where color logging might not be supported.
Example:
wrench_logger.overwrite_lambda_mode(False)
Set Global Traceback
wrench_logger.set_global_traceback(self, setting: bool) -> None
- Purpose: Globally enables or disables the inclusion of traceback information in logs, aiding in debugging without having to enable it per log call.
Example:
wrench_logger.set_global_traceback(True)
Enabling this will append traceback information to all log messages
File Logging Methods
Set log file location
wrench_logger.set_log_file_location(self, path: str, new_append_mode: Optional[str] = None) -> None
- Purpose: Specifies the log file's location and the mode of appending to the log file. This is crucial for managing log file output and retention.
Example:
wrench_logger.set_log_file_location("/var/log/my_app.log")
Sets the log file location to /var/log/my_app.log
, directing all file-based logging output to this file.
Set file logging
wrench_logger.set_file_logging(self, file_logging: bool) -> None
- Purpose: Enables or disables logging to a file. This method provides a straightforward way to toggle file logging according to runtime decisions.
Example:
wrench_logger.set_file_logging(True)
This enables logging to a file, ensuring that logs are not only displayed in the console but also saved for later review.
Release Resources
wrench_logger.release_resources(self) -> None
- Purpose: Ensures that all file handles and other resources are properly closed and released. This is particularly important in long-running applications to avoid resource leaks.
Example:
wrench_logger.release_resources()
Calling this method when the application is shutting down or when logging needs to be reconfigured ensures that resources are cleanly released.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for WrenchCL-1.10.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cea8152b6755be302a3aacbf6f9cd14e50c03cd2977d3d0639ab5d3379ca255 |
|
MD5 | 825f531ec7c7dc0cfd96b45a944082c3 |
|
BLAKE2b-256 | dc64fa261bf98f9b7a8c8f644b47289eddde7ff418446621424ab259ef785cc6 |