Skip to content

Instantly share code, notes, and snippets.

@jtatum
Created April 4, 2013 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtatum/5311955 to your computer and use it in GitHub Desktop.
Save jtatum/5311955 to your computer and use it in GitHub Desktop.
Setup logging so that it works with utf-8 log files and ensure stdout is outputting in utf-8
import logging
# Change the default encoding of stdout to utf-8.
# This is effective if the output is piped or redirected.
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# Add a file handler with utf-8 encoding
handler = logging.FileHandler('output.log', 'w',
encoding = 'utf-8')
root_logger = logging.getLogger()
root_logger.addHandler(handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment