Skip to content

Instantly share code, notes, and snippets.

@ajpen
Created April 10, 2018 21:23
Show Gist options
  • Select an option

  • Save ajpen/342e7624f8416426df2d3466c12b0ab5 to your computer and use it in GitHub Desktop.

Select an option

Save ajpen/342e7624f8416426df2d3466c12b0ab5 to your computer and use it in GitHub Desktop.
Separates Supervisor logs by process and passes them to handlers specified by a python's logging module configuration file.
[formatter_form01]
format=F1 %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter
[handler_hand01]
class=pygelf.GelfHttpHandler
level=NOTSET
args=('dockerhostip', 12201, False)
[formatters]
keys=form01
[loggers]
keys=root, webserver
[handlers]
keys=hand01
[logger_root]
level=DEBUG
handlers=hand01
[logger_webserver]
level=DEBUG
handlers=hand01
propagate=0
qualname=webserver
[program:webserver]
command=python -m SimpleHTTPServer 8000
redirect_stderr=true
stdout_events_enabled=true
[eventlistener:supervisor_log_event_handler.py]
command=supervisor_log_event_handler.py -c log_configuration_sample.conf -x
events=PROCESS_LOG
#!/usr/bin/python
import sys
import logging
import logging.config
import logging.handlers
import argparse
def configure(filepath, disable_existing=False):
logging.config.fileConfig(
filepath,
disable_existing_loggers=disable_existing
)
def configure_from_cmd():
cmd_parser = argparse.ArgumentParser(
description='Aggrigate STDOUT/STDERR events with the flexibility of the python logging module.'
)
cmd_parser.add_argument(
'-c',
nargs=1,
required=True,
type=str,
help="Path to the configuration file"
)
cmd_parser.add_argument(
'-x',
action='store_true',
help='Disable existing loggers'
)
args = cmd_parser.parse_args()
configure(args.c, args.x)
def write_stdout(s):
sys.stdout.write(s)
sys.stdout.flush()
def run():
while True:
write_stdout("READY\n")
# Get headers and parse
line = sys.stdin.readline()
headers = dict([x.split(":") for x in line.split()])
log = sys.stdin.read(int(headers["len"]))
# Log to process logger
header_and_log = log.split('\n')
payload_headers = dict(
[x.split(":") for x in header_and_log[0].split()]
)
p_logger = logging.getLogger(payload_headers["processname"])
p_logger.debug(header_and_log[1])
write_stdout("RESULT 2\nOK")
def main():
configure_from_cmd()
run()
if __name__ == "__main__":
try:
main()
except:
import traceback
traceback.print_exc()
sys.exit(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment