[Python-checkins] bpo-39826: add getConnection() hook to logging HTTPHandler (GH-18745)

l0rb webhook-mailer at python.org
Wed Mar 4 05:49:56 EST 2020


https://github.com/python/cpython/commit/22a9a546ff3bf2a63d77ca1e5494e758bc59132f
commit: 22a9a546ff3bf2a63d77ca1e5494e758bc59132f
branch: master
author: l0rb <lorbritzer at yahoo.de>
committer: GitHub <noreply at github.com>
date: 2020-03-04T10:49:51Z
summary:

bpo-39826: add getConnection() hook to logging HTTPHandler (GH-18745)

files:
A Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst
M Lib/logging/handlers.py

diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index 047798f6dc145..4a120e9f1ec48 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -1173,6 +1173,20 @@ def mapLogRecord(self, record):
         """
         return record.__dict__
 
+    def getConnection(self, host, secure):
+        """
+        get a HTTP[S]Connection.
+
+        Override when a custom connection is required, for example if
+        there is a proxy.
+        """
+        import http.client
+        if secure:
+            connection = http.client.HTTPSConnection(host, context=self.context)
+        else:
+            connection = http.client.HTTPConnection(host)
+        return connection
+
     def emit(self, record):
         """
         Emit a record.
@@ -1180,12 +1194,9 @@ def emit(self, record):
         Send the record to the Web server as a percent-encoded dictionary
         """
         try:
-            import http.client, urllib.parse
+            import urllib.parse
             host = self.host
-            if self.secure:
-                h = http.client.HTTPSConnection(host, context=self.context)
-            else:
-                h = http.client.HTTPConnection(host)
+            h = self.getConnection(host, self.secure)
             url = self.url
             data = urllib.parse.urlencode(self.mapLogRecord(record))
             if self.method == "GET":
diff --git a/Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst b/Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst
new file mode 100644
index 0000000000000..e425bbe2d5110
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-03-02-15-15-01.bpo-39826.DglHk7.rst
@@ -0,0 +1 @@
+Add getConnection method to logging HTTPHandler to enable custom connections.
\ No newline at end of file



More information about the Python-checkins mailing list