{"id":754,"date":"2011-09-22T13:34:50","date_gmt":"2011-09-22T17:34:50","guid":{"rendered":"http:\/\/efsavage.com\/blog\/?p=754"},"modified":"2011-09-22T15:56:03","modified_gmt":"2011-09-22T19:56:03","slug":"logging-like-it%e2%80%99s-2011","status":"publish","type":"post","link":"https:\/\/efsavage.com\/blog\/posts\/logging-like-it%e2%80%99s-2011\/","title":{"rendered":"Logging Like it\u2019s 2011"},"content":{"rendered":"<p>Earlier this year I <a title=\"Logging Like it\u2019s 2002\" href=\"http:\/\/efsavage.com\/blog\/posts\/logging-like-its-2002\/\">revisited how I was logging things<\/a> in Java, and decided I would try a hybrid approach. \u00a0I&#8217;m reporting back to say that it&#8217;s been successful. There are basically two ways I do it now:<\/p>\n<h3>Use java.util.Logging for libraries<\/h3>\n<p>It turns out that there&#8217;s nothing actually wrong with JUL aside from its limitations in terms of configuration. \u00a0It has different names than the\u00a0Ceki G\u00fclc\u00fc loggers (warning() instead of warn(), finest() instead of trace(), etc.) but otherwise works the same. \u00a0The configuration side of things I can&#8217;t even speak do as I haven&#8217;t had to configure it, I never use the actual JUL output.<\/p>\n<h3>Use Logback for applications<\/h3>\n<p>As suspected, Logback is basically just a new version of Log4J. \u00a0It&#8217;s got some niceties like\u00a0<a href=\"http:\/\/logback.qos.ch\/manual\/mdc.html#mis\">MDCInsertingServletFilter<\/a> that means I don&#8217;t have to write that filter myself anymore, and it&#8217;s supposedly faster and more stable, so there&#8217;s no reason I can see not to use it. \u00a0I also like that it has a format for condensing class names without completely losing the package names, so it goes from &#8220;com.efsavage.application.servlet.LoginServlet&#8221; to &#8220;c.e.a.s.LoginServlet&#8221; instead of just &#8220;LoginServlet&#8221;.<\/p>\n<h3>Why use two?<\/h3>\n<p>I like the fact that my libraries have zero logging dependencies and configuration, so I can always pop them into another app without adding baggage or conflicts or much additional configuration. \u00a0I can upgrade the logger in one application without having to deal with version conflicts of my other apps and having to do classpath exclusions and that type of nastiness.<\/p>\n<h4>Tip<\/h4>\n<p>If you do it this way, and you see your JUL logging showing up twice, you can edit the default logging config in your JDK installation, or if you prefer to leave that untouched as I do, try this (via <a href=\"http:\/\/blog.cn-consult.dk\/2009\/03\/bridging-javautillogging-to-slf4j.html\" target=\"_blank\">Java Bits<\/a>):<\/p>\n<p><code>java.util.logging.Logger rootLogger = LogManager.getLogManager().getLogger(\"\");<br \/>\nHandler[] handlers = rootLogger.getHandlers();<br \/>\nfor (int i = 0; i &lt; handlers.length; i++) {<br \/>\n    rootLogger.removeHandler(handlers[i]);<br \/>\n}<br \/>\nSLF4JBridgeHandler.install();<\/code><\/p>\n<p>This basically yankts out the default logger and let&#8217;s SLF4J do it&#8217;s thing alone. \u00a0In a webapp you&#8217;ll probably throw this in your context listener or startup servlet where you previously did your log4j config.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier this year I revisited how I was logging things in Java, and decided I would try a hybrid approach. \u00a0I&#8217;m reporting back to say that it&#8217;s been successful.<\/p>\n","protected":false},"author":1,"featured_media":760,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5,6],"tags":[233,72,232,231],"series":[],"class_list":["post-754","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-logging","tag-jul","tag-log4j","tag-logback","tag-slf4j"],"jetpack_featured_media_url":"https:\/\/efsavage.com\/blog\/wp-content\/uploads\/2011\/09\/4636162605_9ac8e91b56.jpg","jetpack_shortlink":"https:\/\/wp.me\/p1AkJt-ca","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts\/754","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/comments?post=754"}],"version-history":[{"count":5,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts\/754\/revisions"}],"predecessor-version":[{"id":761,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts\/754\/revisions\/761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/media\/760"}],"wp:attachment":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/media?parent=754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/categories?post=754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/tags?post=754"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/series?post=754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}