如何为Constructor创建实例?

时间:2011-06-04 09:30:37

标签: java constructor

我无法为此编码创建对象。如何访问此值我想在此编码中返回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;
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解这个问题,但听起来很容易。您可以创建这样的实例:

new LogCapture(Level.INFO);

您可以在此处找到其他日志级别: Documentation for Level

编辑(因为OP增加了一些代码): 更改main方法中的代码,使其显示

LogCapture logCapture = new LogCapture(Level.INFO);
logCapture.getMessage();