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

Add --use-colors / --no-use-colors flags. #502

Merged
merged 14 commits into from
Dec 9, 2019
Merged

Conversation

euri10
Copy link
Member

@euri10 euri10 commented Nov 30, 2019

A potential attempt at #497

I wish I could have found a way to add tests but I have no idea as to what to test, I can see if I pass the use_colors flag to False that it's working but that's weak

True
True

False
https://imgur.com/pyHzfpL

log_level=None,
access_log=True,
use_colors=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should default this to None

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -57,12 +57,12 @@
SSL_PROTOCOL_VERSION = getattr(ssl, "PROTOCOL_TLS", ssl.PROTOCOL_SSLv23)


LOGGING_CONFIG = {
LOGGING_CONFIG_COLORS = {
Copy link
Member

@tomchristie tomchristie Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just stick with a single LOGGING_CONFIG.

We can pass an argument "use_colors": None, in the config to the DefaultFormatter, to indicate that it should be autodetermined.

If use_colors=[True|False] is pass to the config, then we should update just that bit in the config dictionary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -215,10 +250,15 @@ def __reduce__(self):
logging.Logger.__reduce__ = __reduce__

if self.log_config is not None:
if not self.use_colors:
self.log_config = LOGGING_CONFIG_NO_COLORS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of switching the entire config here, lets use...

if self.use_colors in (True, False):
    self.log_config["formatters"]["default"]["use_colors"] = self.use_colors

We should only do this within the if isinstance(self.log_config, dict): case. If someone is using the file config then it's up to them to choose the correct settings there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the same logic for access too

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff!

I've got a bit of a requested tweak in how in think we should handle this... let me know if I've not explained something clearly enough here!

@euri10
Copy link
Member Author

euri10 commented Dec 3, 2019

Great stuff!

I've got a bit of a requested tweak in how in think we should handle this... let me know if I've not explained something clearly enough here!

hopefully I got this right ;)

logging.config.dictConfig(self.log_config)
else:
logging.config.fileConfig(self.log_config)
else:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like we need the else: pass block here.

def __init__(self, fmt=None, datefmt=None, style="%"):
self.use_colors = self.should_use_colors()
def __init__(self, fmt=None, datefmt=None, style="%", use_colors=None):
self.use_colors = use_colors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd want to do...

if use_colors in (True, False):
    self.use_colors = use_colours
else:
    self.use_colors = sys.stdout.isatty()

Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, great stuff!
Just a coupla minor comments to address.

@tomchristie
Copy link
Member

Fantastic!
Last bit is the docs...

  • The homepage and deployment pages both include the output of uvicorn --help which will need to be updated.
  • The settings page includes a "Logging" section which is probably where we want to add your flag docs into.

@euri10
Copy link
Member Author

euri10 commented Dec 3, 2019

Fantastic!
Last bit is the docs...

  • The homepage and deployment pages both include the output of uvicorn --help which will need to be updated.
  • The settings page includes a "Logging" section which is probably where we want to add your flag docs into.

yes sorry I forgot the docs, will do that asap

@euri10
Copy link
Member Author

euri10 commented Dec 4, 2019

just one thing, I modified the cli flag too as it was wrong (it was "--use-colors/--use-colors" and I realized it pasting the --help output), I have one question about the default I set to None, but I'm not too sure it's valid in case of a bool

also I'm not sure but think the huge diff in the uvicorn --help output comes from the fact it has not been updated in a long time, and for the index.md it seems previous version had not the 2 spaces before each flag which explains the huge diff

@euri10
Copy link
Member Author

euri10 commented Dec 4, 2019

added some examples of custom logging config for #491
I realized during this docs aren't tested, might be worth it

edit: commit message wanted to link 491 not 409

@tomchristie
Copy link
Member

added some examples of custom logging config

Okay - Let's drop those out of this pull request. We may want a PR for them, but that's a seperate issue, and I'd prefer to review it in isolation to the rest of this work.

@euri10
Copy link
Member Author

euri10 commented Dec 4, 2019

added some examples of custom logging config

Okay - Let's drop those out of this pull request. We may want a PR for them, but that's a seperate issue, and I'd prefer to review it in isolation to the rest of this work.

ok I'm no git pro but I think this might be ok, I did
git branch logging_examples
git reset --keep HEAD~1
git push -f origin

so now colors branch should be in the state it was before adding the examples,

--log-level [critical|error|warning|info|debug|trace]
Log level. [default: info]
--access-log / --no-access-log Enable/Disable access log.
--use-colors / --use-colors Enable/Disable colorized logging.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this should be --use-colors / --no-use-colors

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I was sure I pasted it....now I'm sure

docs/index.md Outdated
--log-level [critical|error|warning|info|debug|trace]
Log level. [default: info]
--access-log / --no-access-log Enable/Disable access log.
--use-colors / --use-colors Enable/Disable colorized logging.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably --use-colors / --no-use-colors, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same...

docs/settings.md Outdated
@@ -30,6 +30,7 @@ equivalent keyword arguments, eg. `uvicorn.run("example:app", port=5000, reload=
* `--log-config <path>` - Logging configuration file.
* `--log-level <str>` - Set the log level. **Options:** *'critical', 'error', 'warning', 'info', 'debug', 'trace'.* **Default:** *'info'*.
* `--no-access-log` - Disable access log only, without changing log level.
* `--use-colors` - Enable colorized formatting of the log records.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this doc around so that:

  • We include both the "on" and "off" flag variants.
  • It's clear that we'll autodetect if the flag isn't used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed, let me know if this is better

@tomchristie
Copy link
Member

"I'm no git pro but I think this might be ok, I did"

Looks okay to me, yup.

As it happens you don't actually need to do those smarts - we have a policy of squashing changes down into a single-commit-per-pull-request on merges, so we don't get any of the messy "getting towards the goal" individual commits, but instead just have a single "here's the PR functionality" commit in the history.

You'd be absolutely fine just making a new commit that removed the work we didn't want in here.

@tomchristie tomchristie changed the title should use colors Add --use-colors / --no-use-colors flags. Dec 9, 2019
@tomchristie tomchristie merged commit be50ca6 into encode:master Dec 9, 2019
@tomchristie
Copy link
Member

Thanks for this!

@euri10 euri10 deleted the colors branch December 9, 2019 13:27
@euri10
Copy link
Member Author

euri10 commented Dec 10, 2019

I think I somewhat f*** up this PR by fogetting to add the new flag in the main() @tomchristie

tthe below patch should fix it I think, not sure how to proceed from here

Index: uvicorn/main.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- uvicorn/main.py	(revision abd4b006c8a443bc80201409187ab3a662ac7c7e)
+++ uvicorn/main.py	(date 1576008880785)
@@ -257,6 +257,7 @@
     ssl_ca_certs: str,
     ssl_ciphers: str,
     headers: typing.List[str],
+    use_colors: bool,
 ):
     sys.path.insert(0, ".")
 
@@ -292,6 +293,7 @@
         "ssl_ca_certs": ssl_ca_certs,
         "ssl_ciphers": ssl_ciphers,
         "headers": list([header.split(":") for header in headers]),
+        "use_colors": use_colors,
     }
     run(**kwargs)

@tomchristie
Copy link
Member

the below patch should fix it I think

Looks right to me too. Wanna issue a PR for that?

@euri10
Copy link
Member Author

euri10 commented Dec 10, 2019 via email

@euri10
Copy link
Member Author

euri10 commented Dec 11, 2019

ok done in #520

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

Successfully merging this pull request may close these issues.

2 participants