I have created a Logger over the log4j logger with ConsoleAppender. The code is as follows,
public class AppLogger{
static {
ConsoleAppender ca = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
org.apache.log4j.Logger.getRootLogger().addAppender(ca);
}
public AppLogger(Class classname) {
logger = LoggerFactory.getLogger(classname);
}
public void debug(String message){
logger.debug(message);
}
...
...
}
I have not used the log4j.properties file because I am not sure where to put it. The above code is working fine and logging properly on the console. Now I want to change it to use it with a FileAppender.
I simply updated the static constructors as follows,
FileAppender fa = new FileAppender();
fa.setFile("d:/log.txt");
fa.setLayout(new PatternLayout(PatternLayout.DEFAULT_CONVERSION_PATTERN));
org.apache.log4j.Logger.getRootLogger().addAppender(fa);
And added fa
insted of ca
. But now I am getting this error,
log4j:ERROR No output stream or file set for the appender named [null].
I am not sure which property I am missing to define fro the File Appender.
You forgot to activate the options:
FileAppender fa = new FileAppender();
fa.setFile("d:/log.txt");
fa.setLayout(new PatternLayout(PatternLayout.DEFAULT_CONVERSION_PATTERN));
fa.activateOptions(); \\ ← activate the options
org.apache.log4j.Logger.getRootLogger().addAppender(fa);
The method OptionHandler#activateOptions()
activate the options that were previously set with calls to option setters.