Reputation: 1030
I'm making a project backup java application, that needs to create a mail report and send it every day. My idea was to generate the body message, and to attach to the email the application log. I'm using slf4j logger, and I don't know ho to automatically retrieve the log file path using the logger.
Using plain log4j seems to be easy, but I can't get out trying to retrieve it from the slf4j logger. Anyone faced this problem?
Upvotes: 3
Views: 1956
Reputation: 1030
At the end this is what I did, and it seems to work fine. Any review or comment will be appreciated!
private File getLogFile(){
File file = null;
Appender rightAppender = null;
Enumeration<Appender> e = org.apache.log4j.Logger.getRootLogger().getAllAppenders();
while(e.hasMoreElements()){
Appender appender = (Appender)e.nextElement();
if(appender instanceof FileAppender){
rightAppender = appender;
}
}
if(null != rightAppender){
file = new File(((FileAppender)rightAppender).getFile());
}
return file;
}
Upvotes: 3
Reputation: 299048
There is no such thing as an SLF4J logger. SLF4J is a facade for another logging framework (log4j, commons-logging, java.util.Log, etc.). So an SLF4J logger is always just a wrapper around a logger in one of the supported frameworks. Look at how you configured the logger underneath.
So in your case, if you are using slf4j with log4j, consult your log4j.properties or log4j.xml as usual to find where your logs are. And if you haven't set up either of those files (or otherwise initialized log4j), you don't have logs.
Upvotes: 2