{"id":16359,"date":"2014-12-16T15:30:57","date_gmt":"2014-12-16T10:00:57","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16359"},"modified":"2014-12-17T15:44:07","modified_gmt":"2014-12-17T10:14:07","slug":"groovy-annotations-for-logging","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovy-annotations-for-logging\/","title":{"rendered":"Groovy Annotations for Logging"},"content":{"rendered":"<p>We use logging in our application to report and persist error and warning messages as well as info messages (e.g. runtime statistics) so that the messages can later be retrieved and analyzed.<\/p>\n<p>Initially we were getting an instance of Logger from LoggerFactory and uses it in our class for logging information, errors, exceptions, warnings etc.<\/p>\n<p>Example code we use to write to get the instance of Logger<\/p>\n<p>[java]<br \/>\nLogger log = LoggerFactory.getLogger(this)<\/p>\n<p>[\/java]<\/p>\n<p>and method calls goes on log object like log.info(), log.error()&#8230; etc<br \/>\nWith Groovy 1.8 we can inject a log field into our classes with a simple annotation (@Log4j). We don&#8217;t need to write the above line in each and every class, just put @Log4j annotation above the class .<\/p>\n<p>[java]<\/p>\n<p>@Log4j<br \/>\nclass LoggerDemo {<br \/>\n   def useLogger() {<br \/>\n      log.info &quot;Cool!!! log field injected&quot;<br \/>\n      log.debug &quot;This is log&#8217;s debug method&quot;<br \/>\n   }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>You can also change logger\u00a0variable&#8217;s name by passing value to Annotation<\/p>\n<p>[java]<\/p>\n<p>@Log4j(value=&#8217;LOGGER&#8217;)<br \/>\nclass LoggerDemoUsingCustomVariableName {<br \/>\n   def useLogger() {<br \/>\n      LOGGER.info &quot;Hey! You have changed variable&#8217;s name&quot;<br \/>\n   }<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Other than @Log4j API grails provides\u00a0other logging libraries too. Here is the\u00a0list of available\u00a0loggers:<\/p>\n<ul>\n<li><strong>@Log<\/strong> for java.util.logging<\/li>\n<li><strong>@Commons<\/strong> for Commons-Logging<\/li>\n<li><strong>@Log4j<\/strong> for Log4J<\/li>\n<li><strong>@Slf4j<\/strong> for SLF4J<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Cool! you are done. Hope this will help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We use logging in our application to report and persist error and warning messages as well as info messages (e.g. runtime statistics) so that the messages can later be retrieved and analyzed. Initially we were getting an instance of Logger from LoggerFactory and uses it in our class for logging information, errors, exceptions, warnings etc. [&hellip;]<\/p>\n","protected":false},"author":143,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":33},"categories":[7],"tags":[350,9,288],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16359"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/143"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16359"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16359\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}