In [1]:
from __future__ import print_function
import logging
import logging.config
import logging.handlers
import mimetypes
import gzip, bz2
import yaml
In [2]:
# Logs to stdout|err with a default loglevel.
# Logs are not printed on jupyter cells but you can
# check them in another terminal with
# #docker-compose logs
logging.basicConfig(level=logging.DEBUG)
# Create a logger.
log = logging.getLogger()
# Logs supports a print-like syntax, and doesn't
# build unneeded message strings.
log.info("Use %r instead of %s to avoid", [u"Serialization"], "issues")
In [3]:
%cat logger.yml
In [4]:
with open('logger.yml') as logger_config:
logging.config.dictConfig(yaml.safe_load(logger_config))
# The ```os``` module goes to syslog
log.info("To syslog")
In [5]:
# To process compressed files, use an helper function.
import mimetools
import gzip
import bz2
def log_open(path):
"""Open log files using its mimetype to choose the correct method"""
l_type, l_encoding = mimetypes.guess_type(path)
if l_encoding == 'gzip':
return gzip.open(path, 'rb')
elif l_encoding == 'bzip2':
return bz2.BZ2File(path, 'rb')
else:
return open(path, 'rb')
In [6]:
# Exercise:
# log some messages modifying the default format string.
# check the outcome in a separate terminal with
# docker-compose logs