Issue with logging.config

Joe Hughes jwhughes at hughesconcepts.com
Wed Jul 14 10:21:32 EDT 2010


Peter,

	Thanks for the information.  I sent an email to the maintainer and got some information that helped me continue with this.  My solution was to change line 785 of handlers.py to

self.socket.sendto(bytes(msg, 'ascii'), self.address)

After I made the change, I got exactly what I was looking for.

Joe

On Jul 13, 2010, at 8:02 AM, Peter Otten wrote:

> Joe Hughes wrote:
> 
>> I'm doing some work with logging.config and I'm running into an
>> interesting situation.  I've run this by python-help, but that didn't help
>> so I thought I would send to the list.  Here is the config file
>> 
>> [loggers]
>> keys=root,log,syslog
>> 
>> [handlers]
>> keys=console,log,syslog
>> 
>> [formatters]
>> keys=rootFormat,logFormat,syslogFormat
>> 
>> [logger_root]
>> level=DEBUG
>> handlers=console
>> 
>> [logger_log]
>> level=DEBUG
>> handlers=log
>> qualname=log
>> 
>> [logger_syslog]
>> level=DEBUG
>> handlers=syslog
>> qualname=syslog
>> 
>> [handler_console]
>> class=StreamHandler
>> level=DEBUG
>> formatter=rootFormat
>> args=(sys.stdout,)
>> 
>> [handler_log]
>> class=handlers.RotatingFileHandler
>> level=DEBUG
>> formatter=logFormat
>> args=('E:\local\Logs\syslog_interface.txt', 'a', 10000000, 10)
>> propagate=0
>> 
>> [handler_syslog]
>> class=handlers.SysLogHandler
>> level=DEBUG
>> formatter=syslogFormat
>> host=141.232.41.205
>> port=handlers.SYSLOG_UDP_PORT
>> facility=LOG_LOCAL0
>> 
> args=(('141.232.41.205',handlers.SYSLOG_UDP_PORT),handlers.SysLogHandler.LOG_LOCAL0)
>> 
>> [formatter_rootFormat]
>> format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
>> 
>> [formatter_logFormat]
>> format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
>> 
>> [formatter_syslogFormat]
>> format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
>> 
>> and the python code
>> 
>> ########################################################################
>> # Imports
>> ########################################################################
>> import logging
>> import logging.config
>> import os
>> import re
>> from threading import Thread
>> 
>> ########################################################################
>> # Constants
>> ########################################################################
>> CONFIG_FILENAME = 'E:\local\Config\syslog_interface.conf'
>> MCU_LIST_FILENAME = 'E:\local\Scada_Devices\mcu.txt'
>> 
>> ########################################################################
>> # Classes
>> ########################################################################
>> 
>> ########
>> # PingIt
>> ########
>> class PingIt(Thread):
>>    def __init__(self, mcuIP):
>>        Thread.__init__(self)
>>        self.ip = mcuIP
>>        self.status = -1
>> 
>>    def run(self):
>>        pinging = os.popen("ping -n 2 " + self.ip, 'r')
>> 
>>        while 1:
>>            line = pinging.readline()
>> 
>>            if not line:
>>                break
>> 
>>            gotResponse = re.findall(PingIt.lifeline, line)
>> 
>>            if gotResponse:
>>                self.status = int(gotResponse[0])
>> 
>> ########################################################################
>> # Main Routine
>> ########################################################################
>> #
>> # Get the logger configuration information
>> #
>> logging.config.fileConfig(CONFIG_FILENAME)
>> 
>> #
>> # Check if running from command line and create logger
>> #
>> if os.environ.get('PROMPT'):
>>    #
>>    # create logger for output to stdout
>>    #
>>    logger = logging.getLogger('root')
>> else:
>>    #
>>    # create logger for output to logfile
>>    #
>>    logger = logging.getLogger('log')
>> 
>> #
>> # Create logger for syslog output
>> #
>> syslog = logging.getLogger('syslog')
>> 
>> #
>> # Declare variables
>> #
>> PingIt.lifeline = re.compile(r"Received = (\d)")
>> mcu_dict = {}
>> ping_list = []
>> status = ("Not responding", "Responded to only 1 ping of 2", "Alive")
>> 
>> #
>> # Open the MCU file
>> #
>> mcu_file = open(MCU_LIST_FILENAME, 'r')
>> 
>> #
>> # Loop through the contents of the MCU file and ping the IPs
>> #
>> for mcu in mcu_file:
>>    #
>>    # mcu file contents example
>>    # 192.168.97.227 MCU_HMSTD
>>    #
>>    # mcu_info[0] = MCU IP Address
>>    # mcu_info[1] = MCU Name
>>    #
>>    mcu_info = mcu.split()
>>    mcu_dict[mcu_info[0]] = mcu_info[1]
>>    current = PingIt(mcu_info[0])
>>    logger.info("Pinging " + mcu_info[1])
>>    ping_list.append(current)
>>    current.start()
>> 
>> #
>> # Loop through ping list and print the response
>> #
>> for pinged in ping_list:
>>    pinged.join()
>>    logger.info("Status - " + mcu_dict[pinged.ip] + " is " +
>>    status[pinged.status]) syslog.info("Status - " + mcu_dict[pinged.ip] +
>>    " is " + status[pinged.status])
>> 
>> This is the output from the code
>> 
>> 2010-07-06 14:43:58,280 - log - INFO - Status - netboss2 is Not responding
>> msg =  <134>2010-07-06 14:43:58,312 - syslog - INFO - Status - netboss2 is
>> Not responding
>> address =  ('141.232.41.205', 514)
>> Traceback (most recent call last):
>>  File "C:\Python31\lib\logging\handlers.py", line 786, in emit
>>    self.socket.sendto(msg, self.address)
>> TypeError: sendto() takes exactly 3 arguments (2 given)
>> 2010-07-06 14:43:58,312 - syslog - INFO - Status - netboss2 is Not
>> responding
>> 
>> This is the handlers.py code from the library.  I added the print
>> statement to figure out why it is asking for three args but only getting
>> two.
>> 
>> Line 777 of handlers.py
>> 
>>        try:
>>            if self.unixsocket:
>>                try:
>>                    self.socket.send(msg)
>>                except socket.error:
>>                    self._connect_unixsocket(self.address)
>>                    self.socket.send(msg)
>>            else:
>>                print('msg = ', msg, '\naddress = ', self.address)
>>                self.socket.sendto(msg, self.address)
>>        except (KeyboardInterrupt, SystemExit):
>>            raise
>>        except:
>>            self.handleError(record)
>> 
>> line 790 of handlers.py
>> 
>> This is Python/Idle 3.1.2 on Windows 2003 Server.  If anyone has an idea
>> about why this happening I would appreciate knowing what the issue is.
> 
> The error can be reproduced with
> 
> Python 3.1.1+ (r311:74480, Nov  2 2009, 15:45:00)
> [GCC 4.4.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import logging
>>>> from logging.handlers import SysLogHandler
>>>> handler = SysLogHandler()
>>>> logger = logging.getLogger()
>>>> logger.addHandler(handler)
>>>> logger.critical("yadda")
> Traceback (most recent call last):
>  File "/usr/lib/python3.1/logging/handlers.py", line 785, in emit
>    self.socket.sendto(msg, self.address)
> TypeError: sendto() takes exactly 3 arguments (2 given)
> 
> This is is a known bug, see http://bugs.python.org/issue7077
> 
> Peter
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list