Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Last active April 4, 2019 13:59
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 tomislacker/d1391e3e4aaafa2cae427207d723f27e to your computer and use it in GitHub Desktop.
Save tomislacker/d1391e3e4aaafa2cae427207d723f27e to your computer and use it in GitHub Desktop.
Python logging Module Not Showing All Levels
  • Inspired by this gist.
  • Supporting documentation
  • I made a new gist because I wanted to have a more complete example that compared what happens without using that solution versus using it.
# Python logger in AWS Lambda has a preset format. To change the format of the logging statement,
# remove the logging handler & add a new handler with the required format
import logging
import sys
def setup_logging(name):
logger = logging.getLogger(name)
for h in logger.handlers:
logger.removeHandler(h)
h = logging.StreamHandler(sys.stdout)
# use whatever format you want here
FORMAT = '%(asctime)s %(message)s'
h.setFormatter(logging.Formatter(FORMAT))
logger.addHandler(h)
logger.setLevel(logging.DEBUG)
return logger
def lambda_handler(event, context):
# Setup global logging config
logging.basicConfig(level=logging.DEBUG)
# Setup initial logger
logger1 = logging.getLogger('logger1')
# Setup separate logger
logger2 = setup_logging('logger2')
# For each logger, emit a message at each log level
# For each log level, for each logger, emit a message
for level in ['critical', 'error', 'warning', 'info', 'debug']:
for l in [logger1, logger2]:
getattr(l, level)(f'Testing level({l.name}): {level}')
print()
START RequestId: 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Version: $LATEST
[CRITICAL] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger1): critical
2019-04-04 13:55:38,553 Testing level(logger2): critical
[CRITICAL] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger2): critical
[ERROR] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger1): error
2019-04-04 13:55:38,553 Testing level(logger2): error
[ERROR] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger2): error
[WARNING] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger1): warning
2019-04-04 13:55:38,553 Testing level(logger2): warning
[WARNING] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger2): warning
2019-04-04 13:55:38,553 Testing level(logger2): info
[INFO] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger2): info
2019-04-04 13:55:38,553 Testing level(logger2): debug
[DEBUG] 2019-04-04T13:55:38.553Z 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Testing level(logger2): debug
END RequestId: 6bf3e28e-f5b8-433c-a70e-a0a98a417f19
REPORT RequestId: 6bf3e28e-f5b8-433c-a70e-a0a98a417f19 Duration: 0.98 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 41 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment