Package: yogger
YoggerYoggerprovides a minimal logging setup with utilities to represent interpreter stacks.Supportsrequests.Requestandrequests.Responseobjects if theRequestspackage is installed.Example of common usage:importloggingimportyoggerlogger=logging.getLogger(__name__)defmain():yogger.install()yogger.configure(__name__,verbosity=2,dump_locals=True)withyogger.dump_on_exception():# Code goes hereif__name__=="__main__":main()Requirements:Yoggerrequires Python 3.10 or higher, is platform independent, and has no outside dependencies.Issue reportingIf you discover an issue with Yogger, please report it athttps://github.com/Phosmic/yogger/issues.LicenseThis program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program. If not, seehttps://www.gnu.org/licenses/.RequirementsInstallingUsageLibraryInstallingMost stable version fromPyPi:python3-mpipinstallyoggerDevelopment version fromGitHub:gitclonegit+https://github.com/Phosmic/yogger.gitcdyogger
python3-mpipinstall-e.UsageImport packages and instantiate a logger:importloggingimportyoggerlogger=logging.getLogger(__name__)Install the logger class and configure with your package name:Place at the start of the top-level function.defmain():yogger.install()yogger.configure(__name__)# Code goes hereOutputExample of logger output:[ 2023-01-17 10:16:09.0918INFOmy_package ]  Something we want to log.
[ 2023-01-17 10:16:09.0918DEBUGmy_package ]  Something we want to log.
[ 2023-01-17 10:16:09.0918WARNINGmy_package ]  Something we want to log.
[ 2023-01-17 10:16:09.0918ERRORmy_package ]  Something we want to log.`
[ 2023-01-17 10:16:09.0918CRITICALmy_package ]  Something we want to log.Note: Support for rich text has not yet been added for Windows platforms.Support for dumping the stackTraces and exceptionsUsing thedump_on_exceptioncontext managerdumps the exception and trace if an exception is raised:withyogger.dump_on_exception(# Uncomment to override# dump_path="./stack_dump.txt",):raiseSomeExceptionThis is nearly equivalent to:importinspecttry:raiseSomeExceptionexceptExceptionase:trace=inspect.trace()iflen(trace)>1:withopen("./stack_dump.txt",mode="a",encoding="utf-8")asf:yogger.dump(f,trace[1:],e=e,package_name="my_package")StacksSettingdump_locals=Truewhen runningyogger.configuredumps a representation of the caller's stack upon logging with a level of warning or higher.To manually dump the stack, something like this would suffice:importinspectstack=inspect.stack()iflen(stack)>2:withopen("./example.log",mode="w",encoding="utf-8")asf:yogger.dump(f,stack[2:][::-1])If you simply want the string representation, use theyogger.dumpsfunction:stack=inspect.stack()iflen(stack)>2:trace_repr=yogger.dumps(stack[2:][::-1])OutputExample of dictionary representation in dump:example={"user_id":123456790,"profile":{"name":"John Doe","birthdate":datetime.date(2000,1,1),"weight_kg":86.18,},"video_ids":[123,456,789],}example = <builtins.dict>
  example['user_id'] = 123456790
  example['profile'] = <builtins.dict>
    example['profile']['name'] = 'John Doe'
    example['profile']['birthdate'] = datetime.date(2000, 1, 1)
    example['profile']['weight_kg'] = 86.18
  example['video_ids'] = [123, 456, 789]Similarly for a dataclass:@dataclasses.dataclassclassExample:user_id:intprofile:dict[str,str|float|datetime.date]video_ids:list[int]example = <my_package.Example>
  example.user_id = 'user_id' = example.user_id = 123456790
  example.profile = 'profile' = example.profile = <builtins.dict>
    example.profile['name'] = 'John Doe'
    example.profile['birthdate'] = datetime.date(2000, 1, 1)
    example.profile['weight_kg'] = 86.18
  example.video_ids = 'video_ids' = example.video_ids = [123, 456, 789]LibraryAbout thepackage_nameparameterThepackage_nameparameter gives Yogger an idea of what belongs to your application. This name is used to identify which frames to dump in the stack. So it’s important what you provide there. If you are using a single module,__name__is always the correct value. If you are using a package, it’s usually recommended to hardcode the name of your package there.For example, if your application is defined in "my_package/app.py", you should create it with one of the two versions below:yogger.configure("my_package")yogger.configure(__name__.split(".")[0])Why is that? The application will work even with__name__, thanks to how resources are looked up. However, it will make debugging more painful. Yogger makes assumptions based on the import name of your application. If the import name is not properly set up, that debugging information may be lost.yogger.pformatCreate a formatted representation of a variable's name and value.This module is used to create a formatted representation of a variable's name and value.pformatdefpformat(name:str,value:Any,outer_line_continuation:bool=True)->strCreate a formatted representation of a variable's name and value.Arguments:namestr- Name of the variable to represent.valueAny- Value to represent.outer_line_continuationbool, optional- Whether the outermost representation should be line continued. Defaults to True.Returns:str- Formatted representation of a variable's name and value.yogger.baseYogger Base ModuleThis module contains the base classes and functions for Yogger.YoggerObjectsclassYogger(logging.Logger)Yogger Logger ClassThis class is used to override the defaultlogging.Loggerclass.installdefinstall()->NoneInstall the Yogger Logger Class and Instantiate the Global Loggerconfiguredefconfigure(package_name:str,*,verbosity:int=0,dump_locals:bool=False,dump_path:str|bytes|os.PathLike|None=None,remove_handlers:bool=True)->NonePrepare for LoggingArguments:package_namestr- Name of the package to dump from the stack.verbosityint, optional- Level of verbosity (0-2) for log messages. Defaults to 0.dump_localsbool, optional- Dump the caller's stack when logging with a level of warning or higher. Defaults to False.dump_pathstr | bytes | os.PathLike, optional- Custom path to use when dumping with 'dump_on_exception' or when 'dump_locals=True', otherwise use a temporary path if None. Defaults to None.remove_handlersbool, optional- Remove existing logging handlers before adding the new stream handler. Defaults to True.dumpsdefdumps(stack:list[inspect.FrameInfo],*,err:Exception|None=None,package_name:str|None=None)->strCreate a String Representation of an Interpreter StackExternalizes '_stack_dumps' to be accessed by the user.Arguments:stacklist[inspect.FrameInfo]- Stack of frames to represent.errException | None, optional- Exception that was raised. Defaults to None.package_namestr | None, optional- Name of the package to dump from the stack, otherwise non-exclusive if set to None. Defaults to None.Returns:str- Representation of the stack.dumpdefdump(fp:io.TextIOBase|io.BytesIO,stack:list[inspect.FrameInfo],*,err:Exception|None=None,package_name:str|None=None)->NoneWrite the Representation of an Interpreter Stack using a File ObjectArguments:fpio.TextIOBase | io.BytesIO- File object to use for writing.stacklist[inspect.FrameInfo]- Stack of frames to dump.errException | None, optional- Exception that was raised. Defaults to None.package_namestr | None, optional- Name of the package to dump from the stack, otherwise non-exclusive if set to None. Defaults to None.dump_on_exception@contextlib.contextmanagerdefdump_on_exception(dump_path:str|bytes|os.PathLike|None=None)->Generator[None,None,None]Content Manager to Dump if an Exception is RaisedWrites a representation of the exception and trace stack to file.Arguments:dump_pathstr | bytes | os.PathLike | None, optional- Override the file path to use for the dump. Defaults to None.Yields:Generator[None, None, None]- Context manager.Raises:Exception- Exception that was raised.