Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to discard logging? #151

Closed
wsw70 opened this issue Dec 22, 2016 · 10 comments
Closed

How to discard logging? #151

wsw70 opened this issue Dec 22, 2016 · 10 comments

Comments

@wsw70
Copy link

wsw70 commented Dec 22, 2016

Per the docs

waitress.serve calls logging.basicConfig() to set up logging to the console when the server starts up. Assuming no other logging configuration has already been done, this sets the logging default level to logging.WARNING. The Waitress logger will inherit the root logger’s level information (it logs at level WARNING or above).

I tried to discard the loggging from waitress by issuing a logging.getLogger('waitress').setLevel(logging.ERROR) before serve(which is the last call in my script).

This not stop the logging and, worse, duplicates the log messages from my other loggers (which are named loggers, e.g. log = logging.getLogger('myscriptname'), the lines with the pipe delimiter in the example below):

domotique | 2016-12-22 13:58:04,172 | INFO | webserver.py | 263 | calendar | initialized google calendar
INFO:domotique:initialized google calendar
domotique | 2016-12-22 13:58:04,173 | DEBUG | googlecalendar.py | 23 | gettoken | requesting new google token
DEBUG:domotique:requesting new google token

How can i actually discard logging from waitress and especially not having it interacting with my other loggers?

@mmerickel
Copy link
Member

mmerickel commented Dec 22, 2016

You need to setup the logging yourself via logging.basicConfig or logging.config.fileConfig or logging.config.dictConfig prior to invoking waitress.serve. From here you can configure the levels of the loggers, and define the handlers.

As far as why your loggers are receiving duplicated messages you'd need to paste more of your actual logging configuration. basicConfig in waitress.serve is a no-op if the root logger already has a handler on it - and if you aren't setting one yourself via any of the above methods or manually then you aren't setting up logging correctly.

The symptom of duplicate log messages is almost always because you have multiple handlers handling the same messages. You need to figure out what you have done to get 2 handlers (normally you'd just want one per output device).

@wsw70
Copy link
Author

wsw70 commented Dec 22, 2016

@mmerickel I do not use basicConfig, maybe that this is the reason? My logging is set up via the following code, invoked at the very start of my script, before waitress.serve

# disable waitress logging (this line is placed right before waitress.serve)
logging.getLogger('waitress').setLevel(logging.ERROR)

# all of the code below is at the very top of the script
# common logging info
log = logging.getLogger('domotique')
log.setLevel(logging.DEBUG)

# log format
logformat = logging.Formatter("domotique | %(asctime)s | %(levelname)s | %(filename)s | %(lineno)s | %(funcName)s | %(message)s")
syslogHandler = logging.handlers.SysLogHandler(address='/dev/log')
syslogHandler.setFormatter(logformat)
log.addHandler(syslogHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logformat)
log.addHandler(consoleHandler)

@mmerickel
Copy link
Member

mmerickel commented Dec 22, 2016

@wsw70 you are setting a handler on your domotique logger.. This is not the root logger, thus basicConfig is configuring its own on the root logger and you now have 2 handlers. Almost always your handlers should be on the root logger OR you should set propagate=0 on the logger if you don't want its messages propagating up to the root / parent logger's handlers.

loggers are a tree structure and are not independent

@wsw70
Copy link
Author

wsw70 commented Jan 5, 2017

@mmerickel Thank you. With your comments I managed to make a configuration which outputs only what I need - logging is not obvious to get a grip on :)

@wsw70 wsw70 closed this as completed Jan 5, 2017
@evandrocoan
Copy link

Related to #235 - Repeatedly getting this warning 'WARNING:waitress.queue:Task queue depth is 1'

@stevepiercy
Copy link
Member

@evandrocoan https://stackoverflow.com/a/55861495/2214933 or follow the advice in #235

@1zg12
Copy link

1zg12 commented May 18, 2020

I was facing the same issue with a lot of waitress noises and duplicated logging, and couldn't find a solution from any forum. So I am posting the solution here which I have found out and finally working for me. Hope it could be helpful for whoever facing the issue.

You can turn the waitress logging off completely, by passing in the _quiet parameter, like

serve(app, _quiet=True)

#133 (comment)

@digitalresistor
Copy link
Member

That _quiet parameter is a testing shim, and is not public API.

If you don't want Python logging it is a better idea to setup the Python loggers yourself, in which case the call to basicConfig won't actually do anything.

@kemingy
Copy link

kemingy commented Mar 6, 2024

Setting the root logger may collect other unwanted logs. For example, if I use httpx as an HTTP client, the root logger will also collect the httpx client logs.

My solution is to assign a null handler for the root logger:

logging.getLogger().addHandler(logging.NullHandler())

@chuqing1997
Copy link

I feel that when waitress starts, the root logger will be added by default, which will allow the logger we created ourselves to inherit its handler. We can turn off the propagate property of creating loggers ourselves.
Just add this line, no need to do anything else:

log = logging.getLogger('myscriptname')
log.propagate = False

The method in the previous comment
logging.getLogger().addHandler(logging.NullHandler())
will add NullHandler() to the root logger, and the waitress's logger inherits the root logger's Handler, so when you perform these operations, you will never receive waitress's logs again.
These are all my speculations, and different understandings can be exchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants