@@ -758,13 +758,17 @@ class SysLogHandler(logging.Handler):
758758 }
759759
760760 def __init__ (self , address = ('localhost' , SYSLOG_UDP_PORT ),
761- facility = LOG_USER , socktype = socket . SOCK_DGRAM ):
761+ facility = LOG_USER , socktype = None ):
762762 """
763763 Initialize a handler.
764764
765765 If address is specified as a string, a UNIX socket is used. To log to a
766766 local syslogd, "SysLogHandler(address="/dev/log")" can be used.
767- If facility is not specified, LOG_USER is used.
767+ If facility is not specified, LOG_USER is used. If socktype is
768+ specified as socket.SOCK_DGRAM or socket.SOCK_STREAM, that specific
769+ socket type will be used. For Unix sockets, you can also specify a
770+ socktype of None, in which case socket.SOCK_DGRAM will be used, falling
771+ back to socket.SOCK_STREAM.
768772 """
769773 logging .Handler .__init__ (self )
770774
@@ -777,18 +781,37 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
777781 self ._connect_unixsocket (address )
778782 else :
779783 self .unixsocket = False
784+ if socktype is None :
785+ socktype = socket .SOCK_DGRAM
780786 self .socket = socket .socket (socket .AF_INET , socktype )
781787 if socktype == socket .SOCK_STREAM :
782788 self .socket .connect (address )
789+ self .socktype = socktype
783790 self .formatter = None
784791
785792 def _connect_unixsocket (self , address ):
786- self .socket = socket .socket (socket .AF_UNIX , self .socktype )
793+ use_socktype = self .socktype
794+ if use_socktype is None :
795+ use_socktype = socket .SOCK_DGRAM
796+ self .socket = socket .socket (socket .AF_UNIX , use_socktype )
787797 try :
788798 self .socket .connect (address )
799+ # it worked, so set self.socktype to the used type
800+ self .socktype = use_socktype
789801 except OSError :
790802 self .socket .close ()
791- raise
803+ if self .socktype is not None :
804+ # user didn't specify falling back, so fail
805+ raise
806+ use_socktype = socket .SOCK_STREAM
807+ self .socket = socket .socket (socket .AF_UNIX , use_socktype )
808+ try :
809+ self .socket .connect (address )
810+ # it worked, so set self.socktype to the used type
811+ self .socktype = use_socktype
812+ except OSError :
813+ self .socket .close ()
814+ raise
792815
793816 def encodePriority (self , facility , priority ):
794817 """
0 commit comments