{"id":7513,"date":"2019-11-03T21:23:38","date_gmt":"2019-11-03T21:23:38","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7513"},"modified":"2019-11-03T21:23:38","modified_gmt":"2019-11-03T21:23:38","slug":"one-log-decorator-to-rule-them-all","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/one-log-decorator-to-rule-them-all\/","title":{"rendered":"One log decorator to rule them all"},"content":{"rendered":"<p><strong>One log to rule them all<\/strong><\/p>\n<p>In a previous post we looked at implementing <a href=\"http:\/\/putridparrot.com\/blog\/decorators-attributes-annotations-in-typescript\/\" rel=\"noopener noreferrer\" target=\"_blank\">log decorators<\/a> using the experimental features of TypeScript. <\/p>\n<p>Obviously it&#8217;d be far nicer if we had a single log decorator that we can apply to each place where a decorator might be used, i.e. a log decorator which can be used instead of logClass, logMethod etc. We can simply create a log function which takes any number of arguments and have this delegate to the specific log function as required (a factory function basically). I&#8217;m not going to write the whole thing here, but something along the lines of (the following code) would do the job<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfunction log(...args: any&#x5B;]): void {\r\n  switch(args.length) {\r\n    case 1: \r\n      \/\/ return logClass\r\n    case 2: \r\n      \/\/ return logProperty     \r\n    case 3: \r\n      return (typeof args&#x5B;2] === &quot;number&quot;) ? \r\n        logParameter :\r\n        logMethod\r\n      default:\r\n         throw new Error();    \r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One log to rule them all In a previous post we looked at implementing log decorators using the experimental features of TypeScript. Obviously it&#8217;d be far nicer if we had a single log decorator that we can apply to each place where a decorator might be used, i.e. a log decorator which can be used [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46],"tags":[],"class_list":["post-7513","post","type-post","status-publish","format-standard","hentry","category-typescript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7513"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7513\/revisions"}],"predecessor-version":[{"id":7598,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7513\/revisions\/7598"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}