Reputation: 183
I need to run a Python web server, while still taking inputs from the user, but when someone visits the website, the information logged to the terminal window is on the same line as the input, making it look like this:
I've tried setting the thread the the servers on to daemon mode, I've tried logging.getLogger("socketserver").setLevel(logging.CRITICAL)
, I've tried logger.propagation = False
, and logger.disable = True
This is my server code:
os.chdir("docs")
port = 8980
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", port), Handler) as httpd:
httpd.serve_forever()
Upvotes: 2
Views: 3052
Reputation: 36
I wasn't quite clear if you wanted to remove the logging message on the command line, or redirect logging to file.
If you're wanting to redirect to file, you can use os.write to print to file.
If you're wanting to turn off the logging messages, and you can't get them off the command line, you could assign that logging to a nullHandler. Something a bit like:
noop = logging.NullHandler()
log.addHandler(noop)
# nothing happens here
log.info('message that will not be logged')
Using a nullHandler is a bit of a large solution, as the ideal would be to have logging going to a .log file for reference, not turning it off or skipping for certain messages.
Upvotes: 0
Reputation: 11157
Subclass SimpleHTTPRequestHandler
and override the log_message
method to do nothing:
class QuietHandler(http.server.SimpleHTTPRequestHandler):
def log_message(self, format, *args):
pass
with socketserver.TCPServer(("", 8980), QuietHandler) as httpd:
httpd.serve_forever()
Upvotes: 7
Reputation: 23
One thing you can try is piping the output into a file, if the file size ever becomes a concern I would try piping it to nul, like: python3 myserver.py 2> nul
Upvotes: 1