Log4j File Appenders - LOGGING TO A FILE
How to add Logs in File by using Log4j
There are various type of File appenders are available with log4j library. These include FileAppender which will log the message to a single file, and more advanced file-appenders such as RollingFileAppender that will automatically log message to a new file after reaching a pre-specified size.
How to add Logs in File by using Log4j
There are various type of File appenders are available with log4j library. These include FileAppender which will log the message to a single file, and more advanced file-appenders such as RollingFileAppender that will automatically log message to a new file after reaching a pre-specified size.
log4j.properties file
## Create a single appender called 'R' with ## a log level of 'INFO'. This will create new ## files whenever the current file reaches 100KB log4j.rootLogger=INFO, stdout log4j.appender.R=org.apache.log4j.RollingFileAppender # Path and file name to store the log file log4j.appender.R.File=D:/log/applog.log log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=2 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%r [%t] %5p (%F:%L) - %m%n
In the above log4j configuration, we are creating a new log4j appender with the name of rollingFile and setting it's options. In the above setting most of the options are self explanatory.The MaxFileSize tells the log4j to keep maximum size of the log file should be 100kb. The MaxBackupIndex tells the log4j to keep a maximum of 2 backup files for the log applog.log. If the log file is exceeding the MaximumFileSize, then the contents will be copied to a backup file and the logging will be added to a new empty applog.log file. From the above configuration, there can be a maximum of 2 backup files can be created.
logs will be generated in "D" drive inside "log" folder with the name "applog.log" because path is mentioned as "D:/log/applog.log" in the configuration.
Create Java Class to test log message
package com.jwt.log; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class Log4jDemo { static final Logger logger = Logger.getLogger(Log4jDemo.class); static final String LOG_PROPERTIES_FILE = "log4j.properties"; public static void main(String[] args) { new Log4jDemo(); // sample info log message logger.info("leaving the main method of Log4JDemo"); } public Log4jDemo() { initializeLogger(); // sample info log message logger.info("Log4jDemo - leaving the constructor ..."); } private void initializeLogger() { Properties logProperties = new Properties(); try { // load log4j properties configuration file logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE)); PropertyConfigurator.configure(logProperties); logger.info("Logging initialized."); } catch (IOException e) { logger.error("Unable to load logging property :", e); } try { FileInputStream fstream = new FileInputStream("D:\\textfile.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { System.out.println(strLine); } in.close(); } catch (FileNotFoundException fe) { logger.error("File Not Found", fe); logger.warn("This is a warning message"); logger.trace("This message will not be logged since log level is set as DEBUG"); } catch (IOException e) { logger.error("IOEXception occured:", e); } } }
After executing the program, applog.log file will be generated inside "D:/log" folder with the foolowing entry.
0 [main] INFO (Log4jDemo.java:38) - Logging initialized. 4 [main] ERROR (Log4jDemo.java:52) - File Not Found java.io.FileNotFoundException: D:\textfile.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at com.jwt.log.Log4jDemo.initializeLogger(Log4jDemo.java:43) at com.jwt.log.Log4jDemo.<init>(Log4jDemo.java:26) at com.jwt.log.Log4jDemo.main(Log4jDemo.java:20) 8 [main] WARN (Log4jDemo.java:53) - This is a warning message 8 [main] INFO (Log4jDemo.java:28) - Log4jDemo - leaving the constructor ... 8 [main] INFO (Log4jDemo.java:22) - leaving the main method of Log4JDemo