diff --git a/guest/log4j2-example/.gitignore b/guest/log4j2-example/.gitignore new file mode 100644 index 0000000000..60be5b80aa --- /dev/null +++ b/guest/log4j2-example/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/guest/log4j2-example/java.log b/guest/log4j2-example/java.log new file mode 100644 index 0000000000..21e964d9ce --- /dev/null +++ b/guest/log4j2-example/java.log @@ -0,0 +1,3 @@ +14:00:35.258 INFO Programmatic Logger Message +14:03:51.178 INFO Programmatic Logger Message +14:04:11.753 INFO Programmatic Logger Message diff --git a/guest/log4j2-example/logs/app.log b/guest/log4j2-example/logs/app.log new file mode 100644 index 0000000000..022f6fcea9 --- /dev/null +++ b/guest/log4j2-example/logs/app.log @@ -0,0 +1,25 @@ +[ +{ + "timeMillis" : 1496315051753, + "thread" : "main", + "level" : "INFO", + "loggerName" : "RollingFileLogger", + "message" : "Json Message 1", + "endOfBatch" : false, + "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger", + "threadId" : 1, + "threadPriority" : 5 +} +, { + "timeMillis" : 1496315051862, + "thread" : "main", + "level" : "INFO", + "loggerName" : "RollingFileLogger", + "message" : "Json Messag 2", + "endOfBatch" : false, + "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger", + "threadId" : 1, + "threadPriority" : 5 +} + +] diff --git a/guest/log4j2-example/pom.xml b/guest/log4j2-example/pom.xml new file mode 100644 index 0000000000..197f3df2b9 --- /dev/null +++ b/guest/log4j2-example/pom.xml @@ -0,0 +1,56 @@ + + 4.0.0 + log4j2-example + log4j2-example + 0.0.1-SNAPSHOT + + + + + org.apache.logging.log4j + log4j-core + ${log4j-core.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + junit + junit + 4.12 + + + + + + + src/main/resources/ + + **/*.java + + + + + + maven-compiler-plugin + 3.5 + + 1.8 + 1.8 + + + + + + + 2.8.8.1 + 2.8.2 + + + \ No newline at end of file diff --git a/guest/log4j2-example/src/main/java/com/stackify/models/User.java b/guest/log4j2-example/src/main/java/com/stackify/models/User.java new file mode 100644 index 0000000000..1a53651292 --- /dev/null +++ b/guest/log4j2-example/src/main/java/com/stackify/models/User.java @@ -0,0 +1,43 @@ +package com.stackify.models; + +import java.time.LocalDate; + +public class User { + private String name; + private String email; + private LocalDate dateOfBirth; + + public User() { + } + + public User(String name, String email) { + super(); + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public LocalDate getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(LocalDate dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + +} diff --git a/guest/log4j2-example/src/main/java/com/stackify/services/MyService.java b/guest/log4j2-example/src/main/java/com/stackify/services/MyService.java new file mode 100644 index 0000000000..4b1dc0bf34 --- /dev/null +++ b/guest/log4j2-example/src/main/java/com/stackify/services/MyService.java @@ -0,0 +1,14 @@ +package com.stackify.services; + +import java.time.LocalDate; +import java.time.Period; + +import com.stackify.models.User; + +public class MyService { + + public int calculateUserAge(User user) { + return Period.between(user.getDateOfBirth(), LocalDate.now()).getYears(); + } + +} diff --git a/guest/log4j2-example/src/main/resources/log4j2.xml b/guest/log4j2-example/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..06f2ba5245 --- /dev/null +++ b/guest/log4j2-example/src/main/resources/log4j2.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/guest/log4j2-example/src/test/java/com/stackify/services/MyServiceTest.java b/guest/log4j2-example/src/test/java/com/stackify/services/MyServiceTest.java new file mode 100644 index 0000000000..49e367e45b --- /dev/null +++ b/guest/log4j2-example/src/test/java/com/stackify/services/MyServiceTest.java @@ -0,0 +1,81 @@ +package com.stackify.services; + +import java.time.LocalDate; +import java.time.Month; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.FileAppender; +import org.apache.logging.log4j.core.config.AppenderRef; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.junit.Test; + +import com.stackify.models.User; +import com.stackify.services.MyService; + +public class MyServiceTest { + + private static final Logger logger = LogManager.getLogger(MyServiceTest.class); + + @Test + public void testService() { + MyService myService = new MyService(); + User user = new User("John", "john@yahoo.com"); + user.setDateOfBirth(LocalDate.of(1980, Month.APRIL, 20)); + logger.info("Age of user {} is {}", () -> user.getName(), () -> myService.calculateUserAge(user)); + } + + @Test + public void testColoredLogger() { + logger.fatal("Fatal level message"); + logger.error("Error level message"); + logger.warn("Warn level message"); + logger.info("Info level message"); + logger.debug("Debug level message"); + } + + @Test + public void testRollingFileAppender() { + Logger rfLogger = LogManager.getLogger("RollingFileLogger"); + rfLogger.info("Json Message 1"); + rfLogger.info("Json Message 2"); + } + + @Test + public void testProgrammaticConfig() { + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + Configuration config = ctx.getConfiguration(); + + PatternLayout layout = PatternLayout.newBuilder().withConfiguration(config).withPattern("%d{HH:mm:ss.SSS} %level %msg%n").build(); + + Appender appender = FileAppender.newBuilder().setConfiguration(config).withName("programmaticFileAppender").withLayout(layout).withFileName("java.log").build(); + appender.start(); + config.addAppender(appender); + AppenderRef ref = AppenderRef.createAppenderRef("programmaticFileAppender", null, null); + AppenderRef[] refs = new AppenderRef[] { ref }; + + LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "programmaticLogger", "true", refs, null, config, null); + + loggerConfig.addAppender(appender, null, null); + config.addLogger("programmaticLogger", loggerConfig); + ctx.updateLoggers(); + + Logger pLogger = LogManager.getLogger("programmaticLogger"); + pLogger.info("Programmatic Logger Message"); + } + + @Test + public void testCustomLevel() { + Level myLevel = Level.forName("NEW_LEVEL", 350); + logger.log(myLevel, "Custom Level Message"); + + logger.log(Level.getLevel("NEW_XML_LEVEL"), "Custom XML Level Message"); + + } + +}