Pregunta

Am having a java program which listens to ApacheMQ and runs in background and am using Linux Server. Here I have used System.out.println() for my logging andI have to transfer those contents to a file.

Is it possible, am completely novice to this and Whether I have to use Log4J or nohup to write this sysout() value to a file.

Thanks in advance.

¿Fue útil?

Solución 2

As you observe, using nohup you get out redirected to a file. More generally, a linux process can have its output redirected to a file

 myApp > someFile.log

or maybe better appended to a file

 myApp >> someFile.log

so that when you restart you don't lose previous output.

Alternatively, as you indicate, you can put the app in charge of choosing where to write, and then using a library such as Log4j instead of System.out is very helpful.

I would use both approches: I recommend using some such logging library in any serious application, and I would start my application from a script that ensures that all output is captured in log files, this ensures that should you use some library code that writes to a stdout or stderr that the output is kept. I like to create output files whose names include date and time that the app was started, this makes it easier to find particular output.

Otros consejos

You can use

PrintWriter out = new PrintWriter("my-output.txt");

// write text.
out.println("hello world");

// when finished.
out.close();

Just replace System.out with out

I am surprised you can use JMS but don't know how to work out how to write to a file.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top