From charlesreid1

(Created page with "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 mo...")
 
Line 31: Line 31:
console.setFormatter(formatter)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
logging.getLogger('').addHandler(console)
logging.info('hello world')
</source>
</source>



Revision as of 17:29, 11 July 2014

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

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.')