Log4j2 Setup
Log4j2 Setup
Pre-Requisite
1. Maven Or Jar of Log4J2
a. http://apachemirror.wuchna.com/logging/log4j/2.12.1/apache-log4j-2.12.1-bin.zip
(download jar from her) log4j-api-2.11.2.jar,
log4j-core-2.11.2.jar
b. Maven Dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
< dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
2. Log4j2.properties file with configured
properly (in com.package.resource folder)
status = error
name = PropertiesConfig
property.filename = C:\\logs\\logs.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = C:\\logs\\Previous\\debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval
= 1
appender.rolling.policies.time.modulate
= true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
#Make sure to change the package
structure as per your application
logger.rolling.name = javascientist.Log4JImpl
logger.rolling.level = debug
logger.rolling.additivity = true
logger.rolling.appenderRef.rolling.ref
= RollingFile
3.
Java Code with
Log4j2 Implementation:
package javascientist.Log4JImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class App
{
private static final Logger LOG = LogManager.getLogger(App.class);
public void logMethod() {
System.out.println("Start
Method");
LOG.debug("DEBUG
LOG");
LOG.info("INFO
LOG");
LOG.error("ERROR
LOG");
LOG.warn("WARN
LOG");
LOG.fatal("FATAL
LOG");
}
public static void main( String[] args )
{
System.out.println( "Hello
World!" );
App app = new App();
app.logMethod();
}
}
File
Folder Structure should be same :
YouTube Video Of avobe Tutorial: Click Here
nice
ReplyDeleteThanks
Delete