I have created a JAVA application. Sometimes, users do invalid operations on it or there are some exceptions that the application encounters when it outputs the errors. However, these outputs are not visible unless I run the application from command line using
java -jar myapp.jar
I wish to record all of these to a file in the form of a log but I am unable to find a function or object which is responsible for outputting these errors etc.
To simplify my explanation, assume that my application outputs number from 1 to 10 using a for loop and a Sytem.out command. How can I record everything that is output to the System?
Thanks
Agreed with above, you should use a logging framework. I prefer to use abstractions such as SLF4J.
In this example you can use log4j underneath. Here is an example log4j.properties file you can put in the classpath of your java application.
Using this config will create two log files with a rolling appender, printing all debug log output of any class that logs in the com.techtrip.mypackage to /tmp/mypackage.log.
Suppose a simple example class with a Logger and a Formatter:
This will log the setter output to the log file. Turn it off by simply changing the property file. Very simple.