seatgeek / fuzzywuzzy

Fuzzy String Matching in Python
http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/
GNU General Public License v2.0
9.21k stars 874 forks source link

don't attach to root logger, attach to scoped logger instead #268

Closed arahayrabedian closed 2 years ago

arahayrabedian commented 4 years ago

Allows inheriting projects to conveniently control the log level of fuzzywuzzy's logging by attaching to it's own package-spaced logger rather than the root logger - for example our project wants to set fuzzywuzzy's logging level to ERROR without setting the entire root logger to ERROR.

Evidence that this works:

before the changes (your master):

In [1]: from logging_tree import printout                                                                                     

In [2]: printout()                                                                                                            
<--""
   Level WARNING
   |
   o   "TerminalIPythonApp"
   |   Level WARNING
   |   Propagate OFF
   |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
   |     Formatter <traitlets.config.application.LevelFormatter object at 0x7fc5c9924f60>
   |
   o<--"asyncio"
   |   Level NOTSET so inherits level WARNING
   |
   o<--[concurrent]
   |   |
   |   o<--"concurrent.futures"
   |       Level NOTSET so inherits level WARNING
   |
   o<--[parso]
   |   |
   |   o<--"parso.cache"
   |   |   Level NOTSET so inherits level WARNING
   |   |
   |   o<--[parso.python]
   |       |
   |       o<--"parso.python.diff"
   |           Level NOTSET so inherits level WARNING
   |
   o<--"prompt_toolkit"
       Level NOTSET so inherits level WARNING

In [3]: from fuzzywuzzy import process                                                                                        
/home/arah/dev/libraries/fuzzywuzzy/fuzzywuzzy/fuzz.py:11: UserWarning: Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning
  warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning')

In [4]: printout()                                                                                                            
<--""
   Level WARNING
   |
   o   "TerminalIPythonApp"
   |   Level WARNING
   |   Propagate OFF
   |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
   |     Formatter <traitlets.config.application.LevelFormatter object at 0x7fc5c9924f60>
   |
   o<--"asyncio"
   |   Level NOTSET so inherits level WARNING
   |
   o<--[concurrent]
   |   |
   |   o<--"concurrent.futures"
   |       Level NOTSET so inherits level WARNING
   |
   o<--[parso]
   |   |
   |   o<--"parso.cache"
   |   |   Level NOTSET so inherits level WARNING
   |   |
   |   o<--[parso.python]
   |       |
   |       o<--"parso.python.diff"
   |           Level NOTSET so inherits level WARNING
   |
   o<--"prompt_toolkit"
       Level NOTSET so inherits level WARNING

after the changes:

In [1]: from logging_tree import printout                                                                                     

In [2]: printout()                                                                                                            
<--""
   Level WARNING
   |
   o   "TerminalIPythonApp"
   |   Level WARNING
   |   Propagate OFF
   |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
   |     Formatter <traitlets.config.application.LevelFormatter object at 0x7f9461678f60>
   |
   o<--"asyncio"
   |   Level NOTSET so inherits level WARNING
   |
   o<--[concurrent]
   |   |
   |   o<--"concurrent.futures"
   |       Level NOTSET so inherits level WARNING
   |
   o<--[parso]
   |   |
   |   o<--"parso.cache"
   |   |   Level NOTSET so inherits level WARNING
   |   |
   |   o<--[parso.python]
   |       |
   |       o<--"parso.python.diff"
   |           Level NOTSET so inherits level WARNING
   |
   o<--"prompt_toolkit"
       Level NOTSET so inherits level WARNING

In [3]: from fuzzywuzzy import process                                                                                        
/home/arah/dev/libraries/fuzzywuzzy/fuzzywuzzy/fuzz.py:11: UserWarning: Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning
  warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning')

In [4]: printout()                                                                                                            
<--""
   Level WARNING
   |
   o   "TerminalIPythonApp"
   |   Level WARNING
   |   Propagate OFF
   |   Handler Stream <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
   |     Formatter <traitlets.config.application.LevelFormatter object at 0x7f9461678f60>
   |
   o<--"asyncio"
   |   Level NOTSET so inherits level WARNING
   |
   o<--[concurrent]
   |   |
   |   o<--"concurrent.futures"
   |       Level NOTSET so inherits level WARNING
   |
   o<--[fuzzywuzzy]
   |   |
   |   o<--"fuzzywuzzy.process"
   |       Level NOTSET so inherits level WARNING
   |
   o<--[parso]
   |   |
   |   o<--"parso.cache"
   |   |   Level NOTSET so inherits level WARNING
   |   |
   |   o<--[parso.python]
   |       |
   |       o<--"parso.python.diff"
   |           Level NOTSET so inherits level WARNING
   |
   o<--"prompt_toolkit"
       Level NOTSET so inherits level WARNING

this means that I can now do: logging.getLogger("fuzzywuzzy").setLevel("ERROR") in my own application (or any other way of configuring a level) and expect it not to spew out that logger.warning line in to my logs.

to my knowledge this shouldn't affect the warnings.warn by default

gwerbin commented 4 years ago

Is there a reason this wasn't merged? Can I help at all?

Note that using logging.warning instead of logging.getLogger().warning is even worse than just using the root logger. It also attaches a StreamHandler to the root logger, and this behavior cannot be changed. This is extremely annoying and conflicts with logger handling in pretty much any application that makes non-trivial use of logging.

arahayrabedian commented 4 years ago

I think/suspect general inactivity on fuzzywuzzy. I'm still all ears to any changes that need to be made.

jdevries3133 commented 3 years ago

Please merge this pull request; it's a very simple change, but I'm learning more complicated logging patterns for the first time, and I was tearing my hair out for a really long time trying to figure out why I was getting duplicate log output and it was because of this.

This change is best practice, take it straight from the python documentation:

zackkitzmiller commented 3 years ago

@josegonzalez could you take a look and merge this one?

josegonzalez commented 3 years ago

Yeah seems legit. I'll merge on Monday, I need to get access to PyPi again now that I've come back :)

gwerbin commented 2 years ago

Why was this closed? It would be great if we could have this 1-line change merged!

Ping @josegonzalez in case it slipped through the cracks.

arahayrabedian commented 2 years ago

i closed it only cos i saw fuzzywuzzy's apparently been moved to thefuzz (see readme) and I no longer expect this to be merged here.

recently started using github more extensively again and didn't want to keep seeing this in the list of my open pull requests (which i use as a part of my workflow)