阅读背景:

使用带有AWS Lambda的python日志记录

来源:互联网 

As the AWS documentation suggests:

正如AWS文档所示:

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')

Now I made:

现在我做了:

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

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

第一段代码在Cloud Watch控制台中打印,但第二段没有。

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

我没有看到任何差异,因为这两个片段正在使用根记录器。

4 个解决方案

#1


10  

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):

直接从问题的顶部答案复制@StevenBohrer的答案链接(这对我来说很有用,用我自己的配置替换最后一行):

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

#2


8  

I had a similar problem, and I suspect that the lambda container is calling logging.basicConfig to add handlers BEFORE the lambda code is imported. This seems like bad form...

我遇到了类似的问题,我怀疑lambda容器在导入lambda代码之前调用logging.basicConfig来添加处理程序。这似乎是糟糕的形式......

Workaround was to see if root logger handlers were configured and if so, remove them, add my formatter and desired log level (using basicConfig), and restore the handlers.

解决方法是查看是否已配置root logger处理程序,如果已配置,请删除它们,添加格式化程序和所需的日志级别(使用basicConfig),然后还原处理程序。

See this article Python logging before you run logging.basicConfig?

在运行logging.basicConfig之前,请参阅此文章Python日志记录?

#3


2  

Probably not referencing the same logger, actually. In the first snippet, log the return of: logging.Logger.manager.loggerDict

实际上可能没有引用相同的记录器。在第一个片段中,记录return:logging.Logger.manager.loggerDict

It will return a dict of the loggers already initialized.

它将返回已初始化的记录器的字典。

Also, from the logging documentation, an important note on logging.basicConfig:

另外,从日志文档中,有关logging.basicConfig的重要说明:

Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger. The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger.

通过使用默认Formatter创建StreamHandler并将其添加到根记录器来为日志记录系统执行基本配置。如果没有为根记录器定义处理程序,函数debug(),info(),warning(),error()和critical()将自动调用basicConfig()。

This function does nothing if the root logger already has handlers configured for it.

如果根记录器已经为其配置了处理程序,则此函数不执行任何操作。

Source: https://docs.python.org/2/library/logging.html#logging.basicConfig

资料来源:https://docs.python.org/2/library/logging.html#logging.basicConfig

#4


0  

Essentially, the AWS logging monkey patch needs to be handled in a very particular way, where:

实质上,AWS日志记录猴子补丁需要以非常特殊的方式处理,其中:

  1. The log level is set from the TOP level of the script (e.g., at import time)
  2. 日志级别是从脚本的TOP级别设置的(例如,在导入时)
  3. The log statements you are interested in are invoked from within the lambda function
  4. 您感兴趣的日志语句是从lambda函数中调用的

Since it's generally considered good form not to run arbitrary code in Python module import, you usually should be able to restructure your code so that the heavy lifting occurs only inside the lambda function.

由于通常认为在Python模块导入中不运行任意代码是好的形式,因此通常应该能够重构代码,以便仅在lambda函数内部发生繁重的操作。


分享到: