How to specify log levels using JSON format in Python

0

Hello, we would like to ask how to configure LOG_LEVEL of messages produced by python components?

We are using Nucleus(=2.5.3) with the following config:

    "logging": {
      "format": "JSON"
    }

Please, consider the following Python script:

    print(f'This is an INFO message.', file=sys.stdout)
    print(f'This is an WARN message.', file=sys.stderr)

This produces the following two messages in the log file:

{"thread":"Copier","level":"INFO","eventType":"stdout","message":"This is an INFO message.","contexts":{"scriptName":"TEST.lifecycle.Run.Script","serviceName":"TEST","currentState":"RUNNING"},"loggerName":"TEST","timestamp":1646058300228,"cause":null}
{"thread":"Copier","level":"WARN","eventType":"stderr","message":"This is an WARN message.","contexts":{"scriptName":"TEST.lifecycle.Run.Script","serviceName":"TEST","currentState":"RUNNING"},"loggerName":"TEST","timestamp":1646058300228,"cause":null}

To our understanding, the field "level" in the JSON object is deduced from the file to which the log message is written:

  • stdout -> INFO

  • stderr -> WARN

We would like to be able to use all of the four different levels described in the GreenGrass Nucleus documentation (DEBUG, INFO, WARNING, ERROR).

What do we need to to to be able to log a message using level 'DEBUG' or 'ERROR' (i.e. the field "level" in the produced message has a value of "DEBUG" or "ERROR")?

Thank you kindly,
Simon

Simon
질문됨 2년 전882회 조회
2개 답변
1

Hi Simon,

As you have determined, writing to stdout is logged at info and stderr is at warn. There is no way to use any different log levels in the Greengrass logger. You can certainly though using Python's builtin logging library to log to a file of your choosing which gives you full control over the log level and format.

AWS
전문가
답변함 2년 전
0

Hello, thank you for the quick reply.

Just to clarify - if we use the Python's logging module to log to the standard output, we can surely specify different logging levels:

    import logging

    logger = logging.getLogger()
    handler = logging.StreamHandler(sys.stdout)
    logger.setLevel(logging.DEBUG)
    logger.addHandler(handler)

    logger.debug('This is a debug')
    logger.info('This is an info')
    logger.warning('This is a warning')
    logger.error('This is an error')

The above code produces the following output. As we can notice, the field 'level' is set to 'INFO' in all of the entries (given that the stream handler is set to standard output). Is this indeed the expected behaviour?

{"thread":"Copier","level":"INFO","eventType":"stdout","message":"This is a debug","contexts":{"scriptName":"services.TEST.lifecycle.Run.Script","serviceName":"TEST","currentState":"RUNNING"},"loggerName":"TEST","timestamp":1646063970436,"cause":null}
{"thread":"Copier","level":"INFO","eventType":"stdout","message":"This is an info","contexts":{"scriptName":"services.TEST.lifecycle.Run.Script","serviceName":"TEST","currentState":"RUNNING"},"loggerName":"TEST","timestamp":1646063970436,"cause":null}
{"thread":"Copier","level":"INFO","eventType":"stdout","message":"This is a warning","contexts":{"scriptName":"services.TEST.lifecycle.Run.Script","serviceName":"TEST","currentState":"RUNNING"},"loggerName":"TEST","timestamp":1646063970436,"cause":null}
{"thread":"Copier","level":"INFO","eventType":"stdout","message":"This is an error","contexts":{"scriptName":"services.TEST.lifecycle.Run.Script","serviceName":"TEST","currentState":"RUNNING"},"loggerName":"TEST","timestamp":1646063970436,"cause":null}
Simon
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠