Why the log4j configuration fails

Source: Internet
Author: User

Yesterday on-line toss a half-day, found that the log is always unable to print correctly to the specified file, but Tomcat catalina.out print the business log, carefully check the online log4j configuration, no problems found, console log input is not open. Then quickly use the last online package to replace the re-release, found that the problem solved.

Taking into account the new on-line jar package that relies on other systems, after examining the jar, it is found that the jar package contains the log4j.xml!!


Why did log4j choose Log4j.xml in the jar instead of his own log4j.properties file, for the reason that it was in the Logmanager class of log4j?


See section Code:

/**   * @deprecated This variable are for internal use only. It'll   * become package protected in the future versions.   * */  static public final String default_configuration_file = "Log4j.properties";    Static final String default_xml_configuration_file = "Log4j.xml";       /**   * @deprecated This variable are for internal use only. It'll   * become private in the future versions.   * */  static final public String default_configuration_key= "Log4j.configuration";

The above is a member variable of Logmanager.

Then look at the static module:

static {//By default we use a defaultrepositoryselector which always returns ' H '.    Hierarchy h = new Hierarchy (new Rootlogger (level) level.debug);    Repositoryselector = new Defaultrepositoryselector (h);  /** Search for the properties file Log4j.properties in the CLASSPATH.    */String override =optionconverter.getsystemproperty (Default_init_override_key, NULL);    If there is no default init override, then get the resource//specified by the user or the default Config file. if (override = = NULL | | "false". Equalsignorecase (Override)) {String configurationoptionstr = Optionconverter.getsystemproperty (Default_con      Figuration_key, NULL); String configuratorclassname = Optionconverter.getsystemproperty (CONFIG      Urator_class_key, NULL);      URL url = null; If the user has not specified the Log4j.configuration//property, we search first for the file "Log4j.xml" and th En//"log4j. Properties "if (configurationoptionstr = = null) {URL = Loader.getresource (default_xml_configuration_file); if (url = = N      ull) {url = Loader.getresource (default_configuration_file);}  } else {try {url = new URL (configurationoptionstr)} catch (Malformedurlexception ex) {//So, resource are not a URL: Attempt to get the resource from the class path URL = Loader.getresource (CONFIGURATIONOPTIONSTR); }}//If We have a non-null URL and then delegate the rest of the//configuration to the Optionconvert      Er.selectandconfigure//method.        if (URL! = null) {loglog.debug ("Using URL [" +url+ "] for automatic log4j configuration.");        try {optionconverter.selectandconfigure (URL, Configuratorclassname, logmanager.getloggerrepository ());        } catch (Noclassdeffounderror e) {Loglog.warn ("Error during default initialization", E); }} else {loglog.debug ("Could not find resource: [" +configurationoptionstr+ "]."); }} else {Loglog.debug ("Default initialization of overridden by" + Default_init_override_key + "PR  Operty. ");}}

The key point is

if (configurationoptionstr = = null) {URL = Loader.getresource (default_xml_configuration_file); if (url = = null) {  URL = Loader.getresource (default_configuration_file);}      }

Describes the configuration path that log4j first uses, and then uses the XML configuration file under the Classpath before selecting the properties file. This is the reason, I hope to help everyone.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Why the log4j configuration fails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.