Skip to content

Commit

Permalink
Merge pull request #6 from FerdinandZhong/develop
Browse files Browse the repository at this point in the history
add verbose and update version
  • Loading branch information
FerdinandZhong authored Nov 10, 2021
2 parents fd44613 + 065a02f commit 6b8b799
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
6 changes: 4 additions & 2 deletions examples/simple.py → examples/simple_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def worker(text):
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(filename)s- [%(levelname)s]: %(message)s"
)
register_handler(client_logger, logging_level=logging.INFO, formatter=formatter)
register_handler(
client_logger, logging_level=logging.INFO, formatter=formatter, verbose=True
)
client_logger.setLevel(logging.INFO)
pid = os.getpid()
for i in range(TEST_ROUND):
Expand Down Expand Up @@ -47,7 +49,7 @@ def sample_running(text):

server_handler = ServerHandler(LOG_FILE, maxBytes=MAX_BYTES)
server_handler.setLevel(logging.INFO)
server = Server(server_handler, batch_size=BATCH_SIZE)
server = Server(server_handler, batch_size=BATCH_SIZE, verbose=True)
server.start()

sample_running(args.text)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

setup(
name="mp-socket-logging",
version="0.1.0",
version="0.1.1",
description="A small toolkit for python multiprocessing file logging",
long_description=readme,
long_description_content_type="text/markdown",
author="Zhong Qishuai",
author_email="[email protected]",
url="https://git.garena.com/shopee-ds/ml-platform/sre-utils",
url="https://github.com/FerdinandZhong/mp-socket-logging",
packages=find_packages(exclude=["tests*", "example*"]),
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
9 changes: 5 additions & 4 deletions socket_logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Client(logging.Handler):
logging (str): socket address to connect to server
"""

def __init__(self, socket_addr) -> None:
def __init__(self, socket_addr, verbose) -> None:
super().__init__()
self.socket = socket.socket(socket.AF_UNIX)
self.socket.settimeout(1)
self.logger = logging.getLogger(__name__)
register_logger(self.logger)
register_logger(self.logger, verbose)
try:
self.socket.connect(socket_addr)
self.logger.info(f"socket connect to {socket_addr}")
self.logger.debug(f"socket connect to {socket_addr}")
except BrokenPipeError as err:
self.logger.warning(f"socket is broken: {err}")
except FileNotFoundError as err:
Expand Down Expand Up @@ -57,6 +57,7 @@ def register_handler(
logging_level,
formatter: logging.Formatter = None,
socket_addr="/tmp/socket",
verbose=False,
):
"""Register the Client instance to the logger
Expand All @@ -71,7 +72,7 @@ def register_handler(
formatter = logging.Formatter(
"%(asctime)s - %(name)s - [%(levelname)s]: %(message)s"
)
client = Client(socket_addr)
client = Client(socket_addr=socket_addr, verbose=verbose)
client.setFormatter(formatter)
client.setLevel(logging_level)
target_logger.addHandler(client)
11 changes: 6 additions & 5 deletions socket_logging/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(
defined_handler: logging.Handler,
socket_address="/tmp/socket",
batch_size=20000,
verbose=False,
) -> None:
"""Server for receiving logs from client and do writting logs in batch to files.
Expand All @@ -112,12 +113,12 @@ def __init__(
except OSError:
pass
self.logger = logging.getLogger(__name__)
register_logger(self.logger)
register_logger(self.logger, verbose)

self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.bind(socket_address)
self.socket.listen()
self.logger.info(f"listening on {socket_address}")
self.logger.debug(f"listening on {socket_address}")
self.socket.setblocking(False)

self.sel = selectors.DefaultSelector()
Expand All @@ -131,7 +132,7 @@ def __init__(

def accept(self, sock):
conn, addr = sock.accept() # Should be ready to read
self.logger.info(f"accepted connection from {addr}")
self.logger.debug(f"accepted connection from {addr}")
conn.setblocking(False)
events = selectors.EVENT_READ
self.sel.register(conn, events, data=self.read_data)
Expand All @@ -152,7 +153,7 @@ def read_data(self, conn):
else:
self.bytes += LINE_BREAK
if self.termination.is_set():
self.logger.info(f"current total length {self.total_length}")
self.logger.debug(f"rest log record total length {self.total_length}")
self.handler.emit(self.bytes.decode())
self.bytes = bytearray()
self.sel.unregister(conn)
Expand All @@ -179,7 +180,7 @@ def run(self):
callback = key.data
callback(key.fileobj)
if self.termination.is_set():
self.logger.info("termination is set")
self.logger.debug("termination is set")
break
self.socket.close()
self.sel.close()
Expand Down
4 changes: 2 additions & 2 deletions socket_logging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def format(self, record):
return formatter.format(record)


def register_logger(logger=None):
def register_logger(logger=None, verbose=False):
"""register colorful debug log"""
if not logger:
logger = logging.getLogger()
if not logger.hasHandlers():
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(ColorfulFormatter())
logger.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG) if verbose else logger.setLevel(logging.INFO)
logger.addHandler(handler)

0 comments on commit 6b8b799

Please sign in to comment.