Package: sag-py-logging
sag_py_loggingThis library can be used to initialize the python logging by loading a config json.
Furthermore it provides a way to log extra fields.What it doesInitialize logging from configuration jsonPlaceholder support for the config jsonA log filter to log extra fieldsHow to useInstallationpip install sag-py-loggingpip install sag-py-logging[jinia] (optional for templating)pip install sag-py-logging[toml] (optional for toml file support)Initialize logging from jsonAdd the following as early as possible to your application code:fromsag_py_logging.log_config_initializerimportinit_loggingfromsag_py_logging.log_config_loaderimportJsonLoader,TomlLoaderfromsag_py_logging.log_config_processorsimportFormatProcessor,JinjaProcessorplaceholder_container={"host":"myhost.com",...}# For toml config with jinja templatinginit_logging("./log_config.toml",loader=TomlLoader(),processors=[JinjaProcessor(placeholder_container)])# For json config with format templatinginit_logging("./log_config.json",loader=JsonLoader(),processors=[FormatProcessor(placeholder_container)])Init logging returns the log configuration as dictionary if needed for further processing.The configurationJson config:{"version":1,"disable_existing_loggers":false,"root":{"handlers":["myhandler"],"level":"INFO"},"handlers":{"myhandler":{"host":"${host}","formatter":"handler_formatter"}}}Toml config:version=1disable_existing_loggers=false[root]handlers=["myhandler"]level="INFO"[handlers.myhandler]host="${host}"formatter="handler_formatter"This is a very basic sample on the format of the file including placeholders.Read the following for a full schema reference:https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schemaRead more on string templating here:https://docs.python.org/3/library/string.html#template-stringsOr if you use jinja templating there:https://jinja.palletsprojects.com/en/3.1.x/templates/#template-designer-documentationConfigure extra field loggingIt is possible to add a filter that extends log entries by a field for extra fields.The filter is added like that if you initialize logging by code:fromsag_py_logging.console_extra_field_filterimportConsoleExtraFieldFilterconsole_handler=logging.StreamHandler(sys.stdout)console_handler.addFilter(ConsoleExtraFieldFilter())If you init logging by config file the filter is added like that:{"formatters":{"myformatter":{"format":"s%(asctime)s - %(name)s - %(message)s - %(stringified_extra)s",},},"filters":{"console_extra_field_filter":{"()":"sag_py_logging.console_extra_field_filter.ConsoleExtraFieldFilter"}},"handlers":{"myhandler":{"formatter":"myformatter","filters":["console_extra_field_filter"]}}}Afterwards you can use the field "stringified_extra" in your format string.If you for example log the following:logging.warning('Watch out!',extra={"keyOne":"valueOne","keyTwo":2})The resulting log entry would look like that if stringified_extra is added to the end of the format string:Watch out! {"keyOne": "valueOne", "keyTwo": 2}Note: Internally json.dumps is used to convert the object/data to a stringHow to start developingWith vscodeJust install vscode with dev containers extension. All required extensions and configurations are prepared automatically.With pycharmInstall latest pycharmInstall pycharm plugin BlackConnectInstall pycharm plugin MypyConfigure the python interpreter/venvpip install requirements-dev.txtpip install black[d]Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed filesCtl+Alt+S => Check Tools => BlackConnect => Trigger on code reformatCtl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)Ctl+Alt+S => Click Tools => Actions on save => Reformat codeRestart pycharmHow to publishUpdate the version in setup.py and commit your changeCreate a tag with the same version numberLet github do the rest