`log-call` is a Python library that provides a decorator to log function calls. It is designed to make debugging and tracking your code execution easier by providing detailed logs of function calls, including their arguments, return values, and any exceptions that might occur.
`log-call` is a Python library that provides a decorator to log function calls. It is designed to make debugging and
tracking your code execution easier by providing detailed logs of function calls, including their arguments, return
values, and any exceptions that might occur.
## Features
## Features
-**Function Call Logging**: The `@log_call` decorator logs the start and end of a function call, including its arguments and result.
-**Function Call Logging**: The `@log_call` decorator logs the start and end of a function call, including its
-**Customizable Logging Level**: You can specify the logging level for each function using the `level` parameter in the decorator.
arguments and result.
-**Argument and Response Logging**: You can control whether to log the function arguments and result by setting `log_args` and `log_response` parameters in the decorator.
-**Customizable Logging Level**: You can specify the logging level for each function using the `level` parameter in the
decorator.
-**Argument and Response Logging**: You can control whether to log the function arguments and result by
setting `log_args` and `log_response` parameters in the decorator.
-**Asynchronous Support**: The decorator works seamlessly with both synchronous and asynchronous functions.
-**Asynchronous Support**: The decorator works seamlessly with both synchronous and asynchronous functions.
-**Exception Logging**: Any exceptions thrown by the decorated function are logged and re-raised.
-**Exception Logging**: Any exceptions thrown by the decorated function are logged and re-raised.
...
@@ -21,7 +26,7 @@ pip install log-call
...
@@ -21,7 +26,7 @@ pip install log-call
Then, import the `log_call` decorator in your Python script:
Then, import the `log_call` decorator in your Python script:
```python
```python
fromlog_call.log_decoratorimportlog_call
frompit.lib.log_call.log_decoratorimportlog_call
```
```
## Usage
## Usage
...
@@ -30,7 +35,7 @@ Decorate your function using `@log_call`:
...
@@ -30,7 +35,7 @@ Decorate your function using `@log_call`:
In the above example, the `my_function` will log its start and end, along with its arguments and result, at the INFO level.
In the above example, the `my_function` will log its start and end, along with its arguments and result, at the INFO
level.
You can customize the logging level, and whether to log arguments and response, for each function individually. If `log_args` and `log_response` are not explicitly set, they default to `True` if the logger is in DEBUG mode. For higher log levels, these values have to be set explicitly.
You can customize the logging level, and whether to log arguments and response, for each function individually.
If `log_args` and `log_response` are not explicitly set, they default to `True` if the logger is in DEBUG mode. For
higher log levels, these values have to be set explicitly.
For asynchronous functions, simply apply the decorator as you would with a synchronous function:
For asynchronous functions, simply apply the decorator as you would with a synchronous function:
In case of an exception, the error will be logged and then re-raised.
In case of an exception, the error will be logged and then re-raised.
## Notes
## Notes
- By default, when the logger is in `DEBUG` mode, the `log_args` and `log_response` parameters are set to `True`. For higher log levels, these values have to be set explicitly.
- The logger is taken from the module where the decorated function is defined. If you want to use a different logger, you can pass it as an argument to the decorator.
- By default, when the logger is in `DEBUG` mode, the `log_args` and `log_response` parameters are set to `True`. For
higher log levels, these values have to be set explicitly.
- The logger is taken from the module where the decorated function is defined. If you want to use a different logger,
you can pass it as an argument to the decorator.
- The `@log_call` decorator can be used with both synchronous and asynchronous functions.
- The `@log_call` decorator can be used with both synchronous and asynchronous functions.
- For performance reasons, the decorator validates if the log level is enabled for the logger. If the log level is not enabled, the decorator will not log the function call.
- For performance reasons, the decorator validates if the log level is enabled for the logger. If the log level is not
enabled, the decorator will not log the function call.
- Logging configuration is not handled by the library. You need to configure the logging module in your application.
- Logging configuration is not handled by the library. You need to configure the logging module in your application.
## Advices
## Advices
- Prefer using log levels like `INFO` or `DEBUG` for function call logging to avoid excessive logging in production environments.
- Prefer set as False the `log_args` and `log_response` parameters for functions that receive or return large amounts of data to avoid performance issues.
- Prefer using log levels like `INFO` or `DEBUG` for function call logging to avoid excessive logging in production
- Take special attention with functions tha receive sensitive data or data that should not be logged, in this case, set as False the `log_args` and `log_response` parameters.
environments.
- Take special attention when functions receive large collections
- Prefer set as False the `log_args` and `log_response` parameters for functions that receive or return large amounts of
- Prefer pass the argument `_logger` to the decorator to have a better control of the log messages generated by the decorator and yours. By this way you can control the log level independently of the log level of the module where the function is defined.
data to avoid performance issues.
- Take special attention with functions tha receive sensitive data or data that should not be logged, in this case, set
as False the `log_args` and `log_response` parameters.
- Take special attention when functions receive large collections
- Prefer pass the argument `_logger` to the decorator to have a better control of the log messages generated by the
decorator and yours. By this way you can control the log level independently of the log level of the module where the