Java 如何在ofbiz框架中向文件写入文本或日志

Java 如何在ofbiz框架中向文件写入文本或日志,java,ofbiz,Java,Ofbiz,我试图在ofbiz框架中通过java编写一些测试消息或一些文本内容到文件中。如果文件已包含内容,则必须在文件底部追加最近的消息 简言之,我想要一个类似Debug.log的功能。当它将所有内容写入debug.log文件时,我需要一个单独的文件来编写文本消息 我试过这个 File file = new File("/applications/party/webapp/partymgr/test.txt"); if (!file.exists()) { try { file.createNewF

我试图在ofbiz框架中通过java编写一些测试消息或一些文本内容到文件中。如果文件已包含内容,则必须在文件底部追加最近的消息

简言之,我想要一个类似Debug.log的功能。当它将所有内容写入debug.log文件时,我需要一个单独的文件来编写文本消息

我试过这个

File file = new File("/applications/party/webapp/partymgr/test.txt");
if (!file.exists()) {
 try {
  file.createNewFile();
  FileWriter fw = new FileWriter(file.getAbsoluteFile());
  BufferedWriter bw = new BufferedWriter(fw);
  Debug.logInfo("writing...........", module);
  bw.write("this is first line in file");
  bw.close();
 } catch (IOException e) {
  e.printStackTrace();
 }
}
但它正在抛出FileNotFoundException。
因为我不擅长ofbiz,所以我不确定文件路径。如果文件路径是问题所在,请向我建议解决方案。

您可以使用记录器记录消息

Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
        logger.setLevel(Level.INFO);
        try {
            FileHandler fileTxt = new FileHandler("/root/apache-ofbiz/applications/party/webapp/partymgr/test.txt",true);

            SimpleFormatter formatterTxt = new SimpleFormatter();
            fileTxt.setFormatter(formatterTxt);
            logger.addHandler(fileTxt);

            logger.log(Level.INFO,"this is first line in file");

    } catch (SecurityException e) {
    } catch (IOException e) {
    }
别忘了提到绝对文件路径。Ex:/root/apache of biz/applications/party/webapp/partymgr/test.txt

从组件获取相对路径

 filepath = ComponentConfig.getRootLocation("party")+"webapp/log/test.txt";

为什么不在log4j配置中添加一个新的rollingfileappender呢?如果您想将perf stats写入自己的日志文件,您会做的事情……我已经看到了,但我无法理解这种机制。你能不能说得更详细一些,或者给我一些有帮助的教程。感谢@arajasheThanks的回复。假设它在其他机器上运行,我们如何知道绝对路径。意思是,在我的系统中,我知道绝对路径。如果在其他机器上执行我的代码,它是如何工作的。如何在该路径中保存文件。您知道ofbiz是如何动态创建ofbiz.log.1、ofbiz.log.2等ofbiz.log.N的吗@senthil Plz帮助。记录器正在工作,伙计。。但我在文件路径方面有问题。我不能在每台机器上都给出这样的路径,对吗?