Skip to content

Instantly share code, notes, and snippets.

@nmcspadden
Last active November 28, 2017 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmcspadden/1f79f79a61a4fc4393c19edd94ac0fe5 to your computer and use it in GitHub Desktop.
Save nmcspadden/1f79f79a61a4fc4393c19edd94ac0fe5 to your computer and use it in GitHub Desktop.
Testing named vs. unnamed loggers
$ python named_logger.py
WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical
Hi.
$ python unnamed_logger.py
2017-11-28 08:05:40 INFO: Writing verbose debug log to: /var/folders/th/3tqg05g54f73x0k_mstm5wy1_0g_xw/T/mm.XcXk7B.log
2017-11-28 08:05:40 INFO: info
2017-11-28 08:05:40 WARNING: warning
2017-11-28 08:05:40 ERROR: error
2017-11-28 08:05:40 CRITICAL: critical
Hi.
2017-11-28 08:05:40 INFO: Stuff
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
import sys
from tempfile import NamedTemporaryFile
def setup_logging(log_fname_prefix, loglevel):
"""
Set up logging for common usage.
This logger prints out stream content to sys.stdout formatted like this:
2017-11-27 20:23:13 INFO: Message
If the level is set to anything less than ERROR, a debug log file is also
created, and all content (logging.DEBUG and up) is sent there as well.
Args:
log_fname_prefix (str): prefix for temp file name for debug log.
loglevel (int): default level for output. Anything below this level is
not shown. A value below ERROR creates a debug log file.
"""
mylogger = logging.getLogger(log_fname_prefix)
mylogger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(sys.stdout) # defaults to stderr
stream_handler.setLevel(loglevel)
stream_handler.setFormatter(logging.Formatter(
fmt='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
mylogger.addHandler(stream_handler)
if loglevel < logging.ERROR:
log_file = NamedTemporaryFile(prefix=log_fname_prefix + '.',
suffix='.log', delete=False)
file_handler = logging.FileHandler(log_file.name)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
fmt='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
mylogger.addHandler(file_handler)
logging.info("Writing verbose debug log to: " + log_file.name)
return mylogger
logger = setup_logging(
'mm',
logging.INFO
)
logging.debug("debug")
logging.info("info")
logging.warning("warning")
logging.error("error")
logging.critical("critical")
print('Hi.')
logging.info("Stuff")
#!/usr/bin/python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
import sys
from tempfile import NamedTemporaryFile
def setup_logging(log_fname_prefix, loglevel):
"""
Set up logging for common usage.
This logger prints out stream content to sys.stdout formatted like this:
2017-11-27 20:23:13 INFO: Message
If the level is set to anything less than ERROR, a debug log file is also
created, and all content (logging.DEBUG and up) is sent there as well.
Args:
log_fname_prefix (str): prefix for temp file name for debug log.
loglevel (int): default level for output. Anything below this level is
not shown. A value below ERROR creates a debug log file.
"""
mylogger = logging.getLogger()
mylogger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(sys.stdout) # defaults to stderr
stream_handler.setLevel(loglevel)
stream_handler.setFormatter(logging.Formatter(
fmt='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
mylogger.addHandler(stream_handler)
if loglevel < logging.ERROR:
log_file = NamedTemporaryFile(prefix=log_fname_prefix + '.',
suffix='.log', delete=False)
file_handler = logging.FileHandler(log_file.name)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
fmt='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
mylogger.addHandler(file_handler)
logging.info("Writing verbose debug log to: " + log_file.name)
return mylogger
logger = setup_logging(
'mm',
logging.INFO
)
logging.debug("debug")
logging.info("info")
logging.warning("warning")
logging.error("error")
logging.critical("critical")
print('Hi.')
logging.info("Stuff")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment