10000 Merge pull request #299 from python/master · sthagen/python-cpython@a9f0237 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9f0237

Browse files
authored
Merge pull request #299 from python/master
bpo-39826: add getConnection() hook to logging HTTPHandler (pythonGH-18745)
2 parents eb5aa50 + 22a9a54 commit a9f0237

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Lib/logging/handlers.py

Lines changed: 16 additions & 5 deletions
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":
Lines changed: 1 addition & 0 deletions
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)
0