A simple(r) logger for Python
Project description
logEZ
Make logging easy in your applications! Use this simple library to easily use logs in any of your applications.
Installation
pip install logEZ
How to use
from logEZ import MyLogger
logger = MyLogger()
More in Sample App
MyLogger
class
The init method of the MyLogger class takes four optional arguments:
log_file_name
logging_level
disable_console_logs
disable_file_logs
The default values are:
log_file_name="logEZ.log",
logging_level="INFO",
disable_console_logs=False,
disable_file_logs=False
log_file_name
: Specify the file name of your log file here.logging_level
: Specify the default logging level of your logs. It can beINFO
,DEBUG
,WARNING
,ERROR
, orCRITICAL
.disable_console_logs
: Disable the console logging of your logs, if set toTrue
.disable_file_logs
: Disable the file logging of your logs, if set toTrue
.
MyLogger
methods
Once you have initialized a MyLogger object, you can use the following methods:
setLoggingLevel(level: str)
: Change the logging level after initializingMyLogger()
. (Refer to logging_level underMyLogger
class for levels.)debug(inString: str)
: Log aDEBUG
level message. Accepts a string input.info(inString: str)
: Log anINFO
level message. Accepts a string input.warning(inString: str)
: Log aWARNING
level message. Accepts a string input.error(inString: str, exc_info: Optional[bool] = False)
: Log anERROR
level message. Accepts a string input. Ifexc_info
is set to True, it appends the complete execution information along with the log string.critical(inString: str, exc_info: Optional[bool] = False)
: Log aCRITICAL
level message. Accepts a string input. Ifexc_info
is set to True, it appends the complete execution information along with the log string.myExcept(inString: str)
: Log an exception message. Accepts a string input.
Using exc_info
to send complete execution information
The exc_info
parameter is an optional argument available in the error
and critical
methods of the MyLogger
class. When set to True
, it appends the complete execution information, including the traceback, along with the log message. This can be particularly useful when debugging errors or critical issues that require detailed information about the context in which they occurred.
Here's an example of how to use the exc_info
parameter with myError and critical
methods:
from logEZ import MyLogger
logger = MyLogger()
def divide(a, b):
try:
result = a / b
except ZeroDivisionError as e:
logger.myError(f"An error occurred while dividing {a} by {b}", exc_info=True)
return None
return result
divide(10, 0)
In this example, we have a divide
function that takes two numbers as arguments and attempts to perform a division operation. If a ZeroDivisionError
exception is raised, the error
method is called with the exc_info
parameter set to True
. This will log the error message along with the complete execution information, including the traceback, making it easier to understand the context in which the error occurred.
The output of this code will be:
01-04-23 14:35:28 - root : ERROR : An error occurred while dividing 10 by 0
Traceback (most recent call last):
File "example.py", line 7, in divide
result = a / b
ZeroDivisionError: division by zero
Similarly, you can use the exc_info
parameter with the critical
method if you want to log critical messages with complete execution information.
Why use logEZ?
The logEZ library provides a simplified interface to work with the standard logging module in Python. While it does not introduce any new functionality or significant improvements over the built-in logging module, it may be helpful for developers who want a more straightforward, easy-to-use API for common logging tasks.
Advantages of logEZ:
- Simplified initialization: The logEZ library makes it easier to set up logging with default values and simple customization options. You can easily configure log file names, logging levels, and toggle console or file logging with just a few parameters.
- Unified logging methods: The logEZ library offers a single class, MyLogger, with methods for different logging levels (debug, info, warning, error, critical), making it more convenient to use compared to the standard logging module, which requires calling separate functions for each logging level.
- Easier traceback logging: The logEZ library provides the exc_info parameter for error and critical level logs, making it more straightforward to include traceback information when logging exceptions.
However, for more advanced logging use cases or if you require fine-grained control over your logging configuration, the built-in logging module offers more comprehensive features and flexibility. If you are already familiar with the standard logging module, you may not find logEZ to be significantly more helpful.
In summary, the logEZ library can be useful for developers looking for a simpler and more accessible interface for basic logging tasks. However, it might not offer substantial advantages over the built-in logging module for more experienced users or advanced logging scenarios.
What's next?
We have some exciting plans for the future development of logEZ, and we're always looking for ways to make it even more useful for our users. Here's a list of features we're considering adding:
- Configuration from file support
- Log rotation functionality
- Support for custom log handlers
- Colored console logs
- Enhanced context information in log messages
- Improved exception handling and logging
- Integrated performance metrics
- Advanced filtering capabilities
- Asynchronous logging support
- Better documentation and examples
We're open to contributions! If you'd like to help implement any of these features or have suggestions for improvements, please feel free to reach out to us. You can contact us at gehlotkunal@outlook.com or connect with us on Twitter at @ZackCodesAI.
We're looking forward to collaborating with the community to make logEZ an even more valuable and versatile logging library for Python developers!
Change Log
Unreleased
[0.1.0]
Updated
- Refactored the code to adhere to PEP8 standards for better readability and maintainability.
- Added type hints to function signatures for improved code comprehension and editor support.
- Improved exception handling with more specific exceptions and error messages.
- Modularized the code into separate files for better organization and extensibility:
mylogger.py
: Contains the mainMyLogger
class.handlers.py
: Contains handlers for logging to files and consoles.formatters.py
: Contains the log formatter used for log messages.
- Updated the
__init__.py
file to import necessary components for easier library usage. - Enhanced the library with docstrings for better documentation and understanding.
- Updated the
README.md
file to provide clearer instructions on how to use the library, including examples and explanations of the new features. - Improved the
setup.py
file to adhere to the latest Python norms and standards. - Added
nox
as a development dependency to simplify the development workflow.
[0.0.3] - 2022-05-08
Updated
- Updated code with method name fix and logging level method.
- Added "How to use" with complete method definition in README.md
[0.0.2] - 2022-05-08
Fixed
- Fixing project directory name
[0.0.1] - 2022-05-08
Added
- First Release
Removed
- Removed
requirements.txt
- Removed
logging
frominstall_requires
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.