Logger

A basic logging class

Traceback


source

get_traceback

 get_traceback (root_module:str='<module>', num_stacks_to_drop=0,
                parent_class:str=None, debug_traceback:bool=False)

method that retrieves traceback

Type Default Details
root_module str
num_stacks_to_drop int 0 drop entries from the top of stack to exclude the functions that retrieve the traceback
parent_class str None
debug_traceback bool False
Returns TracebackDetails returns a filtered list of FrameSummaries from traceback

source

TracebackDetails

 TracebackDetails (traceback_stack:List[traceback.FrameSummary],
                   function_name:str=None, file_name:str=None,
                   function_trail:str=None, parent_class:str=None,
                   debug_traceback:bool=False)

*result of _get_traceback_details function*

sample implementation of TracebackDetails

class Foo:
    def __init__(self):
        pass

    def test_get_traceback_details(self, debug_traceback: bool = False):
        return get_traceback(parent_class=self.__class__.__name__, debug_traceback=debug_traceback)


# # print traceback details for test_get_details function
test_foo = Foo()

test_foo.test_get_traceback_details(debug_traceback=True).to_json()
{'len orig stack': 25, 'len filtered stack': 2, 'root_module_name': '<module>', 'root_module_index': 22, 'stacks_to_drop': 1}
{'parent_class': 'Foo', 'function_name': 'get_traceback', 'file_name': None}

source

Logger

 Logger (app_name:str, root_module:Optional[str]='<module>',
         output_fn:Optional[<built-infunctioncallable>]=None,
         entity_id:Optional[str]=None, domo_instance:Optional[str]=None)

log class with user customizeable output method

Type Default Details
app_name str name of the app for grouping logs
root_module Optional root module for stack trace
output_fn Optional None
entity_id Optional None function to call with write_logs method.
domo_instance Optional None

source

Logger.get_traceback

 Logger.get_traceback (root_module:str='<module>', num_stacks_to_drop=0,
                       parent_class:str=None)
Type Default Details
root_module str
num_stacks_to_drop int 0 drop entries from the top of stack to exclude the functions that retrieve the traceback
parent_class str None

sample implementations of stack tracing methods

# assert that the result of test_trace is of type FrameSummary
log = Logger(app_name="test traceback")


class Foo:
    logger: Logger

    def __init__(self):
        self.logger = Logger(app_name=self.__class__.__name__)

    def test_traceback(self):
        return self.logger.get_traceback().__dict__


test_foo = Foo()

test_foo.test_traceback()
{'traceback_stack': [<FrameSummary file /tmp/ipykernel_54263/2551220874.py, line 17 in <module>>,
  <FrameSummary file /tmp/ipykernel_54263/2551220874.py, line 12 in test_traceback>],
 'function_name': 'get_traceback',
 'file_name': None,
 'function_trail': '<module> -> test_traceback',
 'parent_class': 'Logger',
 'debug_traceback': False}

Logger logging methods


source

Logger.log_warning

 Logger.log_warning (message, entity_id:Optional[str]=None,
                     domo_instance:Optional[str]=None, debug_log=False,
                     num_stacks_to_drop=3)

log a warning message


source

Logger.log_error

 Logger.log_error (message, entity_id:Optional[str]=None,
                   domo_instance:Optional[str]=None, debug_log=False,
                   num_stacks_to_drop=3)

log an error message


source

Logger.log_info

 Logger.log_info (message, entity_id:Optional[str]=None,
                  domo_instance:Optional[str]=None, debug_log=False,
                  num_stacks_to_drop=3)

log an informational message

logger = Logger(
    app_name="test",
)


def test_log():
    return logger.log_info("test the error returns type Info", debug_log=False)


test_log()
adjusting num_stacks_to_drop, consider revising `get_traceback` call
{'stack_length': 28, 'module_index': 22, 'num_stacks_to_drop_passed': 5}
{'date_time': datetime.datetime(2024, 10, 22, 17, 32, 17, 591693),
 'application': 'test',
 'log_type': 'Info',
 'log_message': 'test the error returns type Info',
 'breadcrumb': '',
 'domo_instance': None,
 'entity_id': None,
 'function_name': 'get_traceback',
 'file_name': None,
 'function_trail': '<module> -> test_log'}

Outputting Logs

During Logger instantiation, users can pass a function, output_fn which will be called with the Logger.output_log method


source

Logger.output_log

 Logger.output_log ()

calls the user defined output function

Sample implementation with a custom write_logs method
import pandas as pd


def custom_write_logs_fn(logs):
    print("printing logs")
    return pd.DataFrame(logs)


logger = Logger(app_name="test", output_fn=custom_write_logs_fn)


def test_error():
    try:
        if 1 == 1:
            raise Exception("random error")

    except Exception as e:
        logger.log_error(e)


def double_test():
    test_error()


# record first error
test_error()

# records second error nested inside double_test()
double_test()

logger.output_log()
adjusting num_stacks_to_drop, consider revising `get_traceback` call
{'stack_length': 32, 'module_index': 26, 'num_stacks_to_drop_passed': 5}
printing logs
date_time application log_type log_message breadcrumb domo_instance entity_id function_name file_name function_trail
0 2025-03-11 23:23:37.182053 test Error random error None None get_traceback None <module> -> test_error
1 2025-03-11 23:23:37.182349 test Error random error None None get_traceback None <module> -> double_test