How to write log file in java using log4j ?
Logging is a very important aspect of any programming language.Logs file are very helpful in maintaining the applications from the developer point of view .Log4j is popular framework provided by Apache for handling the logs in Java applications.
In this tutorial, we are going to see how to generate logs for any application.So we are going to log INFO, DEBUG and ERROR in the application log.We can also use ALL, FATAL, OFF, TRACE and WARN according to application requirement.
Before that make sure you have created the folder for logs and updated the same in the properties file.
log4j.properties
--------------------------------------------------------------------------------------------------------------------------
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=D:/Logs/app.log
# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
-------------------------------------------------------------------------------------------------------------------------
Example Code
SampleProgram.java
package com.javainstance;
import java.util.Scanner;
import org.apache.log4j.Logger;
/**
* @author javainstance
*
*/
public class SampleProgram {
static Logger log = Logger.getLogger(SampleProgram.class.getName());
public static void main(String[] args) {
int result = printResult();
log.info("Output: " + result);
}
public static int printResult() {
int output = 0;
try {
Scanner scanf = new Scanner(System.in);
System.out.println("Enter input no");
int inputNo = scanf.nextInt();
log.debug("Input No :" + inputNo);
output = 5 / inputNo;
} catch (ArithmeticException e) {
e.printStackTrace();
log.error("Arithmetic Exception :" + e.getMessage());
}
return output;
}
}
In this tutorial, we are going to see how to generate logs for any application.So we are going to log INFO, DEBUG and ERROR in the application log.We can also use ALL, FATAL, OFF, TRACE and WARN according to application requirement.
- Download log4j.jar from https://logging.apache.org/log4j/1.2/download.html.
- Create the log4j.properties file according to the information to be logged. Below is the sample log4j.properties file.
Before that make sure you have created the folder for logs and updated the same in the properties file.
log4j.properties
--------------------------------------------------------------------------------------------------------------------------
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
# Set the name of the file
log4j.appender.FILE.File=D:/Logs/app.log
# Set the append to false, should not overwrite
log4j.appender.FILE.Append=true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
-------------------------------------------------------------------------------------------------------------------------
- Create a Java project and add log4j.jar into build path.
Example Code
SampleProgram.java
package com.javainstance;import java.util.Scanner;
import org.apache.log4j.Logger;
/**
* @author javainstance
*
*/
public class SampleProgram {
static Logger log = Logger.getLogger(SampleProgram.class.getName());
public static void main(String[] args) {
int result = printResult();
log.info("Output: " + result);
}
public static int printResult() {
int output = 0;
try {
Scanner scanf = new Scanner(System.in);
System.out.println("Enter input no");
int inputNo = scanf.nextInt();
log.debug("Input No :" + inputNo);
output = 5 / inputNo;
} catch (ArithmeticException e) {
e.printStackTrace();
log.error("Arithmetic Exception :" + e.getMessage());
}
return output;
}
}
Log File Generated:
app.log
2016-11-19 11:48:33 DEBUG SampleProgram:26 - Input No :6
2016-11-19 11:48:33 INFO SampleProgram:16 - Output: 0
2016-11-19 11:49:22 DEBUG SampleProgram:26 - Input No :9
2016-11-19 11:49:22 INFO SampleProgram:16 - Output: 0
2016-11-19 11:49:50 DEBUG SampleProgram:26 - Input No :0
2016-11-19 11:49:50 ERROR SampleProgram:33 - Arithmetic Exception :/ by zero
2016-11-19 11:49:50 INFO SampleProgram:16 - Output: 0
2016-11-19 11:51:44 DEBUG SampleProgram:26 - Input No :8
2016-11-19 11:51:44 INFO SampleProgram:16 - Output: 0
How to schedule a job in Spring 4 ?
Give your suggestion in comments.
If you like the post Share with your friends on social network.
Follow us: www.facebook.com/javainstance
Follow us: www.facebook.com/javainstance
How to write log file in java using log4j ?
Reviewed by JavaInstance
on
10:48:00 AM
Rating:
Reviewed by JavaInstance
on
10:48:00 AM
Rating:
No comments: