有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

string Java,我们如何将其记录并存储到本地文件(无状态会话bean)中?

我试图记录程序的执行流程,以便更好地理解servlet、EJB和JSP是如何协同工作的。 目前我面临的困难是将日志输出到本地文件

我首先尝试了Java logger API,研究了以下示例:

使用Java日志API:

http://wiki4.caucho.com/Java_EE_Servlet/JSP_tutorial_:_Adding_an_error_page,_logging,_and_other_forms_of_debugging#Using_Java_log_API

我写过:

package beans;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;


@Stateless
public class ComentarioNota {

    private static final Logger log = Logger.getLogger(ComentarioNota.class.getName());

    public String convierteComentarioNota(String evaluacion, String comentario) {

            log.logp(Level.WARNING,
            this.getClass().getName(),
            this.getClass().getName(), this.getClass().getName() + "::convierteComentarioNota::el usuario introdujo: " + evaluacion + comentario);


            if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
                return "Apto";
            } else {
                return "No Apto";
            }

}

它确实会输出带有ClassName::ClassMethod::用户输入信息的日志

不管它如何将日志信息输出到控制台,我需要将其保存在本地文件中,我们如何才能登录到本地文件

我尝试使用PrintWriter并创建一个包含其内容的新文件:

package beans;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;


@Stateless
public class ComentarioNota {

    private static final Logger log = Logger.getLogger(ComentarioNota.class.getName());

    public String convierteComentarioNota(String evaluacion, String comentario) {
        try (PrintWriter out = new PrintWriter("log.txt")) {


            out.println(this.getClass().getName() + "::convierteComentarioNota::el usuario introdujo: " + evaluacion + comentario);

            if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
                return "Apto";
            } else {
                return "No Apto";
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ComentarioNota.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "No Apto";
    }
}

但是,这不会创建新文件

我们如何创建新的本地文件并将日志输出发送给它

谢谢你的帮助

我还读到: How do I save a String to a text file using Java?

Where does the ServletContext.log messages go in tomcat 7?

编辑:我已经阅读了评论教程:http://tutorials.jenkov.com/java-logging/handlers.html#streamhandler

我发现我们可以在日志中添加处理程序,还有一个内置的处理程序,它是FileHandler,应该创建一个包含日志内容的文件。我尝试了以下方法:

package beans;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;


@Stateless
public class ComentarioNota {

    private static final Logger log = Logger.getLogger(ComentarioNota.class.getName());

    public String convierteComentarioNota(String evaluacion, String comentario) {

        try {
            FileHandler handler = new FileHandler("comentarioNota.txt");
            log.addHandler(handler);

            log.logp(Level.WARNING,
                    this.getClass().getName(),
                    "convierteComentarioNota", this.getClass().getName() + "::convierteComentarioNota::el usuario introdujo: " + evaluacion + comentario);

            if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
                return "Apto";
            } else {
                return "No Apto";
            }
        } catch (IOException ex) {
            Logger.getLogger(ComentarioNota.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ComentarioNota.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "No Apto";
    }
}

但是,我仍然看到日志被输出到控制台,并且没有创建任何文件:

enter image description here

没有任何文件被认可,因为至少它没有出现在IDE中,我也尝试过找到它:

enter image description here

你能帮我弄清楚如何正确地登录到本地文件吗

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    您需要配置java。util。登录中。你要找的是文件处理器。实际上我更喜欢其他日志API,但出于您的目的,请从这里开始:Tutorial: Java Logging Configuration