Commit d6103181 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

separate formatting for subprocesses

parent 4db490de
......@@ -5,12 +5,14 @@
import sys
import os
import logging
import logging.handlers
import itertools
import subprocess
import socket
import atexit
import datetime
import StringIO
import inspect
import OperationalMail
......@@ -47,7 +49,7 @@ class OperationalLogger:
"""Main class for Operational Logging."""
def __init__(self, log_file='log.txt', log_level=logging.DEBUG,
log_format=None, date_format=None, from_address=None, logger_name=None, console_level=logging.DEBUG):
log_format=None, date_format=None, from_address=None, logger_name='', console_level=logging.DEBUG):
"""Initialize logging handlers, formats, etc."""
self.__file_name = log_file if logger_name is None else logger_name
self.__server_name = socket.getfqdn()
......@@ -66,6 +68,15 @@ class OperationalLogger:
logging.basicConfig(filename=log_file, format=self.__log_format, datefmt=self.__date_format, level=log_level)
formatter = logging.Formatter(fmt=self.__log_format, datefmt='%m/%d %H:%M:%S')
formatter_sub = logging.Formatter(
fmt='[%(asctime)s][%(levelname)5s][p%(process)d][%(parentname)s:%(parentline)d] %(message)s',
datefmt='%H:%M:%S'
)
sub_handler = logging.handlers.WatchedFileHandler(log_file)
sub_handler.setLevel(logging.INFO)
sub_handler.setFormatter(formatter_sub)
logging.getLogger(logger_name + '_sub').addHandler(sub_handler)
logging.getLogger(logger_name + '_sub').propagate = False
full_trigger = logging.StreamHandler(self.__full_buffer)
full_trigger.setLevel(logging.DEBUG)
......@@ -91,6 +102,11 @@ class OperationalLogger:
))
logging.getLogger(logger_name).addHandler(console)
sub_handler_console = logging.StreamHandler(sys.stdout)
sub_handler_console.setLevel(logging.INFO)
sub_handler_console.setFormatter(formatter_sub)
logging.getLogger(logger_name + '_sub').addHandler(sub_handler_console)
def has_errors(self):
"""Return whether any ERROR-level log statements in buffer."""
return len(self.__error_buffer.getvalue()) > 0
......@@ -220,11 +236,17 @@ def logged_sub(arg_list, logging_level=logging.INFO, **kwargs):
logging.info('Attempting {}'.format(' '.join(arg_list)))
try:
#results = ''
my_name = os.path.basename(inspect.getfile(inspect.currentframe()))
for f in inspect.stack():
if my_name != os.path.basename(f[1]):
parent_name, line_num = os.path.basename(f[1]), f[2]
break
proc = subprocess.Popen(arg_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, **kwargs)
for line in iter(proc.stdout.readline, b''):
if not line:
break
logging.info(" :: " + line.rstrip())
logging.getLogger('_sub').info(" :: " + line.rstrip(), extra={'parentname':parent_name, 'parentline':line_num})
#results += line
return_code = proc.wait()
if return_code:
......@@ -260,9 +282,9 @@ if __name__ == '__main__':
logging.critical('Put up the Jimsignal!')
#long_error(test)
logging.error(test)
c = ['find', '.', '-name', '*.py']
#c = ['find', '.', '-name', '*.py']
c = ['diff', 'a', 'b']
logged_subprocess(c)
#logged_subprocess(c)
logged_sub(c)
try:
raise Exception("I AM ERROR.")
......
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