1- # Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
1+ # Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
22#
33# Permission to use, copy, modify, and distribute this software and its
44# documentation for any purpose and without fee is hereby granted,
1818Additional handlers for the logging package for Python. The core package is
1919based on PEP 282 and comments thereto in comp.lang.python.
2020
21- Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
21+ Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
2222
2323To use, simply 'import logging.handlers' and log away!
2424"""
@@ -767,7 +767,7 @@ class SysLogHandler(logging.Handler):
767767 }
768768
769769 def __init__ (self , address = ('localhost' , SYSLOG_UDP_PORT ),
770- facility = LOG_USER , socktype = socket . SOCK_DGRAM ):
770+ facility = LOG_USER , socktype = None ):
771771 """
772772 Initialize a handler.
773773
@@ -786,18 +786,37 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
786786 self ._connect_unixsocket (address )
787787 else :
788788 self .unixsocket = False
789+ if socktype is None :
790+ socktype = socket .SOCK_DGRAM
789791 self .socket = socket .socket (socket .AF_INET , socktype )
790792 if socktype == socket .SOCK_STREAM :
791793 self .socket .connect (address )
794+ self .socktype = socktype
792795 self .formatter = None
793796
794797 def _connect_unixsocket (self , address ):
795- self .socket = socket .socket (socket .AF_UNIX , self .socktype )
798+ use_socktype = self .socktype
799+ if use_socktype is None :
800+ use_socktype = socket .SOCK_DGRAM
801+ self .socket = socket .socket (socket .AF_UNIX , use_socktype )
796802 try :
797803 self .socket .connect (address )
804+ # it worked, so set self.socktype to the used type
805+ self .socktype = use_socktype
798806 except socket .error :
799807 self .socket .close ()
800- raise
808+ if self .socktype is not None :
809+ # user didn't specify falling back, so fail
810+ raise
811+ use_socktype = socket .SOCK_STREAM
812+ self .socket = socket .socket (socket .AF_UNIX , use_socktype )
813+ try :
814+ self .socket .connect (address )
815+ # it worked, so set self.socktype to the used type
816+ self .socktype = use_socktype
817+ except socket .error :
818+ self .socket .close ()
819+ raise
801820
802821 def encodePriority (self , facility , priority ):
803822 """
0 commit comments