Skip to content

Instantly share code, notes, and snippets.

@JimDennis
Created July 11, 2014 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimDennis/355d14c8a8a1e92c2210 to your computer and use it in GitHub Desktop.
Save JimDennis/355d14c8a8a1e92c2210 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''Act as a simple relay from a FIFO logging target (or regular file)
Into another file and the local UNIX syslog service
Call with the name of a file to write to (new log file) and the name of a FIFO
or regular file to relay from.
'''
if __name__ == '__main__':
import sys, time, syslog
## https://docs.python.org/2/library/syslog.html ## syslog.openlog('HGS relogger', syslog.LOG_LOCAL7, syslog.LOG_ALERT)
syslog.openlog('HGS relogger', syslog.LOG_USER)
log = open(sys.argv[1], 'ab', 0)
with open(sys.argv[2], 'rb', 0) as f:
try:
linebuf = []
while True:
time.sleep(0.5)
line = f.readline()
if not line.strip():
# skip blank lines
continue
# rest of this buffers up indented line to pass them all as one
# long call to syslog ... but you might want to skip all this
# and just blindly write each fragment:
if line[0] in ' \t':
linebuf.append(line)
else:
finished_line = ''.join(linebuf)
syslog.syslog(syslog.LOG_ALERT, finished_line)
## syslog.syslog(finished_line)
log.write(finished_line)
linebuf = [line]
except KeyboardInterrupt: # Exit cleanly, without stacktrace on SIGINT
# Remember to flush last (possibly partial) logged line buffer:
finished_line = ''.join(linebuf)
syslog.syslog(syslog.LOG_ALERT, finished_line)
log.write(finished_line)
# file implicitly by context manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment