Skip to content

Commit aef2675

Browse files
authored
Merge pull request Pennyw0rth#406 from Pennyw0rth/neff-fix-mssql-log
Fix file logging for display messages
2 parents 04a695f + ff194d3 commit aef2675

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

nxc/logger.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import re
77
from nxc.console import nxc_console
8+
from nxc.paths import NXC_PATH
89
from termcolor import colored
910
from datetime import datetime
1011
from rich.text import Text
@@ -30,7 +31,7 @@ def setup_debug_logging():
3031
root_logger.setLevel(logging.INFO)
3132
elif debug_args.debug:
3233
nxc_logger.logger.setLevel(logging.DEBUG)
33-
root_logger.setLevel(logging.INFO)
34+
root_logger.setLevel(logging.DEBUG)
3435
else:
3536
nxc_logger.logger.setLevel(logging.ERROR)
3637
root_logger.setLevel(logging.ERROR)
@@ -163,15 +164,16 @@ def log_console_to_file(self, text, *args, **kwargs):
163164
If debug or info logging is not enabled, we still want display/success/fail logged to the file specified,
164165
so we create a custom LogRecord and pass it to all the additional handlers (which will be all the file handlers)
165166
"""
166-
if self.logger.getEffectiveLevel() >= logging.INFO and len(self.logger.handlers): # will be 0 if it's just the console output, so only do this if we actually have file loggers
167+
caller_frame = inspect.currentframe().f_back.f_back.f_back
168+
if len(self.logger.handlers): # will be 0 if it's just the console output, so only do this if we actually have file loggers
167169
try:
168170
for handler in self.logger.handlers:
169-
handler.handle(LogRecord("nxc", 20, "", kwargs, msg=text, args=args, exc_info=None))
171+
handler.handle(LogRecord("nxc", 20, pathname=caller_frame.f_code.co_filename, lineno=caller_frame.f_lineno, msg=text, args=args, exc_info=None))
170172
except Exception as e:
171173
self.logger.fail(f"Issue while trying to custom print handler: {e}")
172174

173175
def add_file_log(self, log_file=None):
174-
file_formatter = TermEscapeCodeFormatter("%(asctime)s - %(levelname)s - %(message)s")
176+
file_formatter = TermEscapeCodeFormatter("%(asctime)s | %(filename)s:%(lineno)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
175177
output_file = self.init_log_file() if log_file is None else log_file
176178
file_creation = False
177179

@@ -193,11 +195,10 @@ def add_file_log(self, log_file=None):
193195

194196
@staticmethod
195197
def init_log_file():
196-
newpath = os.path.expanduser("~/.nxc") + "/logs/" + datetime.now().strftime("%Y-%m-%d")
197-
if not os.path.exists(newpath):
198-
os.makedirs(newpath)
198+
newpath = NXC_PATH + "/logs/" + datetime.now().strftime("%Y-%m-%d")
199+
os.makedirs(newpath, exist_ok=True)
199200
return os.path.join(
200-
os.path.expanduser("~/.nxc"),
201+
NXC_PATH,
201202
"logs",
202203
datetime.now().strftime("%Y-%m-%d"),
203204
f"log_{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.log",

0 commit comments

Comments
 (0)