{"id":19902617,"url":"https://github.com/keivanipchihagh/write-logs-like-pros","last_synced_at":"2026-02-17T02:34:57.220Z","repository":{"id":152110237,"uuid":"501291048","full_name":"keivanipchihagh/write-logs-like-pros","owner":"keivanipchihagh","description":"Still using Try-Catch the old way? Hard-Coding log files into your code? Meh, use Decorators instead!","archived":false,"fork":false,"pushed_at":"2022-09-05T05:10:53.000Z","size":10,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-31T20:49:52.808Z","etag":null,"topics":["decorators","exception-handling","logging"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/keivanipchihagh.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,"zenodo":null}},"created_at":"2022-06-08T14:41:26.000Z","updated_at":"2023-01-16T15:26:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"dcdaeb54-5a76-4c3a-954b-e87a7e7099f0","html_url":"https://github.com/keivanipchihagh/write-logs-like-pros","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/keivanipchihagh/write-logs-like-pros","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keivanipchihagh%2Fwrite-logs-like-pros","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keivanipchihagh%2Fwrite-logs-like-pros/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keivanipchihagh%2Fwrite-logs-like-pros/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keivanipchihagh%2Fwrite-logs-like-pros/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keivanipchihagh","download_url":"https://codeload.github.com/keivanipchihagh/write-logs-like-pros/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keivanipchihagh%2Fwrite-logs-like-pros/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279017244,"owners_count":26086015,"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","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["decorators","exception-handling","logging"],"created_at":"2024-11-12T20:18:43.278Z","updated_at":"2025-10-13T23:25:37.872Z","avatar_url":"https://github.com/keivanipchihagh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# write-logs-like-pros\n## What and Why?\nEvery automated module and scheduled code will at some point run into problems that will cause exceptions to occur. Often times, we want to log our code to monitor its progress. Using _Try-Catch_ the classic way works fine, but is messy and often ruins the flow of the code.\n\n## Clean-Code Architecture\n\u003e As you've probably read elsewhere, a function should do one thing, and only one thing. “Functions should do something, or answer something, but not both.” As he states, a function should change the state of an object, or return some information about that object, but not both. - [Clean Code](https://alvinalexander.com/programming/clean-code-quotes-robert-c-martin#:~:text=As%20you've%20probably%20read,that%20object%2C%20but%20not%20both.)\n\n## How it Works\nWe learned from the previous quote from **Martin C.Roberts** that every function has only one task to do and can only succeed/fail at that spesific task; So, only one exception can happen and one success message if nothing goes wrong.\n\n## Usage\nWe have a simple module in _logger.py_ that writes logs into a file:\n```python\nimport logging\n\n# Create the logging module\nlogging.basicConfig(\n    format = '%(asctime)s - %(levelname)s - %(message)s',\n    datefmt = '%m/%d/%Y %I:%M:%S %p',\n    filename = 'logs.log',\n    level = logging.DEBUG\n)\n_logger = logging.getLogger(__name__)\n```\n\nUsing the classic _Try-Catch_ architecture:\n```python\nfrom logger import _logger # Our logger.py file that goes along our project files\n\n# Our function\ndef square(value: int): return (value * value)\n\ntry:\n    result = square('Whoops!') # We can't multiple string to string\nexcept:\n    print('Cannot multiply non-int values')                       # Print on console\n    _logger.error('Cannot multiply non-int values')               # Write logs in file\n```\n\nUsing decorators:\n```python\nfrom logger import logger # Our logger.py file that goes along our project files\n\n# Our function but with a decorator with custome success/error messages\n@logger(on_success = 'Success', on_failure = 'Cannot multiply non-int values')\ndef square(value: int) -\u003e int:\n  return (value * value)\n\nsquare('Whoops!') # We can't multiple string to string\n```\n\n## Further Customizations\nYou can of course customize the decorator body and parameters according to your needs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeivanipchihagh%2Fwrite-logs-like-pros","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeivanipchihagh%2Fwrite-logs-like-pros","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeivanipchihagh%2Fwrite-logs-like-pros/lists"}