aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio/grpc
diff options
context:
space:
mode:
authorGravatar Richard Belleville <rbellevi@google.com>2018-11-07 11:28:52 -0800
committerGravatar Richard Belleville <rbellevi@google.com>2018-11-09 08:47:42 -0800
commitecd90634248ce495dd1dca41c77254ce7877f6db (patch)
tree0684cf8f24f7beb14e8dba0d945dabf5b56f8cc4 /src/python/grpcio/grpc
parent01c6565fb5a9631f6f1dd4f81662af711cab30aa (diff)
Use single NullHandler for whole library
I was trying to get a feel for what the rest of the python ecosystem does with its logging, so I looked into the top few libraries on pypi: urllib3 maintains a logger for not quite every module, but for each one that does heavy lifting. The logger name is `__name__`, and no handlers are registered for any module-level loggers, including NullHandler. Their documentation spells out how to configure logging for the library. They explicitly configure a library root-level logger called `urllib3` to which they attach a `NullHandler`. This addresses the "no handlers could be found" problem. Their tests explicitly configure handlers, just like ours do. scrapy is more hands-on. It provides a configuration module for its logging and a whole document on how to handle logging with scrapy. It looks like log.py's whole reason for existence is making sure that a handler is attached to to the scrapy handler at startup. I think the extra complexity here is because scrapy also offers a CLI, so there has to be some way to configure logging without resorting to writing python, so I felt we didn't need to resort to this added complexity. --- Based on all of the libraries I've looked at, I think our current approach is reasonable. The one change I would make is to explicitly configure a `grpc` logger and to only attach a `NullHandler` to it instead of putting the burden on the author of each new module to configure it there. With this change, I have - Configured a logger in each module that cares about logging - Removed all NullHandlers attached to module-level loggers - Explicitly configured a `grpc` logger with a `NullHandler` attached Resolves: #16572 Related: #17064
Diffstat (limited to 'src/python/grpcio/grpc')
-rw-r--r--src/python/grpcio/grpc/__init__.py4
-rw-r--r--src/python/grpcio/grpc/_channel.py1
-rw-r--r--src/python/grpcio/grpc/_common.py1
-rw-r--r--src/python/grpcio/grpc/_cython/_cygrpc/grpc_string.pyx.pxi1
-rw-r--r--src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi1
-rw-r--r--src/python/grpcio/grpc/_plugin_wrapping.py1
-rw-r--r--src/python/grpcio/grpc/framework/foundation/callable_util.py1
-rw-r--r--src/python/grpcio/grpc/framework/foundation/logging_pool.py1
-rw-r--r--src/python/grpcio/grpc/framework/foundation/stream_util.py1
9 files changed, 3 insertions, 9 deletions
diff --git a/src/python/grpcio/grpc/__init__.py b/src/python/grpcio/grpc/__init__.py
index df98dd10ad..8c15e54082 100644
--- a/src/python/grpcio/grpc/__init__.py
+++ b/src/python/grpcio/grpc/__init__.py
@@ -15,12 +15,14 @@
import abc
import enum
+import logging
import sys
-
import six
from grpc._cython import cygrpc as _cygrpc
+logging.getLogger(__name__).addHandler(logging.NullHandler())
+
############################## Future Interface ###############################
diff --git a/src/python/grpcio/grpc/_channel.py b/src/python/grpcio/grpc/_channel.py
index 3ff7658748..ab154d8512 100644
--- a/src/python/grpcio/grpc/_channel.py
+++ b/src/python/grpcio/grpc/_channel.py
@@ -25,7 +25,6 @@ from grpc._cython import cygrpc
from grpc.framework.foundation import callable_util
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
_USER_AGENT = 'grpc-python/{}'.format(_grpcio_metadata.__version__)
diff --git a/src/python/grpcio/grpc/_common.py b/src/python/grpcio/grpc/_common.py
index 42f3a4e614..f69127e38e 100644
--- a/src/python/grpcio/grpc/_common.py
+++ b/src/python/grpcio/grpc/_common.py
@@ -21,7 +21,6 @@ import grpc
from grpc._cython import cygrpc
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY = {
cygrpc.ConnectivityState.idle:
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/grpc_string.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/grpc_string.pyx.pxi
index fa356d913e..00a1b23a67 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/grpc_string.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/grpc_string.pyx.pxi
@@ -15,7 +15,6 @@
import logging
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
# This function will ascii encode unicode string inputs if neccesary.
# In Python3, unicode strings are the default str type.
diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
index f9d1e863ca..ce701724fd 100644
--- a/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
+++ b/src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
@@ -19,7 +19,6 @@ import time
import grpc
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
cdef class Server:
diff --git a/src/python/grpcio/grpc/_plugin_wrapping.py b/src/python/grpcio/grpc/_plugin_wrapping.py
index 53af2ff913..916ee080b6 100644
--- a/src/python/grpcio/grpc/_plugin_wrapping.py
+++ b/src/python/grpcio/grpc/_plugin_wrapping.py
@@ -21,7 +21,6 @@ from grpc import _common
from grpc._cython import cygrpc
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
class _AuthMetadataContext(
diff --git a/src/python/grpcio/grpc/framework/foundation/callable_util.py b/src/python/grpcio/grpc/framework/foundation/callable_util.py
index 36066e19df..24daf3406f 100644
--- a/src/python/grpcio/grpc/framework/foundation/callable_util.py
+++ b/src/python/grpcio/grpc/framework/foundation/callable_util.py
@@ -22,7 +22,6 @@ import logging
import six
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
class Outcome(six.with_metaclass(abc.ABCMeta)):
diff --git a/src/python/grpcio/grpc/framework/foundation/logging_pool.py b/src/python/grpcio/grpc/framework/foundation/logging_pool.py
index dfb8dbdc30..216e3990db 100644
--- a/src/python/grpcio/grpc/framework/foundation/logging_pool.py
+++ b/src/python/grpcio/grpc/framework/foundation/logging_pool.py
@@ -18,7 +18,6 @@ import logging
from concurrent import futures
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
def _wrap(behavior):
diff --git a/src/python/grpcio/grpc/framework/foundation/stream_util.py b/src/python/grpcio/grpc/framework/foundation/stream_util.py
index e03130cced..1faaf29bd7 100644
--- a/src/python/grpcio/grpc/framework/foundation/stream_util.py
+++ b/src/python/grpcio/grpc/framework/foundation/stream_util.py
@@ -20,7 +20,6 @@ from grpc.framework.foundation import stream
_NO_VALUE = object()
_LOGGER = logging.getLogger(__name__)
-_LOGGER.addHandler(logging.NullHandler())
class TransformingConsumer(stream.Consumer):