简体   繁体   English

Greengrass V2 生成额外的“stdout”日志

[英]Greengrass V2 generates extra `stdout` logs

I deploy greengrass components into my EC2 instance.我将 greengrass 组件部署到我的 EC2 实例中。 The deploy greengrass components have been generating logs which wraps around my python log.部署 greengrass 组件一直在生成环绕我的 python 日志的日志。

what is causing the "wrapping" around it?是什么导致了它的“环绕”? how can I remove these wraps.我怎样才能去掉这些包裹。

For example, the logs in bold are wraps the original python log.例如,粗体日志是对原始 python 日志的包装。 The log in emphasis is generated by my python log formatter.强调的日志是由我的 python 日志格式化程序生成的。

2022-12-13T23:59:56.926Z [INFO] (Copier) com.bolt-data.iot.RulesEngineCore: stdout. 2022-12-13T23:59:56.926Z [信息](复印机)com.bolt-data.iot.RulesEngineCore:标准输出。 [2022-12-13 23:59:56,925][DEBUG ][iot-ipc] checking redis pub-sub health (io_thread[140047617824320]:pub_sub_redis:_connection_health_nanny_task:61). [2022-12-13 23:59:56,925][DEBUG][iot-ipc] 检查 redis pub-sub 健康状况 (io_thread[140047617824320]:pub_sub_redis:_connection_health_nanny_task:61)。 {scriptName=services.com.bolt-data.iot.RulesEngineCore.lifecycle.Run, serviceName=com.bolt-data.iot.RulesEngineCore, currentState=RUNNING} {scriptName=services.com.bolt-data.iot.RulesEngineCore.lifecycle.Run, serviceName=com.bolt-data.iot.RulesEngineCore, currentState=RUNNING}

The following is my python log formatter.以下是我的 python 日志格式化程序。

    formatter = logging.Formatter(
        fmt="[%(asctime)s][%(levelname)-7s][%(name)s] %(message)s (%(threadName)s[%(thread)d]:%(module)s:%(funcName)s:%(lineno)d)"
    )

    # TODO: when we're running in the lambda function, don't stream to stdout
    _handler = logging.StreamHandler(stream=stdout)
    _handler.setLevel(get_level_from_environment())
    _handler.setFormatter(formatter)

It looks like the logs you are seeing are being generated by different components or processes and are being interleaved or "wrapped" around each other.看起来您看到的日志是由不同的组件或进程生成的,并且相互交错或“包裹”在一起。 This can happen when multiple processes or threads are writing to the same log file or output stream, or when the logs are being collected and processed by a central log management system.当多个进程或线程正在写入同一个日志文件或输出流时,或者当中央日志管理系统正在收集和处理日志时,可能会发生这种情况。

To prevent the wrapping of logs, you may want to consider the following options:为防止日志换行,您可能需要考虑以下选项:

  1. Configure each component or process to write to a separate log file or output stream.配置每个组件或进程以写入单独的日志文件或输出流。 This will allow you to separate the logs and avoid interleaving.这将允许您分离日志并避免交错。
  2. Use log message timestamps to order the logs.使用日志消息时间戳对日志进行排序。 If the logs include timestamps, you can use them to order the logs and determine which log messages were generated first.如果日志包含时间戳,您可以使用它们对日志进行排序并确定首先生成哪些日志消息。 This can help you to understand the order in which events occurred.这可以帮助您了解事件发生的顺序。
  3. Use a log management system that can parse and order the logs.使用可以解析和排序日志的日志管理系统。 There are many log management tools available that can collect and parse logs from multiple sources, including EC2 instances.有许多可用的日志管理工具可以从多个来源(包括 EC2 实例)收集和解析日志。 These tools can often automatically order the logs based on timestamps and other metadata, which can make it easier to understand and analyze the logs.这些工具通常可以根据时间戳和其他元数据自动对日志进行排序,这样可以更轻松地理解和分析日志。

by default Greengrass Nucleus captures the stdoud and stderr streams from the processes it manages, including custom components.默认情况下,Greengrass Nucleus 从它管理的进程中捕获stdoudstderr流,包括自定义组件。 It then outputs each line of the logs with the prefix and suffix you have highlighted in bold .然后它输出日志的每一行,其中包含您以粗体突出显示的前缀和后缀。 This cannot be changed.这是无法更改的。 You can switch the format from TEXT to JSON which can make the log easier to parse by a machine (check greengrass-nucleus-component-configuration - logging.format )您可以将格式从 TEXT 切换为 JSON,这可以使机器更容易解析日志(检查greengrass-nucleus-component-configuration - logging.format

import logging
from logging.handlers import RotatingFileHandler

logger = logging.Logger(__name__)
_handler = RotatingFileHandler("mylog.log")

# additional _handler configuration

logger.addHandler(_handler)

If you want to output a log containing only what your application generates, change the logger configuration output to file.如果您想要 output 日志仅包含您的应用程序生成的内容,请将记录器配置 output 更改为文件。 You can write the file in the work folder of the component or in another location, such as /var/log您可以将文件写入组件的工作文件夹或其他位置,例如/var/log

Cheers, Massimiliano干杯,马西米利亚诺

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM