@@ -835,6 +835,36 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
835
835
self .address = address
836
836
self .facility = facility
837
837
self .socktype = socktype
838
+ self .socket = None
839
+ self .createSocket ()
840
+
841
+ def _connect_unixsocket (self , address ):
842
+ use_socktype = self .socktype
843
+ if use_socktype is None :
844
+ use_socktype = socket .SOCK_DGRAM
845
+ self .socket = socket .socket (socket .AF_UNIX , use_socktype )
846
+ try :
847
+ self .socket .connect (address )
848
+ # it worked, so set self.socktype to the used type
849
+ self .socktype = use_socktype
850
+ except OSError :
851
+ self .socket .close ()
852
+ if self .socktype is not None :
853
+ # user didn't specify falling back, so fail
854
+ raise
855
+ use_socktype = socket .SOCK_STREAM
856
+ self .socket = socket .socket (socket .AF_UNIX , use_socktype )
857
+ try :
858
+ self .socket .connect (address )
859
+ # it worked, so set self.socktype to the used type
860
+ self .socktype = use_socktype
861
+ except OSError :
862
+ self .socket .close ()
863
+ raise
864
+
865
+ def createSocket (self ):
866
+ address = self .address
867
+ socktype = self .socktype
838
868
839
869
if isinstance (address , str ):
840
870
self .unixsocket = True
@@ -871,30 +901,6 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
871
901
self .socket = sock
872
902
self .socktype = socktype
873
903
874
- def _connect_unixsocket (self , address ):
875
- use_socktype = self .socktype
876
- if use_socktype is None :
877
- use_socktype = socket .SOCK_DGRAM
878
- self .socket = socket .socket (socket .AF_UNIX , use_socktype )
879
- try :
880
- self .socket .connect (address )
881
- # it worked, so set self.socktype to the used type
882
- self .socktype = use_socktype
883
- except OSError :
884
- self .socket .close ()
885
- if self .socktype is not None :
886
- # user didn't specify falling back, so fail
887
- raise
888
- use_socktype = socket .SOCK_STREAM
889
- self .socket = socket .socket (socket .AF_UNIX , use_socktype )
890
- try :
891
- self .socket .connect (address )
892
- # it worked, so set self.socktype to the used type
893
- self .socktype = use_socktype
894
- except OSError :
895
- self .socket .close ()
896
- raise
897
-
898
904
def encodePriority (self , facility , priority ):
899
905
"""
900
906
Encode the facility and priority. You can pass in strings or
@@ -914,7 +920,10 @@ def close(self):
914
920
"""
915
921
self .acquire ()
916
922
try :
917
- self .socket .close ()
923
+ sock = self .socket
924
+ if sock :
925
+ self .socket = None
926
+ sock .close ()
918
927
logging .Handler .close (self )
919
928
finally :
920
929
self .release ()
@@ -954,6 +963,10 @@ def emit(self, record):
954
963
# Message is a string. Convert to bytes as required by RFC 5424
955
964
msg = msg .encode ('utf-8' )
956
965
msg = prio + msg
966
+
967
+ if not self .socket :
968
+ self .createSocket ()
969
+
957
970
if self .unixsocket :
958
971
try :
959
972
self .socket .send (msg )
0 commit comments