Skip to content

Commit f47ebb3

Browse files
committed
Add file&line number to debug file log
1 parent cd9e1ad commit f47ebb3

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

nxc/logger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,16 @@ def log_console_to_file(self, text, *args, **kwargs):
163163
If debug or info logging is not enabled, we still want display/success/fail logged to the file specified,
164164
so we create a custom LogRecord and pass it to all the additional handlers (which will be all the file handlers)
165165
"""
166+
caller_frame = inspect.currentframe().f_back.f_back.f_back
166167
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
167168
try:
168169
for handler in self.logger.handlers:
169-
handler.handle(LogRecord("nxc", 20, "", kwargs, msg=text, args=args, exc_info=None))
170+
handler.handle(LogRecord("nxc", 20, pathname=caller_frame.f_code.co_filename, lineno=caller_frame.f_lineno, msg=text, args=args, exc_info=None))
170171
except Exception as e:
171172
self.logger.fail(f"Issue while trying to custom print handler: {e}")
172173

173174
def add_file_log(self, log_file=None):
174-
file_formatter = TermEscapeCodeFormatter("%(asctime)s - %(levelname)s - %(message)s")
175+
file_formatter = TermEscapeCodeFormatter("%(asctime)s | %(filename)s:%(lineno)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
175176
output_file = self.init_log_file() if log_file is None else log_file
176177
file_creation = False
177178

0 commit comments

Comments
 (0)