WIP: issue with logging to --log file (it doesnt output to console as well)
parent
47cf9c9264
commit
e30d629d73
|
@ -13,3 +13,4 @@ except configparser.NoSectionError:
|
|||
first_run_setup(cme_logger)
|
||||
|
||||
cme_workspace = cme_config.get("CME", "workspace", fallback="default")
|
||||
config_log = cme_config.getboolean("CME", "log_mode", fallback=False)
|
||||
|
|
|
@ -15,7 +15,7 @@ from cme.context import Context
|
|||
from cme.paths import CME_PATH, DATA_PATH
|
||||
from cme.console import cme_console
|
||||
from cme.logger import cme_logger
|
||||
from cme.config import cme_config, cme_workspace
|
||||
from cme.config import cme_config, cme_workspace, config_log
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import asyncio
|
||||
import cme.helpers.powershell as powershell
|
||||
|
@ -86,6 +86,20 @@ def main():
|
|||
cme_logger.logger.setLevel(logging.ERROR)
|
||||
root_logger.setLevel(logging.ERROR)
|
||||
|
||||
cme_logger.debug(f"handlers: {logging.getLogger('rich').handlers}")
|
||||
# if these are the same, it will double log to file (two FileHandlers will be added)
|
||||
cme_logger.debug(f"File log: {config_log}")
|
||||
if config_log:
|
||||
cme_logger.add_file_log()
|
||||
cme_logger.debug(f"handlers: {logging.getLogger('rich').handlers}")
|
||||
if args.log:
|
||||
cme_logger.debug(f"args.log: {args.log}")
|
||||
cme_logger.debug(f"args.log type: {type(args.log)}")
|
||||
cme_logger.add_file_log(args.log)
|
||||
cme_logger.debug(f"handlers: {logging.getLogger('rich').handlers}")
|
||||
|
||||
cme_logger.debug(f"handlers: {logging.getLogger('rich').handlers}")
|
||||
|
||||
cme_logger.debug(f"Passed args: {args}")
|
||||
|
||||
if args.darrell:
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
import os.path
|
||||
import sys
|
||||
|
@ -11,23 +10,6 @@ from termcolor import colored
|
|||
from datetime import datetime
|
||||
from rich.text import Text
|
||||
from rich.logging import RichHandler
|
||||
from rich.traceback import install
|
||||
|
||||
# The following hooks the FileHandler.emit function to remove ansi chars before logging to a file
|
||||
# There must be a better way of doing this, but this way we might save some penguins!
|
||||
ansi_escape = re.compile(r'\x1b[^m]*m')
|
||||
|
||||
|
||||
def antiansi_emit(self, record):
|
||||
|
||||
if self.stream is None:
|
||||
self.stream = self._open()
|
||||
|
||||
record.msg = ansi_escape.sub('', record.message)
|
||||
logging.StreamHandler.emit(self, record)
|
||||
|
||||
|
||||
logging.FileHandler.emit = antiansi_emit
|
||||
|
||||
|
||||
class CMEAdapter(logging.LoggerAdapter):
|
||||
|
@ -42,7 +24,7 @@ class CMEAdapter(logging.LoggerAdapter):
|
|||
)
|
||||
self.logger = logging.getLogger("rich")
|
||||
self.extra = extra
|
||||
self.outputfile = None
|
||||
self.output_file = None
|
||||
|
||||
logging.getLogger("pypykatz").disabled = True
|
||||
logging.getLogger("minidump").disabled = True
|
||||
|
@ -136,26 +118,48 @@ class CMEAdapter(logging.LoggerAdapter):
|
|||
text = Text.from_ansi(msg)
|
||||
cme_console.print(text, *args, **kwargs)
|
||||
|
||||
def setup_logfile(self, log_file=None):
|
||||
formatter = logging.Formatter("%(message)s")
|
||||
self.outputfile = init_log_file() if log_file is None else log_file
|
||||
def add_file_log(self, log_file=None):
|
||||
file_formatter = TermEscapeCodeFormatter("%(asctime)s - %(levelname)s - %(message)s")
|
||||
output_file = self.init_log_file() if log_file is None else log_file
|
||||
file_creation = False
|
||||
if not os.path.isfile(self.outputfile):
|
||||
open(self.outputfile, 'x')
|
||||
|
||||
if not os.path.isfile(output_file):
|
||||
open(output_file, 'x')
|
||||
file_creation = True
|
||||
file_handler = logging.FileHandler(filename=self.outputfile, mode="a")
|
||||
|
||||
file_handler = logging.FileHandler(filename=output_file, mode="a")
|
||||
|
||||
with file_handler._open() as f:
|
||||
if file_creation:
|
||||
f.write("[%s]> %s\n\n" % (datetime.now().strftime('%d-%m-%Y %H:%M:%S'), " ".join(sys.argv)))
|
||||
else:
|
||||
f.write("\n[%s]> %s\n\n" % (datetime.now().strftime('%d-%m-%Y %H:%M:%S'), " ".join(sys.argv)))
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
file_handler.setFormatter(file_formatter)
|
||||
self.logger.addHandler(file_handler)
|
||||
|
||||
|
||||
def init_log_file():
|
||||
log_filename = os.path.join(os.path.expanduser('~/.cme'), 'logs', 'full-log_{}.log'.format(datetime.now().strftime('%Y-%m-%d')))
|
||||
return log_filename
|
||||
@staticmethod
|
||||
def init_log_file():
|
||||
log_filename = os.path.join(
|
||||
os.path.expanduser(
|
||||
"~/.cme"
|
||||
),
|
||||
"logs",
|
||||
f"full-log_{datetime.now().strftime('%Y-%m-%d')}.log"
|
||||
)
|
||||
return log_filename
|
||||
|
||||
|
||||
class TermEscapeCodeFormatter(logging.Formatter):
|
||||
"""A class to strip the escape codes from the """
|
||||
def __init__(self, fmt=None, datefmt=None, style='%', validate=True):
|
||||
super().__init__(fmt, datefmt, style, validate)
|
||||
|
||||
def format(self, record):
|
||||
escape_re = re.compile(r'\x1b\[[0-9;]*m')
|
||||
record.msg = re.sub(escape_re, "", str(record.msg))
|
||||
return super().format(record)
|
||||
|
||||
|
||||
# initialize the logger for all of CME - this is imported everywhere
|
||||
cme_logger = CMEAdapter()
|
||||
|
|
Loading…
Reference in New Issue