Logging
From charlesreid1
I discovered the Python logging module via Stack Overflow: http://stackoverflow.com/questions/9321741/printing-to-screen-and-writing-to-a-file-at-the-same-time
Python logging module can be used to log information at various levels (information, warn, debug, etc.), and log them to various destinations.
Single Destination
Single Destination (Screen) Simple Example
This is a simple script that creates a single logging destination (the screen/console) and logs an information message to the screen:
import logging
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
logging.info('This message goes to the screen')
Single Destination (Screen) With Custom Formatting
The stock date formatting that logging uses is so-so. But you don't have to deal with it, you can redefine it:
import logging
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(message)s',datefmt='%m-%d %H:%M')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
logging.info('hello world')
Single Destination (File) With Custom Formatting
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(message)s',
datefmt='%m-%d %H:%M',
filename=log_file,
filemode='w')
logging.info('Hello file!')
Multiple Destinations
Multiple Destinations (File and Screen) Simple Example
Combining the above, to create a file logger and a screen logger:
import logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(message)s',
datefmt='%m-%d %H:%M',
filename=log_file,
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
logging.info('hello world')
Multiple Destinations (File and Screen) Complicated Example
This one's outta the logging cookbook: https://docs.python.org/2/howto/logging-cookbook.html#logging-to-multiple-destinations
import logging
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='/temp/myapp.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')
# Now, define a couple of other loggers which might represent areas in your
# application:
logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')
Strategies for Using Logging
Here's some info on how I use logging.
Separating Code
I set up all of my logging-related variables in a single file, which I can then import wherever I need to append information to the log. This ensures that everyone has access to the same logs, that all changes are propagated, and that code stays fresh and clean.