如何为构造函数创建实例?

发布于 2024-11-13 15:10:24 字数 1074 浏览 0 评论 0原文

我无法为此编码创建对象。我如何访问此值,我想在此编码中返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2024-11-20 15:10:24

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

new LogCapture(Level.INFO);

您可以在此处找到其他日志级别:
级别文档

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文