Skip to content

Commit

Permalink
Merge pull request #107 from deep-compute/feature-sighup
Browse files Browse the repository at this point in the history
references #106, on receiveding SIGUSR1, reopening log file to enable…
  • Loading branch information
prashanthellina committed Jul 14, 2021
2 parents 6e3b772 + f13d062 commit 562ff01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
32 changes: 30 additions & 2 deletions basescript/log.py
Expand Up @@ -6,6 +6,7 @@
import socket
import logging
import numbers
import signal
from six.moves import queue
from threading import Thread, Lock
from datetime import datetime
Expand Down Expand Up @@ -42,6 +43,32 @@ def close(self):
s.close()


class FileWrapper:
def __init__(self, fpath):
self.fpath = fpath
self.lock = Lock()
self.f = open(self.fpath, "a")

signal.signal(signal.SIGUSR1, self.__sighandler__)

def close(self):
with self.lock:
return self.f.close()

def write(self, *args, **kwargs):
with self.lock:
return self.f.write(*args, **kwargs)

def flush(self):
with self.lock:
return self.f.flush()

def __sighandler__(self, signum, frame):
with self.lock:
self.f.close()
self.f = open(self.fpath, "a")


class StderrConsoleRenderer(object):
BACKUP_KEYS = ("timestamp", "level", "event", "logger", "stack", "exception")

Expand Down Expand Up @@ -247,7 +274,7 @@ def setLevel(self, level):


def _structlog_default_keys_processor(logger_class, log_method, event):
""" Add unique id, type and hostname """
"""Add unique id, type and hostname"""
global HOSTNAME

if "id" not in event:
Expand Down Expand Up @@ -393,7 +420,8 @@ def _configure_logger(
streams = []

if fpath:
streams.append(open(fpath, "a"))
f = FileWrapper(fpath)
streams.append(f)

if fmt == "json" and not quiet:
streams.append(sys.stderr)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -25,7 +25,7 @@ def get_long_description():

long_description = get_long_description()

version = "0.3.7"
version = "0.3.8"
setup(
name="basescript",
version=version,
Expand Down

0 comments on commit 562ff01

Please sign in to comment.