Complete Guide to Writing to Files Using Python Logging Module

Nov 16, 2025 · Programming · 10 views · 7.8

Keywords: Python logging module | file logging | basicConfig configuration

Abstract: This article provides a comprehensive guide on using Python's built-in logging module to write log information to files. By comparing the basicConfig configuration method and FileHandler approach, it deeply analyzes core concepts including log levels, format settings, and file modes, with complete code examples and best practice recommendations. The content covers the complete workflow from basic configuration to advanced usage, helping developers master professional logging techniques.

Overview of Python Logging Module File Writing

Python's logging module is a crucial tool in the standard library for recording runtime information of applications. Many developers initially find that log messages only output to the console without being written to files. This article systematically explains how to properly configure the logging module for file-based log recording.

Configuring File Logging Using basicConfig Method

logging.basicConfig is a quick configuration method provided by the logging module, particularly suitable for simple logging requirements. Below is a complete file logging configuration example:

import logging

logging.basicConfig(
    filename='application.log',
    filemode='a',
    format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
    datefmt='%H:%M:%S',
    level=logging.DEBUG
)

logging.info("Application started")
logger = logging.getLogger('urbanGUI')

This configuration includes five key parameters, each serving specific functions:

  1. filename parameter: Specifies the output path for the log file. For example, filename='application.log' writes logs to application.log in the current directory.
  2. filemode parameter: Controls the file opening mode. 'a' indicates append mode (default), while 'w' indicates overwrite mode. For long-running applications, append mode is typically used to preserve historical logs.
  3. format parameter: Defines the output format of log messages. The format string uses %(attribute)s syntax, where asctime represents timestamp, msecs represents milliseconds, name represents logger name, levelname represents log level, and message represents log message content.
  4. datefmt parameter: Customizes time display format, following the format specification of time.strftime() function. The example '%H:%M:%S' only displays hours, minutes, and seconds, omitting date information.
  5. level parameter: Sets the minimum level for log recording. logging.DEBUG means recording all DEBUG and higher level messages, including DEBUG, INFO, WARNING, ERROR, and CRITICAL.

Detailed Explanation of Log Levels

Python logging module defines six standard log levels, arranged in increasing order of severity:

The default log level is WARNING, meaning only WARNING and higher level messages are recorded. By setting level=logging.DEBUG, all level log messages can be captured.

Advanced Configuration Using FileHandler

For more complex logging requirements, the FileHandler class provides fine-grained control. This approach offers greater flexibility, especially when multiple log handlers or custom formatting are needed:

import logging

# Create logger
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)

# Create file handler
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)

# Add handler to logger
logger.addHandler(fh)

# Log messages
logger.debug('Debug information')
logger.info('Runtime status information')

This method allows configuring different handlers for different loggers, enabling multi-output of log messages. For example, DEBUG level messages can be written to files while ERROR level messages are sent to email or console.

Log Format Customization

Log format customization is an important feature of the logging module. Through the format parameter, the display content of log messages can be precisely controlled:

import logging

logging.basicConfig(
    filename='custom.log',
    format='%(asctime)s - %(levelname)-8s - %(name)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    level=logging.INFO
)

Commonly used format attributes include:

Best Practices and Considerations

When using the logging module in actual projects, the following best practices should be followed:

  1. Configure Early: Call basicConfig at program start to ensure all log messages are properly processed.
  2. Set Appropriate Log Levels: Use INFO or WARNING level for production environment, DEBUG level for development environment.
  3. Use Module-Level Loggers: Create loggers using logging.getLogger(__name__) in each module for easy log source tracking.
  4. Handle File Encoding: Python 3.9+ supports encoding parameter, explicitly specify encoding to avoid character set issues.
  5. Log File Management: For long-running applications, consider using RotatingFileHandler or TimedRotatingFileHandler for log rotation.

Common Problem Solutions

Developers often encounter the following issues when using the logging module:

By properly configuring the Python logging module, developers can establish comprehensive log recording systems, providing strong support for application monitoring, debugging, and maintenance. Whether for simple scripts or complex distributed systems, good logging practices are key factors in ensuring software quality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.