• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Logging in Python

Contents

  • Stream output
  • File output
  • See also

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

  • Tutorial
  • Documentation

Published

Aug 1, 2014
by Martin Thoma

Category

Code

Tags

  • Logging 2
  • Python 141

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor