简体   繁体   中英

log4j Logger is not visible

Has someone experience with log4j and knows what "the constructor () is not visible means" ?

Here's the code for the Main-class:

import org.apache.log4j.xml.DOMConfigurator;
import stdGame.*;
public class Main {
  public static void main(String[] args) {
      DOMConfigurator.configureAndWatch( "data/log/myLoggerConfig.xml", 60*1000 );
      new LogTest();
    }
}

The LogTest-class is located in stdGame-package, which is already imported as you can see. The code is working as desired, when placing the LogTest-class in the same package as the Main-class. Here is the code for the LogTest()-class:

package stdGame;
import org.apache.log4j.Logger;
public class LogTest {
    private static Logger logger = Logger.getLogger(LogTest.class);
    LogTest() {
        logger.info("My info-msg in LogTest.");
        logger.error("My error-msg in LogTest.");
    }
}

Try placing them into the same package, the error message will disapear. But i need them placed into seperated packages.

Finally this is the myLoggerConfig.xml-file:

  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="MeinAppender" class="org.apache.log4j.DailyRollingFileAppender">
  <param name="datePattern" value="'.'yyyy-MM-dd_HH-mm" />
  <param name="file" value="data/log/myLogfile.log" />
  <param name="Append" value="true" />
  <layout class="org.apache.log4j.PatternLayout">
  <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
  </layout>
  </appender>
  <root>
  <priority value="INFO" />
  <appender-ref ref="MeinAppender" />
  </root>
  </log4j:configuration>

Your class LogTest is in stdName package and it's constructor has package-private access. It seems Main class is in some package other than stdName and so the compiler is complaining that LogTest() constructor is not visible in Main 's package. Change the constructor of LogTest to public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM