如何为构造函数创建实例?
我无法为此编码创建对象。我如何访问此值,我想在此编码中返回 msg
值?
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.getRenderedMessage();
break;
}
return msg;
}
}
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.getRenderedMessage();
break;
}
return msg;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解这个问题,但这听起来很简单。您可以像这样创建一个实例:
您可以在此处找到其他日志级别:
级别文档
编辑(因为OP有添加了更多代码):
更改 main 方法中的代码,使其显示
I'm not sure that I understand the question, but it sounds easy. You create an instance like this:
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