GeospatialPython / pyshp

This library reads and writes ESRI Shapefiles in pure Python.
MIT License
1.09k stars 259 forks source link

logging should use named logger rather than root logger #240

Closed heikoklein closed 2 years ago

heikoklein commented 2 years ago

Hi, I'm plotting maps with cartopy and use the GSHHS maps as background. From there I get warnings like: WARNING:root:Possible issue encountered when converting Shape #95 to GeoJSON: Shapefile format requires that polygons contain at least one exterior ring, but the Shape was entirely made up of interior holes (defined by counter-clockwise orientation in the shapefile format). The rings were still included but were encoded as GeoJSON exterior rings instead of holes.

After some search I found out that these warnings are from shapefile.py, which uses the root-logger as logger.warning whenever a problem occurs. Currently, I have to suppress the root-logger to suppress the warning, i.e.

logging.root.setLevel(logging.ERROR)

As a library, it would make the users life easier if a named logger rather than the root logger would be used as suggested by the logging documentation:

A good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:

logger = logging.getLogger(__name__)
...
logger.warning(msg)

Then a user (or the GSHHS library provider) could suppress the specific warnings as logging.getLogger('shapefile').setLevel(logging.ERROR)

Best regards, Heiko

karimbahgat commented 2 years ago

I had a small note in the docs to toggle the warnings by setting the module VERBOSE constant to False, but updated the README to make this more clear. In addition I switched to your suggested solution of using a named logger. Thanks!