AWS Lambda: Python logging

Log to local Python interpreter when test locally

1
2
3
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger.info('some log)

But this will not log to Cloud Watch when test on the cloud.

Log to Cloud Watch

AWS Lambda Python runtime has pre-configured log handlers. To log to Cloud Watch:

1
2
3
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.info('some log)

But this will not log to local Python interpreter when test locally.

Log to both Cloud Watch and local

1
2
3
4
5
6
# If there is no pre-configured log handler, we set basicConfig
if not logging.getLogger().hasHandlers():
logging.basicConfig(level=logging.INFO)

logger = logging.getLogger()
logger.setLevel(logging.INFO)