Created
March 26, 2018 15:43
-
-
Save charterchap/8a28d76026eaf4d071cf05005c22e1a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# python_nanomsg_logger_consumer.py | |
# run me first | |
from __future__ import print_function | |
from nanomsg import Socket, PAIR, PUB, SUB, SUB_SUBSCRIBE | |
s2 = Socket(SUB) | |
s2.connect(b"ipc:///tmp/nanologger.ipc") | |
s2.set_string_option(SUB, SUB_SUBSCRIBE, b'') | |
print(s2.recv()) | |
print(s2.recv()) | |
s2.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These two files demonstrate a simple PUB/SUB nanomsg logger | |
# that can be used to capture a log file in another process (IPC) | |
# or you could do it over a tcp connection | |
# please leave a comment if this is useful for you | |
# | |
# python_nanomsg_logger_producer.py | |
# | |
from __future__ import print_function | |
from nanomsg import Socket, PAIR, PUB, SUB | |
import logging | |
class nanoMsgLogger(logging.Handler): | |
""" | |
""" | |
def __init__(self): | |
logging.Handler.__init__(self) | |
self.s1 = Socket(PUB) | |
self.s1.bind(b"ipc:///tmp/nanologger.ipc") | |
def __del__(self): | |
self.s1.close() | |
def emit(self, record): | |
self.s1.send(record.msg.encode()) | |
logging.basicConfig(level=logging.INFO, format='%(message)s') | |
logger = logging.getLogger() | |
logger.addHandler( nanoMsgLogger() ) | |
print = logger.info | |
import time | |
time.sleep(.1) # Don't know why, doesn't work without it... | |
logger.info("hello my tuppentup friend") | |
print("hello my tuppentup friend: print") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment