Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/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
## ## 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:
linebuf = []
while True:
line = f.readline()
if not line.strip():
# skip blank lines
# 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':
finished_line = ''.join(linebuf)
syslog.syslog(syslog.LOG_ALERT, finished_line)
## syslog.syslog(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)
# file implicitly by context manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment