Debug Logging (Maybe just a n00b issue)
See original GitHub issueDisclaimer: n00b to Python altogether. This is the first Python app that I’ve ever built.
Trying to add formatted debug logging to the screen. I’ve gotten it to the point where the debug output shows twice. Once with the formatting applied, and once raw.
Scaled down code would be:
import logging
from fastapi import FastAPI
api = FastAPI()
logger = logging.getLogger("api")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("QED.log")
fh.setLevel(logging.ERROR)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
@api.get("/")
def read_root():
logger.debug("THIS IS A DEBUG LOG")
logger.error("SOMETHING WENT VERY VERY WRONG")
return {"Hello": "World"}
Output to the console is:
2019-06-04 12:44:51,169 - api - DEBUG - THIS IS A DEBUG LOG
DEBUG: THIS IS A DEBUG LOG
2019-06-04 12:44:51,169 - api - ERROR - SOMETHING WENT VERY VERY WRONG
ERROR: SOMETHING WENT VERY VERY WRONG
INFO: ('127.0.0.1', 55229) - "GET / HTTP/1.1" 200
So, two things are happening.
- Seeing both the formatted message and the raw message.
- Seeing the ERROR level message in the console. That should only be written to QED.log
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:22 (8 by maintainers)
Top Results From Across the Web
Ultra n00b question - Can't see Debug.Log output! - Unity Forum
But i can't see the Debug output at all. Is it supposed to appear in the same place? I've tried bringing up the...
Read more >How to debug logrotate warnings or errors when logrotate is ...
Check the logrotate.status file · Run logrotate in a debug mode · Identify a configuration file causing the issue · Re-run logrotate in...
Read more >N00b: E on bro problem - Logstash - Discuss the Elastic Stack
I can't see the GeoIP filter in your configuration. But it is clear that bro0.dfw.rg.net does not resolve to an IP that the...
Read more >The Problem With Logging - Coding Horror
Logging means more code. The more you log, the larger your code grows. This is a serious problem, because code is the enemy....
Read more >SLF4J: SimpleLogger is not logging trace and debug and also ...
and it logs fine. This is not maybe the best solution, but the one I've found fitting my needs. You can log DEBUG...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @charliegriefer
You don’t need to set handlers for logging in your FastAPI app, the code can just be:
Then you set the logging level when starting uvicorn, like:
uvicorn qed.api:api --reload --log-level debug
When you run your application in a server, if it is Linux with systemd, it’ll take care of saving the log to the journal and syslog, as well as timestamping it, then you can redirect to another file if you want. But you don’t need to worry about that when developing, just at deployment time.
ok so basically I’m using this, in a applog package
you use it wherever your entrypint is this way
should you need, you can pass env another file than the default logging.yml for your dev / prod environements
logging.yml looks like this: