Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22a9a54

Browse files
authored
bpo-39826: add getConnection() hook to logging HTTPHandler (GH-18745)
1 parent be501ca commit 22a9a54

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Lib/logging/handlers.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -1173,19 +1173,30 @@ def mapLogRecord(self, record):
11731173
"""
11741174
return record.__dict__
11751175

1176+
def getConnection(self, host, secure):
1177+
"""
1178+
get a HTTP[S]Connection.
1179+
1180+
Override when a custom connection is required, for example if
1181+
there is a proxy.
1182+
"""
1183+
import http.client
1184+
if secure:
1185+
connection = http.client.HTTPSConnection(host, context=self.context)
1186+
else:
1187+
connection = http.client.HTTPConnection(host)
1188+
return connection
1189+
11761190
def emit(self, record):
11771191
"""
11781192
Emit a record.
11791193
11801194
Send the record to the Web server as a percent-encoded dictionary
11811195
"""
11821196
try:
1183-
import http.client, urllib.parse
1197+
import urllib.parse
11841198
host = self.host
1185-
if self.secure:
1186-
h = http.client.HTTPSConnection(host, context=self.context)
1187-
else:
1188-
h = http.client.HTTPConnection(host)
1199+
h = self.getConnection(host, self.secure)
11891200
url = self.url
11901201
data = urllib.parse.urlencode(self.mapLogRecord(record))
11911202
if self.method == "GET":
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add getConnection method to logging HTTPHandler to enable custom connections.

0 commit comments

Comments
 (0)