0

I'm trying to write some test messages or some text content to file through java in ofbiz framework. If the file already has content, recent messages has to append at bottom of file.

In brief, I want a functionality like Debug.log in ofbiz. As it writes everything to debug.log file, I want a separate file to write my text messages.

I tried this,

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();
 }
}

But it is throwing FileNotFoundException.
As, I'm not expert in ofbiz, i'm not sure about the file path. If file path is the problem please suggest me solution.

e-sushi
  • 13,786
  • 10
  • 38
  • 57
Vamsi Krishna
  • 140
  • 1
  • 1
  • 6
  • why dont you simply add a new rollingfileappender in the log4j configuration? the kind of thing that you would do if you wanted to write perf stats to a log file of its own... – arajashe Feb 26 '14 at 12:28
  • I have seen that but i'm unable to understand that mechanism. Could you please say in more elaborated manner or give me any tutorial that helps. Thanks @arajashe – Vamsi Krishna Feb 26 '14 at 13:46

1 Answers1

0

You can use Logger to log message.

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) {
    }

Dont Forget to mention the absolute file path. Ex: /root/apache-ofbiz/applications/party/webapp/partymgr/test.txt

Or

Get the relative path from the component

 filepath = ComponentConfig.getRootLocation("party")+"webapp/log/test.txt";
Senthilmurugan
  • 383
  • 1
  • 14
  • Thanks for reply. For suppose if it is running on other machine, how can we know the absolute path. Means, in my system i know absolute path. If execute my code in other machine, how it works. How to save file in that path. Do you have any idea on how ofbiz.log.1, ofbiz.log.2, etc ofbiz.log.N are being created dynamically by ofbiz. @senthil Plz help. – Vamsi Krishna Feb 27 '14 at 07:43
  • Logger is working dude.. But i have issues with file path. I cannot give path like that in every machine right? – Vamsi Krishna Feb 27 '14 at 07:59