Greengrass V2 generates extra `stdout` logs

Question:

I deploy greengrass components into my EC2 instance. The deploy greengrass components have been generating logs which wraps around my python log.

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.
The log in emphasis is generated by my python log formatter.

2022-12-13T23:59:56.926Z [INFO] (Copier) com.bolt-data.iot.RulesEngineCore: stdout. [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).
{scriptName=services.com.bolt-data.iot.RulesEngineCore.lifecycle.Run, serviceName=com.bolt-data.iot.RulesEngineCore, currentState=RUNNING}

The following is my python log formatter.

    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)
Asked By: user-517752

||

Answers:

by default Greengrass Nucleus captures the stdoud and stderr streams from the processes it manages, including custom components. 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-configurationlogging.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. You can write the file in the work folder of the component or in another location, such as /var/log