Commit cbe6d84b authored by Sigmund Augdal's avatar Sigmund Augdal

Refactor code to configure log file.

Use a custom formatter that will output multiline log records in a format suitable for logstash
parent f718cd87
......@@ -2,7 +2,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import etcd
import argparse
from nova_router import neighbors, security_groups, dns_rr
from nova_router import neighbors, security_groups, dns_rr, setupLogfile
from scvmm_client import odata
import logging
import uuid
......@@ -33,9 +33,8 @@ def main():
conf = SafeConfigParser()
conf.read(args.config)
if args.logfile:
logging.basicConfig(level=logging.INFO,
filename=args.logfile,
format='%(asctime)s %(name)s %(levelname)s %(message)s')
logging.basicConfig(level=logging.INFO)
setupLogfile(args.logfile)
else:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)s %(levelname)s %(message)s')
......
......@@ -9,6 +9,7 @@ import argparse
from daemonize import Daemonize
import socket
import json
from nova_router import setupLogfile
CONF_FILE = "/etc/haproxy/haproxy.cfg"
APP = "nova_dhcp_configurator"
......@@ -34,10 +35,7 @@ class Generator(object):
self.etcd_client = etcd.Client(host=etcd_host, cert=(cert, key),
ca_cert=cacert, protocol='https')
if logfile:
handler = logging.handlers.RotatingFileHandler(logfile,
maxBytes=10*1024**3, backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
setupLogfile(logfile)
logging.info("Process starting")
......
......@@ -8,7 +8,7 @@ import time
import argparse
from daemonize import Daemonize
import tempfile
from nova_router import security_groups
from nova_router import security_groups, setupLogfile
APP = "nova_sg_configurator"
......@@ -38,15 +38,11 @@ def call(cmd, stdin):
class Generator(object):
def __init__(self, cert, key, cacert, logfile=None, noop=False, etcd_host="localhost"):
self.noop = noop
self.etcd_client = etcd.Client(host=etcd_host, cert=(cert, key),
ca_cert=cacert, protocol='https')
if logfile:
handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10*1024**3,
backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
setupLogfile(logfile)
logging.info("Process starting")
self.etcd_client = etcd.Client(host=etcd_host, cert=(cert, key),
ca_cert=cacert, protocol='https')
# maps security group id from etcd to the name of a ipset containing its members
self.group_members_groups = {}
......@@ -311,12 +307,11 @@ def parse_args():
return parser.parse_args()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)s %(levelname)s %(message)s')
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("urllib3").setLevel(logging.WARNING)
args = parse_args()
if args.daemonize:
daemon = Daemonize(app=APP, pid=args.pidfile, action=lambda: daemon_main(args))
daemon.start()
else:
Generator(args.cert, args.key, args.cacert, noop=args.noop, etcd_host=args.etcd_host).main()
Generator(args.cert, args.key, args.cacert, noop=args.noop, logfile=args.logfile, etcd_host=args.etcd_host).main()
......@@ -13,7 +13,7 @@ log = logging.getLogger("neighbors")
#
from pyroute2.netlink.iproute import IPRoute
import etcd
from nova_router import neighbors
from nova_router import neighbors, setupLogfile
APP = "nova_neighbor_monitor"
......@@ -120,10 +120,7 @@ class NeighborCache(object):
def main(args):
logging.getLogger("urllib3").setLevel(logging.INFO)
if args.daemonize:
handler = logging.handlers.RotatingFileHandler(args.logfile, maxBytes=10*1024**3,
backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
setupLogfile(args.logfile)
logging.info("Process starting")
......
......@@ -6,6 +6,7 @@ import subprocess
import logging
import argparse
from daemonize import Daemonize
from nova_router import setupLogfile
APP = "nova_bfd_monitor"
DESCRIPTION = "Check that everything is ok, and signal link down via bfd otherwise"
......@@ -38,10 +39,7 @@ def main(logfile, interface):
format='%(asctime)s %(name)s %(levelname)s %(message)s')
logging.getLogger("requests").setLevel(logging.WARNING)
if logfile:
handler = logging.handlers.RotatingFileHandler(logfile,
maxBytes=10*1024**3, backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
setupLogfile(logfile)
logging.info("Process starting")
......
import logging
FORMAT = '%(asctime)s %(name)s %(levelname)s %(message)s'
class LogStashFormatter(logging.Formatter):
def __init__(self, fmt=None):
if not fmt:
fmt = FORMAT
logging.Formatter.__init__(self, fmt)
def format(self, record):
indent = " "
msg = logging.Formatter.format(self, record).split("\n")
return "\n".join([msg[0]] + [indent + l for l in msg[1:]])
def setupLogfile(logfile):
handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10*1024**3,
backupCount=5)
formatter = LogStashFormatter()
handler.setFormatter(formatter)
logging.getLogger("").addHandler(handler)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment