-#!/usr/bin/env python
+#!/usr/bin/env python3
# -*- coding: utf-8 -*-
+
"""
This module is the entrypoint of the `logparse` shell command and also contains
single-use functions which don't fit elsewhere. All user interaction with
- `rotate_sim()`: Simulate log rotation
"""
-import logging, logging.handlers
import argparse
+from copy import copy
+import logging
+import logging.handlers
import os
-from sys import stdin, version
+from sys import exit, stdin, version
from subprocess import check_output
from datetime import datetime
import logparse
-from logparse import formatting, mail, config, load_parsers
+from logparse import formatting, mail, config, load_parsers, util
def main():
argparser.parse_args().time_period)
# Set up logging
-
logger = logging.getLogger(__name__)
- loghandler = logging.handlers.SysLogHandler(address = '/dev/log')
- loghandler.setFormatter(logging.Formatter(
- fmt='logparse[' + str(os.getpid()) + ']: %(message)s'))
- loghandler.setLevel(logging.INFO) # don't spam syslog with debug messages
-
if (argparser.parse_args().quiet
or config.prefs.getboolean("logparse", "quiet")):
- logging.basicConfig(level=logging.CRITICAL)
+ logparse.logger.setLevel(logging.CRITICAL)
elif (argparser.parse_args().verbose
or config.prefs.getboolean("logparse", "verbose")):
- logging.basicConfig(level=logging.DEBUG)
+ logparse.logger.setLevel(logging.DEBUG)
logger.debug("Verbose mode turned on")
else:
- logging.basicConfig(level=logging.INFO)
+ logparse.logger.setLevel(logging.INFO)
+
- logger.addHandler(loghandler)
# Time analysis
# Set up parsers
loader = load_parsers.ParserLoader()
+
+ try:
+ loader.check_systemd()
+ except Exception as e:
+ logger.error("Failed to check systemd dependencies: ".format(e))
+
if parser_names:
for parser_name in parser_names:
if parser_name not in ignore_logs:
# Execute parsers
+ executed_parsers = []
+
for parser in loader.parsers:
- output.append_section(parser.parse_log())
+ if (argparser.parse_args().verbose
+ or config.prefs.getboolean("logparse", "verbose")):
+ output.append_section(parser.parse_log())
+
+ else:
+ try:
+ output.append_section(parser.parse_log())
+ except Exception as e:
+ logger.error("Uncaught error executing logger {0}: {1}".format(
+ parser.name, e))
+ executed_parsers.append(parser.name)
+
+ if len(executed_parsers) == 0:
+ exit()
# Write footer
output.append_footer()
return
+
def get_argparser():
"""
Initialise arguments (in a separate function for documentation purposes)