I cannot create object for this coding. How can I access this values I want to return the msg
value in this coding?
package com.my;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.VectorAppender;
import org.apache.log4j.spi.LoggingEvent;
import java.util.Vector;
public class LogCapture
{
private final VectorAppender appender;
private final Level level;
public LogCapture(final Level level)
{
this.level = level;
Logger root = Logger.getRootLogger();
appender = new VectorAppender();
root.addAppender(appender);
}
public static void main(String args[])
{
new LogCapture(Level.INFO);
LogCapture.getMessage() ;
}
public String getMessage()
{
Vector vector = appender.getVector();
String msg = null;
switch (vector.size())
{
case 0:
break;
case 1:
LoggingEvent event = (LoggingEvent) vector.elementAt(0);
msg = event.getRendere开发者_StackOverflow中文版dMessage();
break;
}
return msg;
}
}
I'm not sure that I understand the question, but it sounds easy. You create an instance like this:
new LogCapture(Level.INFO);
You can find the other log levels here: Documentation for Level
Edit (since the OP has added some more code): Change the code in the main method so that it says
LogCapture logCapture = new LogCapture(Level.INFO);
logCapture.getMessage();
精彩评论