question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add example of logging config file

See original GitHub issue

In the doc, there is the description of --log-config <path> for the configuration of a config file. I think an example of config file could be really awesome for the new comers.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:83
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

15reactions
luca-drfcommented, Apr 6, 2021

Not sure if this is helpful but I had the same issue as @mattiacampana and I solved by defining a new formatter, a new handler and configuring the root logger to use them. Then setting disable_existing_loggers: False allows existing uvicorn loggers to inherit the custom configuration without having to modify them at “runtime”:

version: 1
disable_existing_loggers: False
formatters:
  timestamped:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: INFO
    formatter: timestamped
    stream: ext://sys.stdout
root:
  level: INFO
  handlers: [console]

Another solution I found, in order to use uvicorn formatter classes (and not lose colors, for instance), is to re-define loggers like this:

version: 1
disable_existing_loggers: False
formatters:
  default:
    (): 'uvicorn.logging.DefaultFormatter'
    fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(message)s'
  access:
    (): 'uvicorn.logging.AccessFormatter'
    fmt: '%(asctime)s %(levelprefix)-9s %(name)s -: %(client_addr)s - "%(request_line)s" %(status_code)s'
handlers:
  default:
    class: logging.StreamHandler
    formatter: default
    stream: ext://sys.stderr
  access:
    class: logging.StreamHandler
    formatter: access
    stream: ext://sys.stdout
loggers:
  uvicorn:
    level: INFO
    handlers:
      - default
  uvicorn.error:
    level: INFO
  uvicorn.access:
    level: INFO
    propagate: False
    handlers:
      - access
5reactions
luca-drfcommented, Mar 29, 2021

Not sure if this is helpful but I had the same issue as @mattiacampana and I solved by defining a new formatter, a new handler and configuring the root logger to use them. Then setting disable_existing_loggers: False allows existing uvicorn loggers to inherit the custom configuration without having to modify them at “runtime”:

version: 1
disable_existing_loggers: False
formatters:
  timestamped:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: INFO
    formatter: timestamped
    stream: ext://sys.stdout
root:
  level: INFO
  handlers: [console]
Read more comments on GitHub >

github_iconTop Results From Across the Web

logging.config — Logging configuration — Python 3.11.1 ...
The following functions configure the logging module. They are located in the logging.config module. Their use is optional — you can configure the...
Read more >
file - How to use logging with python's fileConfig and configure ...
Try calling logging.config.dictConfig() after fileConfig() and just setting the filename.
Read more >
Logger From Config File - Real Python
You can configure logging using the module and class functions or by creating a config file or a dictionary and loading it using...
Read more >
logging.config - Simple Guide to Configure Loggers from ...
An in-depth guide to configure loggers from dictionary and config files. Python module "logging.config" provides us with various methods to ...
Read more >
Config Files and Logging - Medium
Why writing the messages in a log file important? Any message outputted in ... An example of a config file is below. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found