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:

# logging.conf file

# Logger Definitions

# Handler Definitions

# Formatter Definitions

# Root Logger Configuration

# Logger log02 Configuration

# Logger log03 Configuration

# Handler Configuration

args=(‘%(str_log_file_name)s’, ‘M’, 1, 2, ‘utf8’, False, False)

# Formatter Configuration

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:

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():

for i in range(99):

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.