{"id":18684790,"url":"https://github.com/j-sephb-lt-n/my-python-logging-setup","last_synced_at":"2025-08-31T12:35:59.025Z","repository":{"id":254619772,"uuid":"846955128","full_name":"J-sephB-lt-n/my-python-logging-setup","owner":"J-sephB-lt-n","description":"Illustrates various applications of native python logging","archived":false,"fork":false,"pushed_at":"2024-09-19T12:44:17.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-22T11:06:32.202Z","etag":null,"topics":["log","logger","logging","native","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/J-sephB-lt-n.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-24T12:37:40.000Z","updated_at":"2024-10-12T11:55:23.000Z","dependencies_parsed_at":"2024-08-24T20:25:15.472Z","dependency_job_id":"d6b71143-21e5-4b99-bb88-b5960e6dbbb0","html_url":"https://github.com/J-sephB-lt-n/my-python-logging-setup","commit_stats":null,"previous_names":["j-sephb-lt-n/my-python-logging-setup"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/J-sephB-lt-n/my-python-logging-setup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Fmy-python-logging-setup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Fmy-python-logging-setup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Fmy-python-logging-setup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Fmy-python-logging-setup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/J-sephB-lt-n","download_url":"https://codeload.github.com/J-sephB-lt-n/my-python-logging-setup/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/J-sephB-lt-n%2Fmy-python-logging-setup/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261282322,"owners_count":23134939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["log","logger","logging","native","python","python3"],"created_at":"2024-11-07T10:19:22.013Z","updated_at":"2025-06-22T11:06:46.816Z","avatar_url":"https://github.com/J-sephB-lt-n.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# My Python Logging Setup\n\nA toy codebase illustrating native python logging functionality.\n\nTo see it in action run\n\n```bash\npython main.py\n```\n\nGoals:\n\n| Completed | Goal                                                                          | Notes                                                    |\n| --------- | ----------------------------------------------------------------------------- | -------------------------------------------------------- |\n| ✅        | unified logger formatting                                                     |                                                          |\n| ✅        | dynamic logger formatting (easy to set and reset format, and done safely)     |\n| ✅        | function decorator (logs function calls, inputs and outputs, runtime metrics) | Might not be aesthetic for all complex python data types |\n| ❌        | class decorator (logs instantiations and method calls)                        | Haven't started this yet                                 |\n| ✅        | section timer                                                                 |\n| ❌        | flask/fastAPI route call logging                                              | Haven't started this yet                                 |\n| ✅        | integration with Google Cloud logging                                         |                                                          |\n\nHere are examples of the basic functionality available so far:\n\n```python\nimport sys\nfrom src.log import (\n  BASE_LOGGER_FORMAT,\n  create_default_logger,\n  log_handlers,\n  code_section_timer,\n  log_function_or_method_call,\n  change_logger_format,\n)\n\nlogger = create_default_logger(__name__)\n\n# automatically log all errors raised outside of a try/except except block\nsys.excepthook = lambda err_type, err_value, err_traceback: logger.error(\n    \"Uncaught exception\", exc_info=(err_type, err_value, err_traceback)\n)\n\n# basic log messages #\nlogger.info(\"an example log message\")\n# 2024-09-01 20:33:56,769 : __main__ : INFO : [daily ELT] an example log message\n\nverbose_logger = create_default_logger(\n  logger_name=f\"verbose-{__name__}\",\n  handlers=[\n    log_handlers.file,\n    log_handlers.gcp,\n    log_handlers.stream,\n  ],\n)\nverbose_logger.info(\"this log message writes to local file `main.log`, to standard out, and to GCP cloud logging\")\n\n# code section timer #\nimport time\ncode_section_timer.set_logger(logger)\ncode_section_timer.section(\"outer section\").start()\n# 2024-09-01 20:34:11,504 : __main__ : INFO : [daily ELT] Started section 'outer section'\ntime.sleep(1)\ncode_section_timer.section(\"inner section\").start()\n# 2024-09-01 20:34:12,511 : __main__ : INFO : [daily ELT] Started section 'inner section'\ntime.sleep(2)\ncode_section_timer.section(\"inner section\").end()\n# 2024-09-01 20:34:14,518 : __main__ : INFO : [daily ELT] Finished section 'inner section' (total runtime 2 seconds = 0.03 minutes)\ntime.sleep(1)\ncode_section_timer.section(\"outer section\").end()\n# 2024-09-01 20:34:15,526 : __main__ : INFO : [daily ELT] Finished section 'outer section' (total runtime 4 seconds = 0.07 minutes)\n\n# automatic function call logging #\n@log_function_or_method_call(logger, log_inputs=True, log_outputs=True, log_runtime_metrics=True)\ndef do_nothing(task: str) -\u003e None:\n  ignore = task\n  time.sleep(0.69)\n\ndo_nothing(task=\"delete the database\")\n# 2024-09-01 20:36:22,197 : __main__ : INFO : [daily ELT] Called do_nothing('task'='delete the database')\n# 2024-09-01 20:36:22,892 : __main__ : INFO : [daily ELT] Finished function/method do_nothing()\n#     --Function/method Output--\n# 'None'\n#     --Runtime metrics--\n#     Total execution time: 0.69 seconds = 0.01 minutes\n#     (in future I want to add statistics here related to memory and CPU usage)\n\n# dynamic change of logger format #\nfor user in (\"johann\", \"peter\", \"joe\"):\n    change_logger_format(\n        logger,\n        new_format=BASE_LOGGER_FORMAT.replace(\n            \"%(message)s\",\n            f\"user='{user}' %(message)s\"\n        )\n    )\n    logger.info(\"loading user data\")\n    logger.info(\"processing user data\")\n# 2024-09-01 20:36:53,872 : __main__ : INFO : [daily ELT] user='johann' loading user data\n# 2024-09-01 20:36:53,873 : __main__ : INFO : [daily ELT] user='johann' processing user data\n# 2024-09-01 20:36:53,873 : __main__ : INFO : [daily ELT] user='peter' loading user data\n# 2024-09-01 20:36:53,873 : __main__ : INFO : [daily ELT] user='peter' processing user data\n# 2024-09-01 20:36:53,873 : __main__ : INFO : [daily ELT] user='joe' loading user data\n# 2024-09-01 20:36:53,873 : __main__ : INFO : [daily ELT] user='joe' processing user data\nchange_logger_format(\n    logger,\n    new_format=BASE_LOGGER_FORMAT,\n)\nlogger.info(\"logger format back to default\")\n# 2024-09-01 20:37:18,467 : __main__ : INFO : [daily ELT] logger format back to default\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj-sephb-lt-n%2Fmy-python-logging-setup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fj-sephb-lt-n%2Fmy-python-logging-setup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj-sephb-lt-n%2Fmy-python-logging-setup/lists"}