{"id":3691,"date":"2011-08-23T16:59:12","date_gmt":"2011-08-23T14:59:12","guid":{"rendered":"http:\/\/blog.jteam.nl\/?p=3691"},"modified":"2011-08-23T16:59:12","modified_gmt":"2011-08-23T14:59:12","slug":"filtering-specific-exceptions-when-using-log4j","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/","title":{"rendered":"Filtering specific exceptions when using log4j"},"content":{"rendered":"<p><a href=\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3703\" title=\"log4j-logo\" src=\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg\" alt=\"\" width=\"207\" height=\"139\"><\/a>A while ago I was working on a project where a background job was being triggered every few seconds or so. This job would call a method on a service, that connected to a remote backend, that was regularly unavailable.<\/p>\n<p>When the backend was down, the logs would be flooded with long stacktraces every few seconds. Making it all but impossible to spot actual errors&nbsp; amid the carnage.<\/p>\n<p>To solve this problem I could have disabled logging for this service completely. But in this case that seemed like a bad idea. This service was, by all means and purposes a kitchen sink. By disabling logging completely I would run the risk of also swallowing legitimate errors. What I really wanted to do was filter out only this &#8220;BackendNotAvailableException&#8221;<\/p>\n<p>Fortunately there is a way to do that. Contained within <a href=\"http:\/\/logging.apache.org\/log4j\/companions\/extras\/index.html\">Apache Extras Companion for Apache log4j<\/a> there is a filter called <a href=\"http:\/\/svn.apache.org\/repos\/asf\/logging\/site\/trunk\/docs\/log4j\/companions\/extras\/apidocs\/org\/apache\/log4j\/filter\/ExpressionFilter.html\">ExpressionFilter<\/a> that you can use to do exactly that. Here&#8217;s an example of how to configure it for this usecase.<\/p>\n<p><pre class=\"brush: xml; title: ; notranslate\" title=\"\">&lt;br&gt;\n&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;&lt;br&gt;\n&amp;lt;!DOCTYPE log4j:configuration SYSTEM &quot;log4j.dtd&quot;&amp;gt;&lt;br&gt;\n&amp;lt;log4j:configuration xmlns:log4j=&quot;http:\/\/jakarta.apache.org\/log4j\/&quot; debug=&quot;false&quot;&amp;gt;&lt;br&gt;\n    &amp;lt;appender name=&quot;CONSOLE&quot;&amp;gt;&lt;br&gt;\n        &amp;lt;param name=&quot;Target&quot; value=&quot;System.out&quot;\/&amp;gt;&lt;br&gt;\n        &amp;lt;layout&amp;gt;&lt;br&gt;\n            &amp;lt;param name=&quot;ConversionPattern&quot; value=&quot;%d %p &#x5B;%c] - %m%n&quot;\/&amp;gt;&lt;br&gt;\n        &amp;lt;\/layout&amp;gt;&lt;br&gt;\n        &amp;lt;filter class=&quot;org.apache.log4j.filter.ExpressionFilter&quot;&amp;gt;&lt;br&gt;\n            &amp;lt;param name=&quot;expression&quot; value=&quot;EXCEPTION ~= com.company.BackendNotAvailableException&quot; \/&amp;gt;&lt;br&gt;\n            &amp;lt;param name=&quot;acceptOnMatch&quot; value=&quot;false&quot;\/&amp;gt;&lt;br&gt;\n        &amp;lt;\/filter&amp;gt;&lt;br&gt;\n    &amp;lt;\/appender&amp;gt;&lt;br&gt;\n    &amp;lt;root&amp;gt;&lt;br&gt;\n        &amp;lt;priority value =&quot;INFO&quot; \/&amp;gt;&lt;br&gt;\n        &amp;lt;appender-ref ref=&quot;CONSOLE&quot;\/&amp;gt;&lt;br&gt;\n    &amp;lt;\/root&amp;gt;&lt;br&gt;\n&amp;lt;\/log4j:configuration&amp;gt;&lt;br&gt;\n<\/pre><\/p>\n<p>There is actually a lot more you can do with this filter and i encourage you to check it out in greater detail<\/p>\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/bit.ly\/3BAo305\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"256\" src=\"https:\/\/trifork.nl\/articles\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png\" alt=\"\" class=\"wp-image-20303\" srcset=\"https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1024x256.png 1024w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-300x75.png 300w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-768x192.png 768w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1536x384.png 1536w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-2048x512.png 2048w, https:\/\/trifork.nl\/blog\/wp-content\/uploads\/sites\/3\/2022\/02\/Blog-Banner-1-1920x480.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>A while ago I was working on a project where a background job was being triggered every few seconds or so. This job would call a method on a service, that connected to a remote backend, that was regularly unavailable. When the backend was down, the logs would be flooded with long stacktraces every few [&hellip;]<\/p>\n","protected":false},"author":58,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[31,10],"tags":[11,191,192],"class_list":["post-3691","post","type-post","status-publish","format-standard","hentry","category-java","category-development","tag-java","tag-log4j","tag-logging"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Filtering specific exceptions when using log4j - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Filtering specific exceptions when using log4j - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"A while ago I was working on a project where a background job was being triggered every few seconds or so. This job would call a method on a service, that connected to a remote backend, that was regularly unavailable. When the backend was down, the logs would be flooded with long stacktraces every few [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-08-23T14:59:12+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg\" \/>\n<meta name=\"author\" content=\"Jelmer Kuperus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jelmer Kuperus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/\",\"url\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/\",\"name\":\"Filtering specific exceptions when using log4j - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg\",\"datePublished\":\"2011-08-23T14:59:12+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#primaryimage\",\"url\":\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg\",\"contentUrl\":\"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Filtering specific exceptions when using log4j\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2\",\"name\":\"Jelmer Kuperus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g\",\"caption\":\"Jelmer Kuperus\"},\"sameAs\":[\"http:\/\/www.dutchworks.nl\"],\"url\":\"https:\/\/trifork.nl\/blog\/author\/jelmer\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Filtering specific exceptions when using log4j - Trifork Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/","og_locale":"en_US","og_type":"article","og_title":"Filtering specific exceptions when using log4j - Trifork Blog","og_description":"A while ago I was working on a project where a background job was being triggered every few seconds or so. This job would call a method on a service, that connected to a remote backend, that was regularly unavailable. When the backend was down, the logs would be flooded with long stacktraces every few [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/","og_site_name":"Trifork Blog","article_published_time":"2011-08-23T14:59:12+00:00","og_image":[{"url":"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg","type":"","width":"","height":""}],"author":"Jelmer Kuperus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jelmer Kuperus","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/","url":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/","name":"Filtering specific exceptions when using log4j - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#primaryimage"},"image":{"@id":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#primaryimage"},"thumbnailUrl":"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg","datePublished":"2011-08-23T14:59:12+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#primaryimage","url":"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg","contentUrl":"http:\/\/blog.jteam.nl\/wp-content\/uploads\/2011\/08\/log4j-logo.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/filtering-specific-exceptions-when-using-log4j\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"Filtering specific exceptions when using log4j"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/c0ee9f25744015bf661fee1b797341f2","name":"Jelmer Kuperus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fff87cf8073c776ffcbe26326f713998?s=96&d=mm&r=g","caption":"Jelmer Kuperus"},"sameAs":["http:\/\/www.dutchworks.nl"],"url":"https:\/\/trifork.nl\/blog\/author\/jelmer\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/3691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/58"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=3691"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/3691\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=3691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=3691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=3691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}