изменить строку
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=file, filemode=('a' if os.path.exists(file_path) else 'w'))
на
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=file, filemode=('a' if os.path.exists(file_path) else 'w'))
Конечный код
#!/usr/bin/env python
import os, stat
import datetime
import logging
import SocketServer
HOST, PORT = "0.0.0.0", 514
class SyslogUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = bytes.decode(self.request[0].strip())
socket = self.request[1]
today = datetime.datetime.now()
year = today.strftime("%Y")
month=today.strftime("%m")
day=today.strftime("%d")
file_path = "/listen/" + year +"/" + month + "/" + day
file= file_path + "/test.log"
if(not os.path.exists(file_path)):
os.makedirs(file_path)
logging.basicConfig(level=logging.INFO, format='%(message)s', datefmt='', filename=file, filemode=('a' if os.path.exists(file_path) else 'w'))
logging.info(str(data))
if __name__ == "__main__":
try:
server = SocketServer.UDPServer((HOST,PORT), SyslogUDPHandler)
server.serve_forever(poll_interval=0.5)
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")