Let's study Python

Master Python logging with best practices and configuration files for efficient application monitoring.

# Python logging.config.fileConfig 사용법

In Python, logging is a crucial part of application development as it helps in tracking events that occur during the execution of software. By adding logging calls to the code, developers can signify when specific events happen. These events are described with an optional descriptive message that can include variable data, and they also carry a level of importance assigned by the developer, known as the level or severity.

## Best Practices for Python Logging

When it comes to logging in Python, there are several best practices to follow to ensure efficient and effective logging mechanisms. Let’s delve into some of these practices:

1. **Separate Logging Configuration and Source Code**:
– It is recommended to keep logging configuration separate from the source code. This separation allows for easier modification of logging settings without having to alter the source code.

2. **Divide Logs Based on Time Units**:
– To prevent log files from becoming too large, it is advisable to split logs based on time units such as minutes, hours, or days. This division helps in managing disk space efficiently.

3. **Use Vertical Bar ‘|’ as Field Separator**:
– When formatting log messages, it is good practice to use a vertical bar ‘|’ as a field separator instead of commas or other characters that might appear within log messages.

4. **Log Timestamps in ISO 8601 Format**:
– Timestamps in logs should be recorded in the ISO 8601 format, which includes the date, time, and milliseconds with ‘T’ between date and time and ‘Z’ at the end to denote UTC time.

## Creating a Logging Configuration File

To implement the above best practices, a logging configuration file can be created. Below is an example of a `logging.conf` file that adheres to the specified requirements:

“`conf
# logging.conf file

# Logger Definitions
[loggers]
keys=root,log02,log03

# Handler Definitions
[handlers]
keys=handle01,handle02

# Formatter Definitions
[formatters]
keys=form01

# Root Logger Configuration
[logger_root]
handlers=handle01
level=INFO

# Logger log02 Configuration
[logger_log02]
qualname=log02
handlers=handle01
level=NOTSET
propagate=0

# Logger log03 Configuration
[logger_log03]
qualname=log03
handlers=handle01,handle02
level=NOTSET
propagate=0

# Handler Configuration
[handler_handle01]
class=StreamHandler
formatter=form01
level=NOTSET
args=(sys.stdout,)

[handler_handle02]
class=handlers.TimedRotatingFileHandler
formatter=form01
level=NOTSET
args=(‘%(str_log_file_name)s’, ‘M’, 1, 2, ‘utf8’, False, False)

# Formatter Configuration
[formatter_form01]
format=%(asctime)s.%(msecs)03dZ|%(levelname)s|%(funcName)s()|%(message)s
datefmt=%Y-%m-%dT%H:%M:%S
“`

In the provided `logging.conf` file:
– Loggers `root`, `log02`, and `log03` are defined.
– The root logger logs at the `INFO` level, while `log02` and `log03` are set to `NOTSET`.
– `log02` only logs to the console, whereas `log03` logs to both console and a rotating file.
– The rotating file handler is set to rotate every minute with 2 backup files.

## Practical Implementation

To implement the logging configuration defined in the `logging.conf` file, you can use the following Python code snippet:

“`python
import logging
import logging.config

str_log_file_name = “my.log”
logging.config.fileConfig(“logging.conf”, disable_existing_loggers=False, defaults={“str_log_file_name”: str_log_file_name})

logger = logging.getLogger(“log03”)

def function1():
logger.info(‘haha…’)
time.sleep(20)

for i in range(99):
function1()
“`

By running the above code snippet, logs will be output to the console and stored in the specified log file (`my.log`) with the defined formatting and rotation settings.

## Conclusion

In conclusion, proper logging practices in Python are essential for monitoring and debugging applications effectively. By following the recommended guidelines, like separating configuration from source code, dividing logs based on time units, using appropriate field separators, and logging timestamps correctly, developers can maintain clear and organized logs for better application management and troubleshooting. Experimenting with different log levels and configurations can further enhance the logging capabilities in Python.