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
Allow using upper-case levels in ConsoleRenderer #139
Allow using upper-case levels in ConsoleRenderer #139
Conversation
ae8475b
to
7a53dd1
Compare
Codecov Report
@@ Coverage Diff @@
## master #139 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 13 13
Lines 794 802 +8
Branches 102 104 +2
=====================================
+ Hits 794 802 +8
Continue to review full report at Codecov.
|
That codecov report doesn't make any sense to me. Although it does point out that I didn't actually add any tests for this, I'm happy to if they're desired. |
7a53dd1
to
209b925
Compare
I added a test. |
209b925
to
8fec602
Compare
Thanks! This needs an CHANGELOG entry. |
8fec602
to
b52e8dd
Compare
Cool! How's that? |
I'm not sure why the travis build is failing. |
21b1712
to
fdb5bfb
Compare
CHANGELOG.rst
Outdated
@@ -26,7 +26,7 @@ Changes: | |||
|
|||
- Empty strings are valid events now. | |||
`#110 <https://github.com/hynek/structlog/issues/110>`_ | |||
|
|||
- ``structlog.dev.ConsoleRenderer`` now handles log events with all-caps log-levels, in addition to the default lowercase levels generated by ``structlog.stdlib.add_log_level`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please
- add () behind add_log_level (it’s nicer to read if you can identify functions/callables on one blink)
- end the sentence with a period
- add a link to this pr in a separate line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
Eh that seems to be my fault, don’t worry about it. Could you give me a quick reproducer on how to get uppercase log levels tho? I can’t remember to ever having seen them. |
Trying to reproduce, I think I was wrong about this occurring if you just use the standard def add_log_level(_logger, method_name, event_dict):
# type: (str, str, Dict[str, Any]) -> Dict[str, Any]
"""
Add the log level to the event dict.
"""
if method_name == 'warn':
# The stdlib has an alias
method_name = 'warning'
event_dict['level'] = method_name.upper() # <-- the magic bug
return event_dict So you need to actually define your own The reason extending it locally would make me less happy is that the format dict is an implementation detail, so I expect anything I did to modify it in my own code would would be likely to break down the line. Ultimately I'd like to be able to safely specify some colors for the dev renderer. |
fdb5bfb
to
7d26b80
Compare
Yeah that seems very much your problem. :) Why don't we just add a structlog/src/structlog/dev.py Line 174 in 6c77b40
|
Well the issue with putting a >>> from structlog import dev
>>> import timeit
>>> styles = dev._ColorfulStyles
>>> levels = {
... "critical": styles.level_critical,
... "exception": styles.level_exception,
... "error": styles.level_error,
... "warn": styles.level_warn,
... "warning": styles.level_warn,
... "info": styles.level_info,
... "debug": styles.level_debug,
... "notset": styles.level_notset,
... }
>>> levels_with_upper = levels.copy()
>>> for level in list(levels_with_upper.keys()):
... levels_with_upper[level.upper()] = levels_with_upper[level]
>>> timeit.repeat("levels['info']", globals=globals())
[0.04941119905561209, 0.048801488941535354, 0.04878102499060333]
>>> timeit.repeat("levels_with_upper['info']", globals=globals())
[0.04272375698201358, 0.04602321516722441, 0.04284745710901916]
# and now the same thing with a `.lower()` call
>>> timeit.repeat("levels_with_upper['info'.lower()]", globals=globals())
[0.17659165197983384, 0.17641176492907107, 0.17436388204805553] I guess that's not a huge deal for a renderer for in development, but if people are using the I'd rather add a my_styles = ConsoleRenderer.default_level_styles(color=True)
fixup_styles(my_styles)
renderer = ConsoleRenderer(..., level_styles=my_styles) This is flexible enough to allow users to add custom colors (e.g. a |
This works for me in particular because I would like to be able to use capitalized levels, but in general this allows people to override color settings and to add new levels without requiring patching the ConsoleRenderer.
I've added an implementation of my idea so you can see what it looks like. It's more code and adds to the API surface, but hopefully most people wouldn't need to think about it, the default is correct for most cases. |
Sorry it took so long. I’ll rename the method to |
Oh, neat! Thanks! |
Here is a snippet of how-to render the log level in upper case: https://gist.github.com/mauler/a66aaa797c251002f5a8d8d035497259 |
Loggers that come from the standard library will have SCREAMING levels. Using the dev renderer for those causes errors like the following:
I considered adding a more general "set all styles" API, but it seems like this patch should cover 99% of cases.