Re: Issue with logging.config

2010-07-14 Thread Joe Hughes
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', 1000, 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(rReceived = (\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 =  1342010-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.  

Re: Issue with logging.config

2010-07-14 Thread Vinay Sajip
On Jul 14, 3:21 pm, Joe Hughes jwhug...@hughesconcepts.com wrote:
         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.


What I suggested was this:

http://plumberjack.blogspot.com/2010/07/using-custom-formatter-to-deal-with.html

Of course your change may work for you, but it's not a change we can
have in the stdlib - since not everyone will be using ASCII for
everything.

Regards,

Vinay Sajip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with logging.config

2010-07-14 Thread Peter Otten
Vinay Sajip wrote:

 On Jul 14, 3:21 pm, Joe Hughes jwhug...@hughesconcepts.com wrote:
 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.

 
 What I suggested was this:
 
 http://plumberjack.blogspot.com/2010/07/using-custom-formatter-to-deal-
with.html
 
 Of course your change may work for you, but it's not a change we can
 have in the stdlib - since not everyone will be using ASCII for
 everything.

It's not obvious to me how you'd adapt that code to work with python3.1 and 
the SysLogHandler. Can you give some details?

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with logging.config

2010-07-14 Thread Joe Hughes
This is why I did what I did, because I couldn't figure it out either.  I did 
find issue 5421 at python.org which is where I got the idea for the code change.

Joe

On Jul 14, 2010, at 12:35 PM, Peter Otten wrote:

 Vinay Sajip wrote:
 
 On Jul 14, 3:21 pm, Joe Hughes jwhug...@hughesconcepts.com wrote:
 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.
 
 
 What I suggested was this:
 
 http://plumberjack.blogspot.com/2010/07/using-custom-formatter-to-deal-
 with.html
 
 Of course your change may work for you, but it's not a change we can
 have in the stdlib - since not everyone will be using ASCII for
 everything.
 
 It's not obvious to me how you'd adapt that code to work with python3.1 and 
 the SysLogHandler. Can you give some details?
 
 Peter
 
 -- 
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with logging.config

2010-07-14 Thread Vinay Sajip
On Jul 14, 7:08 pm, Joe Hughes jwhug...@hughesconcepts.com wrote:
 This is why I did what I did, because I couldn't figure it out either.  I did 
 find issue 5421 at python.org which is where I got the idea for the code 
 change.

Perhaps you should read the messages for issue 7077, linked to by
Peter above. You'll see that according to RFC 5424, syslog UDP
messages should be encoded in UTF-8 with a BOM, not ASCII. Revisions
r75586 and later of the Python repository implement the correct fix
for the problem.

My suggestion (in the blog post I linked to) would apply to socket
applications other than syslog.

Regards,

Vinay Sajip
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with logging.config

2010-07-14 Thread Joe Hughes
Hi Vinay,

I think I figured out what you are talking about after reading RFC 
5424.  I think this means that this code

syslog.info(Status -  + mcu_dict[pinged.ip] +  is  + status[pinged.status])

needs to become something like this

BOM = 0xEFBBBF
msg = str(BOM) + Status -  + mcu_dict[pinged.ip] +  is  + 
status[pinged.status]
syslog.info(msg)

This would add the BOM to the message that RFC 5424 requires.  Or did I totally 
misread it?

Thanks,
Joe

On Jul 14, 2010, at 8:20 PM, Vinay Sajip wrote:

 On Jul 14, 7:08 pm, Joe Hughes jwhug...@hughesconcepts.com wrote:
 This is why I did what I did, because I couldn't figure it out either.  I 
 did find issue 5421 at python.org which is where I got the idea for the code 
 change.
 
 Perhaps you should read the messages for issue 7077, linked to by
 Peter above. You'll see that according to RFC 5424, syslog UDP
 messages should be encoded in UTF-8 with a BOM, not ASCII. Revisions
 r75586 and later of the Python repository implement the correct fix
 for the problem.
 
 My suggestion (in the blog post I linked to) would apply to socket
 applications other than syslog.
 
 Regards,
 
 Vinay Sajip
 -- 
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Issue with logging.config

2010-07-13 Thread Peter Otten
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', 1000, 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(rReceived = (\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 =  1342010-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:
 

Issue with logging.config

2010-07-08 Thread Joe Hughes
Hi Python Help:

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', 1000, 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(rReceived = (\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 =  1342010-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)