Reputation: 33
I need to create an accessible log file in our linux server. A java program is used to create a log.
In my console
Sep 11, 2014 3:03:05 PM com.gsis.bom.Log appLog
SEVERE: test 1
Sep 11, 2014 3:03:06 PM com.gsis.bom.Log appLog
INFO: test 2
Sep 11, 2014 3:03:06 PM com.gsis.bom.Log appLog
INFO: test 3
But this messages should be saved in a file in linux. For e.g. /home/logs
How can I do it?
THANK YOU
EDIT
LogManager lm = LogManager.getLogManager();
Logger logger;
FileHandler fh = new FileHandler("log_test.txt");
logger = Logger.getLogger("LoggingExample1");
lm.addLogger(logger);
logger.setLevel(Level.INFO);
fh.setFormatter(new XMLFormatter());
logger.addHandler(fh);
logger.log(Level.SEVERE, "test 1");
logger.log(Level.INFO, "test 2");
logger.log(Level.INFO, "test 3");
fh.close();
This is the code that I am using. Ok I can see it in my console. But I need the messages to be saved in a file. For example at /home/logs/log.txt in our linux server
Upvotes: 0
Views: 2462
Reputation: 42020
Seems that you are using java.util.logging. In that case, you can add an appender programmatically as in the following example.
// the maximum number of bytes to write to any one file
int limit = 5 * 1024 * 1024;
// the number of files to use
int count = 10;
Handler handler = new FileHandler("/home/logs/log_test.txt", limit, count);
Logger.getLogger("").addHandler(handler);
You may want to see Where does java.util.logging.Logger store their log, Java logging opening "too many" logfiles and Java Logging API generating empty log files
Upvotes: 0
Reputation: 3405
You can use class something like below,
import org.apache.log4j.Logger;
public class CreateLog{
static Logger logEntry = Logger.getLogger(
CreateLog.class.getName());
public void testLog(){
logEntry.info("some information message");
logEntry .error("some error message");
}
}
Upvotes: 2