Skip to content

Python print to log file

  • by

To print output to a log file in Python, you can use the logging module, which is a built-in module specifically designed for this purpose. Here’s how you can use it to print messages to a log file:

import logging

# Configure the logger
logging.basicConfig(filename='mylog.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Log some messages
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

In the code above:

  1. We import the logging module.
  2. We configure the logger using basicConfig. In this example, we specify the log file name as 'mylog.log', set the logging level to DEBUG (you can adjust this level based on your needs), and define a log message format.
  3. We log messages at different levels using logging.debug(), logging.info(), logging.warning(), logging.error(), and logging.critical().

Make sure to replace 'mylog.log' with the actual path and filename where you want to store your log messages.

Once you run this code, log messages will be appended to the specified log file, and you can review them later.

Python print-to-log file example

Here’s an example of how to print messages to a log file in Python using the logging module:

import logging

# Configure the logger
logging.basicConfig(filename='example.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Log some messages
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

# Close the log file (optional)
logging.shutdown()

Output:

Python print to log file

In this example:

  1. We import the logging module.
  2. We configure the logger using basicConfig. We specify the log file name as 'example.log', set the logging level to INFO, which means it will log messages at the INFO level and above, and define a log message format that includes a timestamp, log level, and the message itself.
  3. We log messages at different levels using logging.debug(), logging.info(), logging.warning(), logging.error(), and logging.critical().
  4. Optionally, you can close the log file using logging.shutdown() when you’re done with logging. It’s good practice to close the log file explicitly, but Python will also close it when the program exits.

When you run this code, it will create or append to the example.log file in the same directory as your Python script. The log messages will be formatted as specified and written to the log file.

You can change the log level and log file name as needed for your specific use case.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading