-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Fails to color Windows console #1832
Comments
I'm not really sure how to explain this. If anything, is an issue with Click, but Click isn't supposed to emit color info on Windows if colorama isn't installed. I have seen more reports of this since switching to Click, but it's not consistent, as shown in the previous screenshots. |
I was able to correct the issue by adding the following code to my flask app import os
import sys
if sys.platform.lower() == "win32":
os.system('color') See https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-python for details. It seems that the double brackets are being filtered in the logs as @mrx23dot suggested. |
I spent some more time investigating this issue. The color addition comes from def log_request(self, code="-", size="-") -> None:
try:
path = uri_to_iri(self.path)
msg = f"{self.command} {path} {self.request_version}"
except AttributeError:
# path isn't set if the requestline was bad
msg = self.requestline
code = str(code)
if click:
color = click.style
if code[0] == "1": # 1xx - Informational
msg = color(msg, bold=True)
elif code[0] == "2": # 2xx - Success
msg = color(msg, fg="white")
elif code == "304": # 304 - Resource Not Modified
msg = color(msg, fg="cyan")
elif code[0] == "3": # 3xx - Redirection
msg = color(msg, fg="green")
elif code == "404": # 404 - Resource Not Found
msg = color(msg, fg="yellow")
elif code[0] == "4": # 4xx - Client Error
msg = color(msg, fg="red", bold=True)
else: # 5xx, or any other response
msg = color(msg, fg="magenta", bold=True)
self.log("info", '"%s" %s %s', msg, code, size) You can see that the import sys
import os
if sys.platform.lower() == 'win32':
os.system('color') This will address most users with the issue, but might not fix some edge cases (e.g. small-footprint or embedded unix distros). The So the way I see it there are 2 questions:
|
Thanks. add the code in config.py , it's works for me. 😀 |
I looked into the When I switched to Click for styling, I thought it would handle not setting styles on Windows, but it turns out Since the |
On windows the new Flask prints weird characters on console, instead of coloring the line as on linux. eg: "←[37m
I would rather go black/white (like in previous version) if coloring doesnt work.
2020-05-03 14:05:47,398 - INFO - 127.0.0.1 - - [03/May/2020 14:05:47] "←[37mGET /result HTTP/1.1←[0m" 200 -
2020-05-03 14:06:42,922 - INFO - 127.0.0.1 - - [03/May/2020 14:06:42] "←[37mGET /result HTTP/1.1←[0m" 200 -
2020-05-03 14:07:24,007 - INFO - 127.0.0.1 - - [03/May/2020 14:07:24] "←[37mGET /result HTTP/1.1←[0m" 200 -
2020-05-03 14:07:24,210 - INFO - 127.0.0.1 - - [03/May/2020 14:07:24] "←[37mGET /static/images/favicon.ico HTT
2020-05-03 14:08:19,539 - INFO - 127.0.0.1 - - [03/May/2020 14:08:19] "←[37mGET /result HTTP/1.1←[0m" 200 -
2020-05-03 14:09:15,065 - INFO - 127.0.0.1 - - [03/May/2020 14:09:15] "←[37mGET /result HTTP/1.1←[0m" 200 -
2020-05-03 14:10:10,579 - INFO - 127.0.0.1 - - [03/May/2020 14:10:10] "←[37mGET /result HTTP/1.1←[0m" 200 -
Env:
Flask==1.1.2
Win7 x64
Python 3.6.7 x64
Werkzeug==1.0.1
The text was updated successfully, but these errors were encountered: