Browse Source

Make logzio handler log correctly after os.fork()

opensearch
Jotham Apaloo 7 years ago
parent
commit
066f73264d
3 changed files with 18 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +4
    -0
      logzio/sender.py
  3. +13
    -0
      tests/test_logzioSender.py

+ 1
- 0
.gitignore View File

@ -1,2 +1,3 @@
*.pyc *.pyc
*egg-info/

+ 4
- 0
logzio/sender.py View File

@ -34,7 +34,9 @@ class LogzioSender:
# Create a queue to hold logs # Create a queue to hold logs
self.queue = queue.Queue() self.queue = queue.Queue()
self._initialize_sending_thread()
def _initialize_sending_thread(self):
self.sending_thread = Thread(target=self._drain_queue) self.sending_thread = Thread(target=self._drain_queue)
self.sending_thread.daemon = False self.sending_thread.daemon = False
self.sending_thread.name = "logzio-sending-thread" self.sending_thread.name = "logzio-sending-thread"
@ -42,6 +44,8 @@ class LogzioSender:
def append(self, logs_message): def append(self, logs_message):
# Queue lib is thread safe, no issue here # Queue lib is thread safe, no issue here
if not self.sending_thread.is_alive():
self._initialize_sending_thread()
self.queue.put(json.dumps(logs_message)) self.queue.put(json.dumps(logs_message))
def flush(self): def flush(self):


+ 13
- 0
tests/test_logzioSender.py View File

@ -106,3 +106,16 @@ class TestLogzioSender(TestCase):
with open(failure_files[0], "r") as f: with open(failure_files[0], "r") as f:
line = f.readline() line = f.readline()
self.assertTrue(log_message in line) self.assertTrue(log_message in line)
def test_can_send_after_fork(self):
childpid = os.fork()
log_message = 'logged from child process'
if childpid == 0:
# Log from the child process
self.logger.info(log_message)
time.sleep(self.logs_drain_timeout * 2)
os._exit(0)
# Wait for the child process to finish
os.waitpid(childpid, 0)
self.assertTrue(self.logzio_listener.find_log(log_message))

Loading…
Cancel
Save