在AWS Lambda中使用python日志记录 [英] Using python Logging with AWS Lambda

查看:122
本文介绍了在AWS Lambda中使用python日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据AWS文档的建议:

As the AWS documentation suggests:

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def my_logging_handler(event, context):
    logger.info('got event{}'.format(event))
    logger.error('something went wrong')

现在我做了:

import logging
logging.basicConfig(level = logging.INFO)
logging.info("Hello World!")

第一个代码段在Cloud Watch控制台中打印,但是第二个代码行.

The first snippet of code prints in the Cloud Watch console, but the second one no.

我没有看到任何区别,因为这两个代码段都使用了根记录器.

I didn't see any difference as the two snippets are using the root logger.

推荐答案

直接从问题@StevenBohrer的答案链接的顶部答案中复制(这确实帮了我大忙,用我自己的配置替换了最后一行):

Copied straight from the top answer in the question @StevenBohrer's answer links to (this did the trick for me, replacing the last line with my own config):

root = logging.getLogger()
if root.handlers:
    for handler in root.handlers:
        root.removeHandler(handler)
logging.basicConfig(format='%(asctime)s %(message)s',level=logging.DEBUG)

这篇关于在AWS Lambda中使用python日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆