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 @@ ...@@ -2,7 +2,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals from __future__ import absolute_import, division, print_function, unicode_literals
import etcd import etcd
import argparse 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 from scvmm_client import odata
import logging import logging
import uuid import uuid
...@@ -33,9 +33,8 @@ def main(): ...@@ -33,9 +33,8 @@ def main():
conf = SafeConfigParser() conf = SafeConfigParser()
conf.read(args.config) conf.read(args.config)
if args.logfile: if args.logfile:
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO)
filename=args.logfile, setupLogfile(args.logfile)
format='%(asctime)s %(name)s %(levelname)s %(message)s')
else: else:
logging.basicConfig(level=logging.INFO, logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)s %(levelname)s %(message)s') format='%(asctime)s %(name)s %(levelname)s %(message)s')
......
...@@ -9,6 +9,7 @@ import argparse ...@@ -9,6 +9,7 @@ import argparse
from daemonize import Daemonize from daemonize import Daemonize
import socket import socket
import json import json
from nova_router import setupLogfile
CONF_FILE = "/etc/haproxy/haproxy.cfg" CONF_FILE = "/etc/haproxy/haproxy.cfg"
APP = "nova_dhcp_configurator" APP = "nova_dhcp_configurator"
...@@ -34,10 +35,7 @@ class Generator(object): ...@@ -34,10 +35,7 @@ class Generator(object):
self.etcd_client = etcd.Client(host=etcd_host, cert=(cert, key), self.etcd_client = etcd.Client(host=etcd_host, cert=(cert, key),
ca_cert=cacert, protocol='https') ca_cert=cacert, protocol='https')
if logfile: if logfile:
handler = logging.handlers.RotatingFileHandler(logfile, setupLogfile(logfile)
maxBytes=10*1024**3, backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
logging.info("Process starting") logging.info("Process starting")
......
...@@ -8,7 +8,7 @@ import time ...@@ -8,7 +8,7 @@ import time
import argparse import argparse
from daemonize import Daemonize from daemonize import Daemonize
import tempfile import tempfile
from nova_router import security_groups from nova_router import security_groups, setupLogfile
APP = "nova_sg_configurator" APP = "nova_sg_configurator"
...@@ -38,15 +38,11 @@ def call(cmd, stdin): ...@@ -38,15 +38,11 @@ def call(cmd, stdin):
class Generator(object): class Generator(object):
def __init__(self, cert, key, cacert, logfile=None, noop=False, etcd_host="localhost"): def __init__(self, cert, key, cacert, logfile=None, noop=False, etcd_host="localhost"):
self.noop = noop self.noop = noop
self.etcd_client = etcd.Client(host=etcd_host, cert=(cert, key),
ca_cert=cacert, protocol='https')
if logfile: if logfile:
handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=10*1024**3, setupLogfile(logfile)
backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
logging.info("Process starting") 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 # maps security group id from etcd to the name of a ipset containing its members
self.group_members_groups = {} self.group_members_groups = {}
...@@ -311,12 +307,11 @@ def parse_args(): ...@@ -311,12 +307,11 @@ def parse_args():
return parser.parse_args() return parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG, logging.basicConfig(level=logging.DEBUG)
format='%(asctime)s %(name)s %(levelname)s %(message)s')
logging.getLogger("urllib3").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING)
args = parse_args() args = parse_args()
if args.daemonize: if args.daemonize:
daemon = Daemonize(app=APP, pid=args.pidfile, action=lambda: daemon_main(args)) daemon = Daemonize(app=APP, pid=args.pidfile, action=lambda: daemon_main(args))
daemon.start() daemon.start()
else: 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") ...@@ -13,7 +13,7 @@ log = logging.getLogger("neighbors")
# #
from pyroute2.netlink.iproute import IPRoute from pyroute2.netlink.iproute import IPRoute
import etcd import etcd
from nova_router import neighbors from nova_router import neighbors, setupLogfile
APP = "nova_neighbor_monitor" APP = "nova_neighbor_monitor"
...@@ -120,10 +120,7 @@ class NeighborCache(object): ...@@ -120,10 +120,7 @@ class NeighborCache(object):
def main(args): def main(args):
logging.getLogger("urllib3").setLevel(logging.INFO) logging.getLogger("urllib3").setLevel(logging.INFO)
if args.daemonize: if args.daemonize:
handler = logging.handlers.RotatingFileHandler(args.logfile, maxBytes=10*1024**3, setupLogfile(args.logfile)
backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
logging.info("Process starting") logging.info("Process starting")
......
...@@ -6,6 +6,7 @@ import subprocess ...@@ -6,6 +6,7 @@ import subprocess
import logging import logging
import argparse import argparse
from daemonize import Daemonize from daemonize import Daemonize
from nova_router import setupLogfile
APP = "nova_bfd_monitor" APP = "nova_bfd_monitor"
DESCRIPTION = "Check that everything is ok, and signal link down via bfd otherwise" DESCRIPTION = "Check that everything is ok, and signal link down via bfd otherwise"
...@@ -38,10 +39,7 @@ def main(logfile, interface): ...@@ -38,10 +39,7 @@ def main(logfile, interface):
format='%(asctime)s %(name)s %(levelname)s %(message)s') format='%(asctime)s %(name)s %(levelname)s %(message)s')
logging.getLogger("requests").setLevel(logging.WARNING) logging.getLogger("requests").setLevel(logging.WARNING)
if logfile: if logfile:
handler = logging.handlers.RotatingFileHandler(logfile, setupLogfile(logfile)
maxBytes=10*1024**3, backupCount=5)
handler.setFormatter(logging.getLogger("").handlers[0].formatter)
logging.getLogger("").addHandler(handler)
logging.info("Process starting") 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