Logging in Python

· by

Contents

Python has a nice logging module. You can use it like this:

Stream output

#!/usr/bin/env python

import logging
import sys

logging.basicConfig(
    format="%(asctime)s %(levelname)s %(message)s",
    level=logging.DEBUG,
    stream=sys.stdout,
)
logging.debug("This message should go to the log file")
logging.info("So should this")
logging.warning("And this, too")

File output

#!/usr/bin/env python

import logging

logging.basicConfig(
    filename="logging.log",
    format="%(asctime)s %(levelname)s %(message)s",
    level=logging.DEBUG,
)
logging.debug("This message should go to the log file")
logging.info("So should this")
logging.warning("And this, too")

See also