pythonconsolefastapigoogle-cloud-logginglog-level

Log Severity not correct in google cloud console


I have a FastApi application which is running on Google Kubernetes engine. Initially, Uvicorn startup logs were being displayed with Severity ERROR in the google console. I added the following logging config file to the uvicorn.run to solve it. Post adding the logging config file to Uvicorn, this issue was solved and I am facing another issue that Errors and Warning are also showing as Severity INFO in google cloud console.

Before adding uvicorn_log.config : enter image description here

To solve the above issue, I added following logging config file to uvicorn.run:

uvicorn.run(app, host="0.0.0.0", port=5021, log_config='uvicorn_log.config')

config file content:

[loggers]
keys=root

[handlers]
keys=console

[formatters]
keys=generic

[logger_root]
level=NOTSET

handlers=console


[handler_console]
class=StreamHandler
formatter=generic
args=(sys.stdout,)

[formatter_generic]
format= %(message)s

In my logger module I am using google.cloud.logging library as below:

client = google.cloud.logging.Client()
client.setup_logging()
logging.error("This appears with severity INFO in google console")

I am not able to find what in the config file is overriding the severity level of python logger. Any suggestion and idea is much appreciated


Solution

  • I was able to solve this issue by using the following configuration file. Essentially, my solution involved redirecting uvicorn logs to standard output(to prevent them appearing as error in GCP console) while leaving the root logger configuration intact.

    version: 1
    disable_existing_loggers: False
    formatters:
      default:
        format: '%(message)s'
    handlers:
      default:
        formatter: default
        class: logging.StreamHandler
        stream: ext://sys.stdout
    
    loggers:
      uvicorn.error:
        level: INFO
        handlers:
          - default
        propagate: no
      uvicorn.access:
        level: INFO
        handlers:
          - default
        propagate: no