简体   繁体   English

没有使用 watchtower 将正确的日志记录(python)格式发送到 Cloudwatch

[英]Correct logging(python) format is not being sent to Cloudwatch using watchtower

I have written following code to enable Cloudwatch support.我编写了以下代码来启用 Cloudwatch 支持。

import logging
from boto3.session import Session
from watchtower import CloudWatchLogHandler

logging.basicConfig(level=logging.INFO,format='[%(asctime)s.%(msecs).03d] [%(name)s,%(funcName)s:%(lineno)s] [%(levelname)s]  %(message)s',datefmt='%d/%b/%Y %H:%M:%S')
log = logging.getLogger('Test')

boto3_session = Session(aws_access_key_id=AWS_ACCESS_KEY_ID,
                aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                region_name=REGION_NAME)

cw_handler = CloudWatchLogHandler(log_group=CLOUDWATCH_LOG_GROUP_NAME,stream_name=CLOUDWATCH_LOG_STREAM_NAME,boto3_session=boto3_session)
log.addHandler(cw_handler)

Whenever i try to print any logger statement, i am getting different output on my local system and cloudwatch.每当我尝试打印任何记录器语句时,我都会在本地系统和 cloudwatch 上得到不同的输出。

Example:例子:

log.info("Hello world")

Output of above logger statement on my local system (terminal):上述记录器语句在我的本地系统(终端)上的输出:

[24/Feb/2019 15:25:06.969] [Test,<module>:1] [INFO]  Hello world

Output of above logger statement on cloudwatch (log stream): cloudwatch 上的上述记录器语句的输出(日志流):

Hello world

Is there something i am missing?有什么我想念的吗?

In the Lambda execution environment, the root logger is already preconfigured.在 Lambda 执行环境中,已经预先配置了根记录器。 You'll have to work with it or work around it.您必须使用它或解决它。 You could do some of the following:您可以执行以下一些操作:

You can set the formatting directly on the root logger:您可以直接在根记录器上设置格式:

root = logging.getLogger()
root.setLevel(logging.INFO)
root.handlers[0].setFormatter(logging.Formatter(fmt='[%(asctime)s.%(msecs).03d] [%(name)s,%(funcName)s:%(lineno)s] [%(levelname)s]  %(message)s', datefmt='%d/%b/%Y %H:%M:%S'))

You could add the Watchtower handler to it (disclaimer: I have not tried this approach):您可以向其中添加 Watchtower 处理程序(免责声明:我没有尝试过这种方法):

root = logging.getLogger()
root.addHandler(cw_handler)

However I'm wondering if you even need to use Watchtower.但是,我想知道您是否甚至需要使用 Watchtower。 In Lambda, every line you print to stdout (so even just using print ) get logged to Cloudwatch.在 Lambda 中,您打印到stdout每一行(因此即使只是使用print )都会记录到 Cloudwatch。 So using the standard logging interface might be sufficient.因此,使用标准logging界面可能就足够了。

This worked for me这对我有用

import logging
import watchtower
watch = watchtower.CloudWatchLogHandler()
watch.setFormatter(fmt = logging.Formatter('%(levelname)s - %(module)s - %(message)s'))
logger = logging.getLogger()
logger.addHandler(watch)

As per comment in https://stackoverflow.com/a/45624044/1021819 (and another answer there),根据https://stackoverflow.com/a/45624044/1021819中的评论(以及那里的另一个答案),

just add force=True to logging.basicConfig() , so in your case you need只需将force=True添加到logging.basicConfig() ,因此在您的情况下您需要

logging.basicConfig(level=logging.INFO, force=True, format='[%(asctime)s.%(msecs).03d] [%(name)s,%(funcName)s:%(lineno)s] [%(levelname)s]  %(message)s',datefmt='%d/%b/%Y %H:%M:%S')
log = logging.getLogger('Test')

This function does nothing if the root logger already has handlers configured, unless the keyword argument force is set to True.如果根记录器已经配置了处理程序,则此函数不执行任何操作,除非关键字参数 force 设置为 True。

(ie AWS case) (即AWS案例)

Re: force:回复:力量:

If this keyword argument is specified as true, any existing handlers attached to the root logger are removed and closed, before carrying out the configuration as specified by the other arguments.如果此关键字参数指定为 true,则在执行其他参数指定的配置之前,将删除并关闭附加到根记录器的任何现有处理程序。

REF: https://docs.python.org/3/library/logging.html#logging.basicConfig参考: https ://docs.python.org/3/library/logging.html#logging.basicConfig

THANKS:谢谢:

https://stackoverflow.com/a/72054516/1021819 https://stackoverflow.com/a/72054516/1021819

https://stackoverflow.com/a/45624044/1021819 https://stackoverflow.com/a/45624044/1021819

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS CloudWatch 的正确格式 - Correct format for AWS CloudWatch Python lambda 多行日志格式 output 到 cloudwatch - Python lambda log format with multline output to cloudwatch 当以 JSON 格式记录到 CloudWatch 日志时,时间戳属性的名称是什么?预期格式是什么? - When logging to CloudWatch logs in a JSON format what is the name of the timestamp property and what is the expected format? Cloudwatch Logs数据发送到kinesis data stream时,它的编码格式是什么 - When Cloudwatch Logs data is sent into kinesis data stream, what is its encoding format 为什么我的 AWS CloudWatch 警报没有被触发? - Why is my AWS CloudWatch alarm not being triggered? 如何配置 Serilog 接收器以记录到 CloudWatch - How to configure a Serilog sink for logging to CloudWatch 如何更改 cloudwatch 代理日志区域? - How to change the cloudwatch agent logging region? 如何在 Python 中使用 Boto3 AWS Lambda 获取 aws cloudwatch 指标统计信息? - How to get aws cloudwatch metrics statistics using Boto3 AWS Lambda in Python? python中如何使用boto3查询cloudwatch日志 - How to query cloudwatch logs using boto3 in python 重新创建/重新附加 AWS Lambda 控制台日志记录到 CloudWatch - Recreating/reattaching AWS Lambda console logging to CloudWatch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM