Package: debugtrace
DebugTrace-pyDebugTrace-pyis a library that outputs trace logs
when debugging your Python programs.
It supports Python 3.7 or later.
By embedding "_ = debugtrace.enter()" at the start of the method,
you can output the execution status of the program under development.1. FeaturesAutomatically outputs the method name, source file name and line number
of invokers ofdebugtrace.enterfunction.Also outputs end logs when the scope ends.Indents logs automatically with nested methods and objects.Automatically line breaks in value output.Uses reflection to output content even for objects of classes
that do not implement the__str__method.You can customize output contents by settingdebugtrace.inifile.You can select sys.stdout, sys.stderr or logging.Logger to output.2. Installpip install debugtrace3. How to useDo the following for the debug target and related functions and methods:Insert "_ = debugtrace.enter()" at the beginning of functions and methods.Insert "debugtrace.print('foo', foo)" to output variables to the log if necessary.The following is an example of a Python program using DebugTrace-py and a log when it is executed.# readme_example.py
  import datetime
  import debugtrace # TODO: Remove after debugging

  # Contact class
  class Contact(object):
      def __init__(self, id: int, firstName: str, lastName: str, birthday: datetime.date) -> None:
          _ = debugtrace.enter(self) # TODO: Remove after debugging
          self.id = id
          self.firstName = firstName
          self.lastName  = lastName
          self.birthday  = birthday

  def func2():
      _ = debugtrace.enter() # TODO: Remove after debugging
      contact = [
          Contact(1, 'Akane' , 'Apple', datetime.date(1991, 2, 3)),
          Contact(2, 'Yukari', 'Apple', datetime.date(1992, 3, 4))
      ]
      debugtrace.print('contact', contact) # TODO: Remove after debugging

  def func1():
      _ = debugtrace.enter() # TODO: Remove after debugging
      debugtrace.print('Hello, World!') # TODO: Remove after debugging
      func2()

  func1()Log output contents:2023-02-26 21:05:06.623919+0900 DebugTrace-py 1.3.0 on Python 3.11.0
2023-02-26 21:05:06.623973+0900   config file path: <No config file>
2023-02-26 21:05:06.623990+0900   logger: sys.stderr
2023-02-26 21:05:06.624044+0900 
2023-02-26 21:05:06.624091+0900 ______________________________ MainThread #140621580739648 ______________________________
2023-02-26 21:05:06.624106+0900 
2023-02-26 21:05:06.625608+0900 Enter func1 (readme_example.py:22) <- (readme_example.py:26)
2023-02-26 21:05:06.625701+0900 | Hello, World! (readme_example.py:23)
2023-02-26 21:05:06.625806+0900 | Enter func2 (readme_example.py:14) <- (readme_example.py:24)
2023-02-26 21:05:06.625902+0900 | | Enter Contact.__init__ (readme_example.py:7) <- (readme_example.py:16)
2023-02-26 21:05:06.625964+0900 | | Leave Contact.__init__ (readme_example.py:7) duration: 0:00:00.000008
2023-02-26 21:05:06.626055+0900 | | 
2023-02-26 21:05:06.626091+0900 | | Enter Contact.__init__ (readme_example.py:7) <- (readme_example.py:17)
2023-02-26 21:05:06.626123+0900 | | Leave Contact.__init__ (readme_example.py:7) duration: 0:00:00.000005
2023-02-26 21:05:06.627114+0900 | | 
2023-02-26 21:05:06.627155+0900 | | contacts = [
2023-02-26 21:05:06.627190+0900 | |   (__main__.Contact){
2023-02-26 21:05:06.627218+0900 | |     birthday: 1991-02-03, firstName: 'Akane', id: 1, lastName: 'Apple'
2023-02-26 21:05:06.627235+0900 | |   },
2023-02-26 21:05:06.627246+0900 | |   (__main__.Contact){
2023-02-26 21:05:06.627257+0900 | |     birthday: 1992-03-04, firstName: 'Yukari', id: 2, lastName: 'Apple'
2023-02-26 21:05:06.627279+0900 | |   }
2023-02-26 21:05:06.627314+0900 | | ] (readme_example.py:19)
2023-02-26 21:05:06.627348+0900 | | 
2023-02-26 21:05:06.627390+0900 | Leave func2 (readme_example.py:14) duration: 0:00:00.001537
2023-02-26 21:05:06.627430+0900 Leave func1 (readme_example.py:22) duration: 0:00:00.0017694. FunctionsThere are mainly the following functions.Function listNameDiscriptionenterOutputs an entering logAlso outputs a leaving log at the end of the code block.Arguments:invoker(object, optional): Pass theselforclsof the invoker.Examples:_ = debugtrace.enter(self)_ = debugtrace.enter(cls)_ = debugtrace.enter()printOutputs the variable name and valueArguments:name(str): Variable name, etcvalue(object, optional): Value to output (output only name if omitted)The following are keyword argumentsforce_reflection(bool, optional): IfTrue, outputs using reflection even if it has a__str__or__repr__method (default:False)output_private(bool, optional): IfTrue, also outputs private members when using reflection (default:False)output_method(bool, optional): IfTrue, also outputs method members when using reflection (default:False)collection_limit(int, optional): The limit value of elements such aslist,tupleanddictto output (default:None)bytes_limit(int, optional): The limit value of elements forbytesandbytearrayto output (default:None)string_limit(int, optional): The limit value of characters for string to output (default:None)reflection_nest_limit(int, optional): The The limit value for reflection nesting (default:None)Examples:debugtrace.print('Hellow')debugtrace.print('foo', foo)debugtrace.print('foo', foo, force_reflection=True)debugtrace.print('foos', foos, collection_limit=1024)5. Options that can be specified in thedebugtrace.inifileDebugTrace-py reads thedebugtrace.inifile
in the current directory for initialization.
The section is[debugtrace].You can specify the following options in thedebugtrace.inifile.listdebugtrace.iniOption NameDescriptionDefault ValueloggerThe logger used by debugtraceSpecifiable Values:stdout- Output tosys.stdoutstderr- Output tosys.stderrlogger- Output usingloggingpackagefile:< log file path> - Output directly to the filestderrlogging_config_fileThe configuration file name specified in logging packagelogging.conflogging_logger_nameThe logger name when using the logging packagedebugtraceis_enabledSpecifiable Values:False: Log output is disabledTrue: Log output is enabledTrueenter_formatThe format string of log output when entering functions or methods{0}: The function or method name{1}: The file name{2}: The line number{3}: The file name of the caller{4}: The line number of the callerEnter {0} ({1}:{2}) <- ({3}:{4})leave_formatThe format string of log output when leaving functions or methods{0}: The function or method name{1}: The file name{2}: The line number{3}: The time since enteredLeave {0} ({1}:{2}) duration: {3}thread_boundary_formatThe format string of logging at threads boundary{0}: The thread name{1}: The thread ID______________________________ {0} #{1} ______________________________maximum_indentsイThe maximum number of indents32indent_stringThe indentation string for code\sdata_indent_stringThe indentation string for data\s\slimit_stringThe string to represent that it has exceeded the limit...non_output_string(Currently unused)The string to be output instead of not outputting value...cyclic_reference_stringThe string to represent that the cyclic reference occurs*** Cyclic Reference ***varname_value_separatorThe separator string between the variable name and value\s=\skey_value_separatorThe separator string between the key and value of dictionary and between the attribute name and value:\sprint_suffix_formatThe format string ofprintmethod suffix\s({1}:{2})count_formatThe format string of the number of elements forlist,tupleanddictcount:{}minimum_output_countThe minimum value to output the number of elements forlist,tupleanddict16length_formatThe format string of the length of string andbyteslength:{}minimum_output_lengthThe minimum value to output the length of string andbytes16log_datetime_formatLog date and time format whenloggerisStdOutorStdErr%Y-%m-%d %H:%M:%S.%f%zmaximum_data_output_widthThe maximum output width of data70bytes_count_in_lineThe count in line ofbytes16collection_limitThe limit value of elements forlist,tupleanddictto output128string_limitThe limit value of characters for string to output256bytes_limitThe limit value of elements forbytesandbytearrayto output256reflection_nest_limitThe The limit value for reflection nesting4Converts\sto space.6. LicenseMIT License (MIT)© 2020 Masato Kokubo7. Release notesDebugTrace-py 1.3.0 - March 4, 2023Added calling source file name and line number to log output ofentermethod.Abolishedlogging_levelsetting indebugtrace.iniand set it to fixed (DEBUG).Addedlog_datetime_formattodebugtrace.inisetting item.DebugTrace-py 1.2.0 - August 15, 2022Added the runtime Python version to the startup log.Changed to output a log that shows thread switching.Changed default values for the following properties.Property NameNew Default ValueOld Default Valueminimum_output_count165minimum_output_length165collection_limit128512string_limit2568192bytes_limit2568192DebugTrace-py 1.1.0 - November 28, 2021Fixed a bug that an error occurs when outputting an object of a class that implements__str__or__repr__.Do not outputtuple,set,dictdata types.(1, 2, 3)←(tuple)(1, 2, 3)(1,)←(tuple)(1)()←(tuple)(){1, 2, 3}←(set){1, 2, 3}{}←(set){}{1: 'A', 2: 'B', 3; 'C'}←(dict){1: 'A', 2: 'B', 3; 'C'}{:}←(dict){}DebugTrace-py 1.0.3 - August 12, 2021Improved the line break handling of data outputDebugTrace-py 1.0.2 - November 29, 2020Change the start message. ('DebugTrace-py ...'<-'DebugTrace-python ...')DebugTrace-py 1.0.1 - July 19, 2020Improved the line break handling of data output.DebugTrace-py 1.0.0 - May 26, 2020First release