UPLupdateSysProperties.log
2018-06-07 11:56:53
INFO UPLupdateSysProperties:12 -
Sample info message
2018-06-07 11:56:53
WARN UPLupdateSysProperties:13 -
Sample warn message
2018-06-07 11:56:53
ERROR UPLupdateSysProperties:14 - Sample error message
2018-06-07 11:56:53
FATAL UPLupdateSysProperties:15 - Sample fatal message
2018-06-07 12:10:49
INFO UPLupdateSysProperties:12 -
Sample info message
2018-06-07 12:10:49
WARN UPLupdateSysProperties:13 -
Sample warn message
2018-06-07 12:10:49
ERROR UPLupdateSysProperties:14 - Sample error message
2018-06-07 12:10:49
FATAL UPLupdateSysProperties:15 - Sample fatal message
|
Create java project and class. Put files log4j.properties and log4j.jar in "src" folder and add jar reference in build path.
modify log file location in file log4j.properties
log4j.appender.file.File=D:\\Logs\\UPLupdateSysProperties.log
This will create folder/s (if not exists), log file at specified location.
Whenever program get executed, new messages will get appended in log file.
UPLupdateSysProperties.java
package com.upl.utility;
import org.apache.log4j.Logger;
public class UPLupdateSysProperties {
final static Logger logger = Logger.getLogger(UPLupdateSysProperties.class);
public static void
main(String[] args){
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
System.out.println("dbc1");
}
}
|
log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file,
support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\Logs\\UPLupdateSysProperties.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
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
|
Output
2018-06-07 12:27:32 INFO UPLupdateSysProperties:12 - Sample info
message
2018-06-07 12:27:32 WARN UPLupdateSysProperties:13 - Sample warn
message
2018-06-07 12:27:32 ERROR
UPLupdateSysProperties:14 - Sample error message
2018-06-07 12:27:32 FATAL
UPLupdateSysProperties:15 - Sample fatal message
dbc1
|