The Open vSwitch daemons written in C support user-configured logging patterns as described in ovs-appctl(8). This commit adds this capability to the daemons written in Python.
- Add a '__log_patterns' attribute to the Vlog class - Populate this using the default patterns in ovs-appctl(8) - Add a '__start_time' attribute to the Vlog class to support '%r' - Update the '_log' method to build the log message according to the pattern - Add a 'set_pattern' method to allow the default patterns to be changed - Update 'set_levels_from_string' to support setting the pattern from a string - Remove milliseconds from date/time format as not available in strftime Signed-off-by: Dave Tucker <d...@dtucker.co.uk> --- python/ovs/vlog.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++------ tests/vlog.at | 12 +++++- 2 files changed, 119 insertions(+), 14 deletions(-) diff --git a/python/ovs/vlog.py b/python/ovs/vlog.py index 478f08e..523d7d7 100644 --- a/python/ovs/vlog.py +++ b/python/ovs/vlog.py @@ -24,7 +24,15 @@ import ovs.dirs import ovs.unixctl import ovs.util +DEFAULT_FILE_PATTERN = "%D{%Y-%m-%dT%H:%M:%SZ}|%0N|%0c|%0p|%0m" +DEFAULT_SYSLOG_PATTERN = "%0N|%0c|%0p|%0m" + FACILITIES = {"console": "info", "file": "info", "syslog": "info"} +PATTERNS = { + "console": DEFAULT_FILE_PATTERN, + "file": DEFAULT_FILE_PATTERN, + "syslog": DEFAULT_SYSLOG_PATTERN +} LEVELS = { "dbg": logging.DEBUG, "info": logging.INFO, @@ -42,9 +50,11 @@ def get_level(level_str): class Vlog: __inited = False __msg_num = 0 + __start_time = 0 __mfl = {} # Module -> facility -> level __log_file = None __file_handler = None + __log_patterns = PATTERNS def __init__(self, name): """Creates a new Vlog object representing a module called 'name'. The @@ -60,22 +70,86 @@ class Vlog: if not Vlog.__inited: return - dt = datetime.datetime.utcnow(); - now = dt.strftime("%Y-%m-%dT%H:%M:%S.%%03iZ") % (dt.microsecond/1000) - syslog_message = ("%s|%s|%s|%s" - % (Vlog.__msg_num, self.name, level, message)) - - level = LEVELS.get(level.lower(), logging.DEBUG) + level_num = LEVELS.get(level.lower(), logging.DEBUG) + msg_num = Vlog.__msg_num Vlog.__msg_num += 1 for f, f_level in Vlog.__mfl[self.name].iteritems(): f_level = LEVELS.get(f_level, logging.CRITICAL) - if level >= f_level: - if f == "syslog": - message = "ovs|" + syslog_message - else: - message = "%s|%s" % (now, syslog_message) - logging.getLogger(f).log(level, message, **kwargs) + if level_num >= f_level: + msg = self._build_message(message, f, level, msg_num) + logging.getLogger(f).log(level_num, msg, **kwargs) + + def _build_message(self, message, facility, level, msg_num): + pattern = self.__log_patterns[facility] + tmp = pattern + + tmp = self._format_time(tmp) + + matches = re.findall("(%-?[0]?[0-9]?[AcmNnpPrtT])", tmp) + for m in matches: + if "A" in m: + #ToDo: Not sure how we can get the application name + tmp = self._format_field(tmp, m, "") + elif "c" in m: + tmp = self._format_field(tmp, m, self.name) + elif "m" in m: + tmp = self._format_field(tmp, m, message) + elif "N" in m: + tmp = self._format_field(tmp, m, str(msg_num)) + elif "n" in m: + tmp = re.sub(m, "\n", tmp) + elif "p" in m: + tmp = self._format_field(tmp, m, level.upper()) + elif "P" in m: + #ToDo: Not sure how we can get the process ID + self._format_field(tmp, m, "") + elif "r" in m: + now = datetime.datetime.utcnow() + delta = now - self.__start_time + ms = int(delta.microseconds * 0.001) + tmp = self._format_field(tmp, m, str(ms)) + elif "t" in m: + #ToDo: Not sure how we can get the sub-process name + tmp = self._format_field(tmp, m, "") + elif "T" in m: + #ToDo: Not sure how we can get the sub-process name + tmp = self._format_field(tmp, m, "") + return tmp + + def _format_field(self, tmp, match, replace): + formatting = re.compile("%(0)?([1-9])?") + matches = formatting.match(match) + # Do we need to apply padding? + if not matches.group(1): + replace = replace.center(len(replace)+2) + # Does the field have a minimum width + if matches.group(2): + min_width = int(matches.group(2)) + if len(replace) < min_width: + replace = replace.center(min_width) + return re.sub(match, replace, tmp) + + def _format_time(self, tmp): + date_regex = re.compile('(%([dD])(\{(.*)\})?)') + match = date_regex.search(tmp) + + if match is None: + return tmp + + # UTC date or Local TZ? + if match.group(2) == "d": + now = datetime.datetime.now() + elif match.group(2) == "D": + now = datetime.datetime.utcnow() + + # Custom format or ISO format? + if match.group(3): + time = datetime.date.strftime(now, match.group(4)) + else: + time = datetime.datetime.isoformat(now.replace(microsecond=0)) + + return re.sub(match.group(1), time, tmp) def emer(self, message, **kwargs): self.__log("EMER", message, **kwargs) @@ -130,6 +204,7 @@ class Vlog: return Vlog.__inited = True + Vlog.__start_time = datetime.datetime.utcnow() logging.raiseExceptions = False Vlog.__log_file = log_file for f in FACILITIES: @@ -191,12 +266,31 @@ class Vlog: Vlog.__mfl[m][f] = level @staticmethod + def set_pattern(facility, pattern): + """ Sets the log pattern of the 'facility' to 'pattern' """ + facility = facility.lower() + Vlog.__log_patterns[facility] = pattern + + @staticmethod def set_levels_from_string(s): module = None level = None facility = None - for word in [w.lower() for w in re.split('[ :]', s)]: + words = re.split('[ :]', s) + if words[0] == "pattern": + try: + if words[1] in FACILITIES and words[2]: + segments = [words[i] for i in range(2, len(words))] + pattern = "".join(segments) + Vlog.set_pattern(words[1], pattern) + return + else: + return "Facility %s does not exist" % words[1] + except IndexError: + return "Please supply a valid pattern and facility" + + for word in [w.lower() for w in words]: if word == "any": pass elif word in FACILITIES: @@ -260,6 +354,7 @@ class Vlog: def _unixctl_vlog_list(conn, unused_argv, unused_aux): conn.reply(Vlog.get_levels()) + def add_args(parser): """Adds vlog related options to 'parser', an ArgumentParser object. The resulting arguments parsed by 'parser' should be passed to handle_args.""" diff --git a/tests/vlog.at b/tests/vlog.at index 35336ce..eb70cdb 100644 --- a/tests/vlog.at +++ b/tests/vlog.at @@ -9,7 +9,7 @@ AT_CHECK([$PYTHON $srcdir/test-vlog.py --log-file log_file \ AT_CHECK([diff log_file stderr_log]) -AT_CHECK([sed -e 's/.*-.*-.*T..:..:..\....Z|//' \ +AT_CHECK([sed -e 's/.*-.*-.*T..:..:..Z|//' \ -e 's/File ".*", line [[0-9]][[0-9]]*,/File <name>, line <number>,/' \ stderr_log], [0], [dnl 0|module_0|EMER|emergency @@ -225,4 +225,14 @@ stream info info dbg test-unixctl info info dbg unixctl_server info info dbg ]) + +AT_CHECK([APPCTL -t test-unixctl.py vlog/set pattern], [0], + [Please supply a valid pattern and facility +]) +AT_CHECK([APPCTL -t test-unixctl.py vlog/set pattern:nonexistent], [0], + [Facility nonexistent does not exist +]) +AT_CHECK([APPCTL -t test-unixctl.py vlog/set pattern:file:'I<3OVS|%m']) +AT_CHECK([APPCTL -t test-unixctl.py log patterntest]) +AT_CHECK([grep -q 'I<3OVS' log]) AT_CLEANUP -- 1.9.1 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev