kiwi.logger_socket
1# Copyright (c) 2022 Marcus Schäfer. All rights reserved. 2# 3# This file is part of kiwi. 4# 5# kiwi is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 3 of the License, or 8# (at your option) any later version. 9# 10# kiwi is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with kiwi. If not, see <http://www.gnu.org/licenses/> 17# 18import logging.handlers 19 20 21class PlainTextSocketHandler(logging.handlers.SocketHandler): 22 def makePickle(self, record): 23 """ 24 Custom makePickle method which actually does not pickle 25 the messages into the pickle binary format but just 26 sends the log message as plain text. A simple server 27 listening could then look like the following example 28 29 .. code:: python 30 31 sock_file = '/tmp/log_socket' 32 buffer = 1024 33 if os.path.exists(sock_file): 34 os.unlink(sock_file) 35 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 36 sock.bind(sock_file) 37 sock.listen(1) 38 while True: 39 connection, client_address = sock.accept() 40 try: 41 while True: 42 data = connection.recv(buffer) 43 if not data: 44 break 45 print(data.decode()) 46 finally: 47 connection.close() 48 """ 49 message = self.formatter.format(record) 50 return message.encode()
class
PlainTextSocketHandler(logging.handlers.SocketHandler):
22class PlainTextSocketHandler(logging.handlers.SocketHandler): 23 def makePickle(self, record): 24 """ 25 Custom makePickle method which actually does not pickle 26 the messages into the pickle binary format but just 27 sends the log message as plain text. A simple server 28 listening could then look like the following example 29 30 .. code:: python 31 32 sock_file = '/tmp/log_socket' 33 buffer = 1024 34 if os.path.exists(sock_file): 35 os.unlink(sock_file) 36 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 37 sock.bind(sock_file) 38 sock.listen(1) 39 while True: 40 connection, client_address = sock.accept() 41 try: 42 while True: 43 data = connection.recv(buffer) 44 if not data: 45 break 46 print(data.decode()) 47 finally: 48 connection.close() 49 """ 50 message = self.formatter.format(record) 51 return message.encode()
A handler class which writes logging records, in pickle format, to a streaming socket. The socket is kept open across logging calls. If the peer resets it, an attempt is made to reconnect on the next call. The pickle which is sent is that of the LogRecord's attribute dictionary (__dict__), so that the receiver does not need to have the logging module installed in order to process the logging event.
To unpickle the record at the receiving end into a LogRecord, use the makeLogRecord function.
def
makePickle(self, record):
23 def makePickle(self, record): 24 """ 25 Custom makePickle method which actually does not pickle 26 the messages into the pickle binary format but just 27 sends the log message as plain text. A simple server 28 listening could then look like the following example 29 30 .. code:: python 31 32 sock_file = '/tmp/log_socket' 33 buffer = 1024 34 if os.path.exists(sock_file): 35 os.unlink(sock_file) 36 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) 37 sock.bind(sock_file) 38 sock.listen(1) 39 while True: 40 connection, client_address = sock.accept() 41 try: 42 while True: 43 data = connection.recv(buffer) 44 if not data: 45 break 46 print(data.decode()) 47 finally: 48 connection.close() 49 """ 50 message = self.formatter.format(record) 51 return message.encode()
Custom makePickle method which actually does not pickle the messages into the pickle binary format but just sends the log message as plain text. A simple server listening could then look like the following example
.. code:: python
sock_file = '/tmp/log_socket'
buffer = 1024
if os.path.exists(sock_file):
os.unlink(sock_file)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(sock_file)
sock.listen(1)
while True:
connection, client_address = sock.accept()
try:
while True:
data = connection.recv(buffer)
if not data:
break
print(data.decode())
finally:
connection.close()