{"id":238,"date":"2005-02-22T13:33:28","date_gmt":"2005-02-22T19:33:28","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=238"},"modified":"2005-02-22T13:33:28","modified_gmt":"2005-02-22T19:33:28","slug":"runtime-log4j-configuration","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/238","title":{"rendered":"Runtime log4j configuration"},"content":{"rendered":"<p>So, I&#8217;ve spent the last day or so trying to track down how to configure log4j at runtime (log4j 1.2.8).  Now, there are some things that are easy: setting the level of the root logger is as easy as: <code>LogManager.getRootLogger().setLevel((Level) Level.DEBUG)<\/code>.  However, if you want to do more complicated things at runtime based on other inputs than the log4j.{properties,xml} file, things begin to get a bit kludgy.  For example, I wanted to set up a set of appenders with sane defaults.  Then, if values were present in a configuration file, I wanted to update those appenders with different configuration values and change the root logger&#8217;s behavior.<\/p>\n<p>The easiest way I could find was to manipulate the properties file, as shown below:<\/p>\n<div><code><\/p>\n<pre>package test;\n\nimport org.apache.log4j.*;\nimport org.apache.log4j.net.SMTPAppender;\nimport org.apache.log4j.net.SyslogAppender;\nimport org.apache.commons.logging.Log;\nimport org.apache.commons.logging.LogFactory;\nimport java.util.Properties;\nimport java.io.*;\n\npublic class Test {\n   private Log log = LogFactory.getLog(Test.class);\n   Test() {\n      log.debug(\"test1\");\n      switchAppenders();\n      log.debug(\"test2\");\n   }\n   public static void main(String args[]) {\n      Test t = new Test();\n   }\n   private void switchAppenders() {\n      Properties props = new Properties();\n      try {\n           InputStream configStream = getClass().getResourceAsStream(\"\/log4j.properties\");\n           props.load(configStream);\n           configStream.close();\n      } catch(IOException e) {\n          System.out.println(\"Error: Cannot laod configuration file \");\n      }\n      props.setProperty(\"log4j.rootLogger\",\"DEBUG, file\");\n      props.setProperty(\"log4j.appender.file.File\",\"out.log\");\n      LogManager.resetConfiguration();\n      PropertyConfigurator.configure(props);\n     }\n}<\/pre>\n<p><\/code><\/div>\n<p>This code is executed via this command, making sure that log4j.properties is present in the classpath:<br \/>\n<code>java -classpath .:log4j-1.2.8.jar:commons-logging.jar test.Test<\/code><\/p>\n<p>This is quite a kludge, but I couldn&#8217;t find anything better out there.  It has the obvious setback that the changes you make to the log4j aren&#8217;t persisted, nor can they easily happen in more than one place, and any changes to appender names break a log of things, but at least it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I&#8217;ve spent the last day or so trying to track down how to configure log4j at runtime (log4j 1.2.8). Now, there are some things that are easy: setting the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}