Hello, everyone!
I have a problem with logger. i want to log to a custom files, but theese files are empty.
Could somebody help?
Base logger:
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import java.io.File;
import java.io.IOException;
public class BaseLogger {
private Logger logger;
private BaseLogger(){
}
public BaseLogger(String loggerName) {
this.logger = Logger.getLogger(loggerName);
try {
this.logger.addAppender(createAppender(loggerName + "_APP.LOG"));
} catch (IOException e) {
e.printStackTrace();
}
logger.info("logger init.");
}
public void logInfo(String info){
logger.info(info);
}
public void logInfo(String info, Throwable t){
logger.info(info, t);
}
public void logError(String error) {
logger.error(error);
}
public void logError(String error, Throwable t) {
logger.error(error, t);
}
public void logFatal(String fatal) {
logger.fatal(fatal);
}
public void logFatal(String fatal, Throwable t) {
logger.fatal(fatal, t);
}
private FileAppender createAppender(String fileName) throws IOException {
return new FileAppender(new PatternLayout(), System.getProperty("jboss.server.log.dir") + File.separator + fileName);
}
}
Logger class:
public class ServicesLogger {
public static final BaseLogger GENERAL_LOGGER = new BaseLogger("GENERAL_LOGGER");
public static final BaseLogger SERVICE_EXECUTOR = new BaseLogger("SERVICE_EXECUTOR");
}
when i invoke ServicesLogger.SERVICE_EXECUTOR.logInfo("info"); in jboss console is appear this text, but in file not.
what is the problem?
You can add to your code only this:
import org.apache.log4j.Logger;
...
public class SomeClass {
private Logger logger = Logger.getLogger("GENERAL_LOGGER");
...
logger.info("some info");
}
And then add appender to server configuration file:
<subsystem xmlns="urn:jboss:domain:logging:1.1">
...
<periodic-rotating-file-handler name="GENERAL_LOGGER_handler">
<formatter>
<pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="GENERAL_LOGGER_APP.LOG"/>
<suffix value=".yyyy-MM-dd"/>
<append value="true"/>
</periodic-rotating-file-handler>
<logger category="GENERAL_LOGGER">
<level name="DEBUG"/>
<handlers>
<handler name="GENERAL_LOGGER_handler"/>
</handlers>
</logger>
...