{"id":13981109,"url":"https://github.com/dormd/rich-logger-decorator","last_synced_at":"2025-07-21T21:31:09.381Z","repository":{"id":65456456,"uuid":"88413796","full_name":"dormd/rich-logger-decorator","owner":"dormd","description":"Rich Logger Typescript Decorator for Easy Coding \u0026 Debugging","archived":false,"fork":false,"pushed_at":"2017-12-18T15:35:17.000Z","size":133,"stargazers_count":99,"open_issues_count":8,"forks_count":19,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-02T17:59:48.464Z","etag":null,"topics":["angular","debugging","decorators","javascript","nodejs","typescript","web"],"latest_commit_sha":null,"homepage":"https://medium.com/@dormoshe/rich-typescript-logger-decorator-for-easy-coding-fc2ff73684c6","language":"TypeScript","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/dormd.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}},"created_at":"2017-04-16T13:07:21.000Z","updated_at":"2024-10-29T23:58:10.000Z","dependencies_parsed_at":"2023-01-24T11:35:10.730Z","dependency_job_id":null,"html_url":"https://github.com/dormd/rich-logger-decorator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dormd/rich-logger-decorator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dormd%2Frich-logger-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dormd%2Frich-logger-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dormd%2Frich-logger-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dormd%2Frich-logger-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dormd","download_url":"https://codeload.github.com/dormd/rich-logger-decorator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dormd%2Frich-logger-decorator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266381911,"owners_count":23920591,"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-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":["angular","debugging","decorators","javascript","nodejs","typescript","web"],"created_at":"2024-08-09T04:02:01.780Z","updated_at":"2025-07-21T21:31:08.826Z","avatar_url":"https://github.com/dormd.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rich-logger-decorator\nRich Logger Typescript Decorator for Easy Coding \u0026amp; Debugging\n\nRead the [medium article](https://medium.com/@dormoshe/rich-typescript-logger-decorator-for-easy-coding-fc2ff73684c6) about the logger usage.\n\n## Installation\n`npm i rich-logger-decorator`\n\n## Basic usage\nIn this example we will use only the ClassLogger decorator in order to log all the class methods.\n```Typescript\nimport { ClassLogger } from 'rich-logger-decorator';\n\n@ClassLogger()\nclass StudentComponent {\n\n  name: string;\n  debt: number;\n  avgGrade: number;\n  examsNumber = 0;\n\n  constructor(name: string, debt: number, avgGrade: number, examsNumber: number) {\n    console.log(`Hi ${name}`);\n\n    this.name = name;\n    this.debt = debt;\n    this.avgGrade = avgGrade;\n    this.examsNumber = examsNumber;\n  }\n\n  addExam(grade: number): void {\n    if (grade \u003c 0 || grade \u003e 100) {\n       console.log('invalid grade');\n       return;\n    }\n\n    this.avgGrade = (this.avgGrade * this.examsNumber + grade) / ++this.examsNumber;\n  }\n\n  fine(): void {\n    this.debt *= 1.1;\n  }\n\n  grantScholarship(dollars: number): void {\n    this.debt += dollars;\n  }\n}\n\nconst stud = new StudentComponent('John Doe', 1000, 98, 3);\nstud.addExam(200);\nstud.addExam(90);\nstud.grantScholarship(1000);\nstud.fine();\n```\n\nThe console looks like:\n\n![console output](https://github.com/dormd/rich-logger-decorator/blob/master/examples/output/class-logger.png?raw=true)\n\n## Mixed decorators and options\nIn this example we use the **ClassLogger** and **DiableMethodLogger** decorators with the default options and the **MethodLogger** decorator with custom options.\n```Typescript\nimport { ClassLogger, Logger, DisableMethodLogger } from 'rich-logger-decorator';\n\n@ClassLogger()\nclass StudentComponent {\n\n  name: string;\n  debt: number;\n  avgGrade: number;\n  examsNumber = 0;\n\n  constructor(name: string, debt: number, avgGrade: number, examsNumber: number) {\n    console.log(`Hi ${name}`);\n\n    this.name = name;\n    this.debt = debt;\n    this.avgGrade = avgGrade;\n    this.examsNumber = examsNumber;\n  }\n\n  addExam(grade: number): void {\n    if (grade \u003c 0 || grade \u003e 100) {\n       console.log('invalid grade');\n       return;\n    }\n\n    this.avgGrade = (this.avgGrade * this.examsNumber + grade) / ++this.examsNumber;\n  }\n\n  @Logger({\n    withClassProperties: ['debt'],\n    withTime: true,\n    logFunction: console.error\n  })\n  fine(): void {\n    this.debt *= 1.1;\n  }\n\n  @DisableMethodLogger()\n  grantScholarship(dollars: number): void {\n    this.debt += dollars;\n  }\n}\n\nconst stud = new StudentComponent('John Doe', 1000, 98, 3);\nstud.addExam(200);\nstud.addExam(90);\nstud.grantScholarship(1000);\nstud.fine();\n```\n\nThe console looks like:\n\n![console output](https://github.com/dormd/rich-logger-decorator/blob/master/examples/output/mixed.png?raw=true)\n\n\n## @Logger decorator\nThe **Logger** decorator needs to be on top of the function you want to log. The log messages will be printed before the function will start and after the function will end. This decorator can get options that defines the behavior of the flow and, eventually, affecting the log messages.\nWhen the options do not supply, the `defaultFunctionLoggerOptions` object is used by the decorator. The options are defined by the `FunctionLoggerOptions` interface\n\n\n### FunctionLoggerOptions\nThis is the interface of the **Logger** decorator options argument.\n\n**Function logger options interface**\n\nAs you can see the interface contains some properties.\n- **withArgs** — this property can be boolean or array of strings. When the value is `false`, the arguments and their values will not be a part of the log message. When the value is `true`, all the argument and their values will be a part of the log message. When the value is `array of strings`, the arguments, that their names contained in the array, will be a part of the log message.\n- **withTime** — when the value is true, the time will be a part of the log message.\n- **withClassProperties** — when the value is `true` and the function is a method of a class, the class properties and their values will be part of the log message. This option can be also an `array of the properties names` of the class (will behave like ‘withArgs’, just for class properties).\n- **logFunction** —a function that replace the traditional console.log. This function will be called with the log message in the start and the end of the original function.\n- **formatAndLogFunction** — a function that gets the resulted string of the decorator flow and logs the customized message. This function gets the time, class name (if exist), function name, start/end log, strings array of the arguments and their values and strings array of the class properties and their values.\n\nAll of the options are optional.\n\n\n**DefaultFunctionLoggerOptions**\n\n`defaultFunctionLoggerOptions` is an object with default values for `LoggerOptions`. \nThe default values are:\n- **withArgs** — true\n- **withTime** — true\n- **withClassProperties** — false\n- **logFunction** — the console.log function\n- **formatAndLogFunction** —no default value. When the value doesn’t exist, the default behavior happened.\n\n\n## @ClassLogger decorator\nThe **ClassLogger** using by classes. When you put the decorator on top of the class definition, all the methods in the class are logged automatically.  \nAs the Logger decorator, the decorator can get options that defines the behavior of the flow and eventually affects the log messages.\nWhen the options do not supplied, the `defaultClassLoggerOptions` used by the decorator. The options are defined by the `ClassLoggerOptions` interface.\n\n### ClassLoggerOptions\nThis is the interface of the **ClassLogger** decorator options argument.\n\n\n**Class logger options interface**\n\nAs you can see the interface contains some properties.\n- **methodOptions** — the options for the methods of the decorated class.  Same as the options of the `Logger` function decorator. \n- **loggedMethodsNames** — array of method’s names that being logged. When the option is undefined, all the class methods are logged.\n\nAll of the options are optional.\n\n\n**DefaultClassLoggerOptions**\n\n`defaultClassLoggerOptions` is an object with default values for `ClassLoggerOptions`. The default values are:\n- **methodOptions** — same as `defaultLoggerOptions` of the function decorator.\n- **loggedMethodsNames** — undefined (so all the class methods will be logged)\n\n## Additional decorators\nFor more convienieng usage here are two more decorators.\n\n### @DisableLogger decorator\nWhen you are using the **ClassLogger** decorator (without the methods array option), all the class methods will be logged. In order to disable specific method for being logged, you can put the **DisableLogger** decorator before the method definition. This is clearer way to prevent the logger behavior than the method array option, because of the second one restricts us to write the method name in the array.\n\n### @LoggerWithoutArgs decorator\nThis decorator is a syntactic sugar of the **Logger** decorator with `withArgs` option of `false`. Namely, the argument and theirs values will not be part of the log message. If another options will be provided, the decorator will use them, except for the `withArgs` option.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdormd%2Frich-logger-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdormd%2Frich-logger-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdormd%2Frich-logger-decorator/lists"}