|
| 1 | +package com.stackify.services; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | +import java.time.Month; |
| 5 | + |
| 6 | +import org.apache.logging.log4j.Level; |
| 7 | +import org.apache.logging.log4j.LogManager; |
| 8 | +import org.apache.logging.log4j.Logger; |
| 9 | +import org.apache.logging.log4j.core.Appender; |
| 10 | +import org.apache.logging.log4j.core.LoggerContext; |
| 11 | +import org.apache.logging.log4j.core.appender.FileAppender; |
| 12 | +import org.apache.logging.log4j.core.config.AppenderRef; |
| 13 | +import org.apache.logging.log4j.core.config.Configuration; |
| 14 | +import org.apache.logging.log4j.core.config.LoggerConfig; |
| 15 | +import org.apache.logging.log4j.core.layout.PatternLayout; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import com.stackify.models.User; |
| 19 | +import com.stackify.services.MyService; |
| 20 | + |
| 21 | +public class MyServiceTest { |
| 22 | + |
| 23 | + private static final Logger logger = LogManager.getLogger(MyServiceTest.class); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testService() { |
| 27 | + MyService myService = new MyService(); |
| 28 | + User user = new User("John", "john@yahoo.com"); |
| 29 | + user.setDateOfBirth(LocalDate.of(1980, Month.APRIL, 20)); |
| 30 | + logger.info("Age of user {} is {}", () -> user.getName(), () -> myService.calculateUserAge(user)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testColoredLogger() { |
| 35 | + logger.fatal("Fatal level message"); |
| 36 | + logger.error("Error level message"); |
| 37 | + logger.warn("Warn level message"); |
| 38 | + logger.info("Info level message"); |
| 39 | + logger.debug("Debug level message"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testRollingFileAppender() { |
| 44 | + Logger rfLogger = LogManager.getLogger("RollingFileLogger"); |
| 45 | + rfLogger.info("Json Message 1"); |
| 46 | + rfLogger.info("Json Message 2"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testProgrammaticConfig() { |
| 51 | + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); |
| 52 | + Configuration config = ctx.getConfiguration(); |
| 53 | + |
| 54 | + PatternLayout layout = PatternLayout.newBuilder().withConfiguration(config).withPattern("%d{HH:mm:ss.SSS} %level %msg%n").build(); |
| 55 | + |
| 56 | + Appender appender = FileAppender.newBuilder().setConfiguration(config).withName("programmaticFileAppender").withLayout(layout).withFileName("java.log").build(); |
| 57 | + appender.start(); |
| 58 | + config.addAppender(appender); |
| 59 | + AppenderRef ref = AppenderRef.createAppenderRef("programmaticFileAppender", null, null); |
| 60 | + AppenderRef[] refs = new AppenderRef[] { ref }; |
| 61 | + |
| 62 | + LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "programmaticLogger", "true", refs, null, config, null); |
| 63 | + |
| 64 | + loggerConfig.addAppender(appender, null, null); |
| 65 | + config.addLogger("programmaticLogger", loggerConfig); |
| 66 | + ctx.updateLoggers(); |
| 67 | + |
| 68 | + Logger pLogger = LogManager.getLogger("programmaticLogger"); |
| 69 | + pLogger.info("Programmatic Logger Message"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testCustomLevel() { |
| 74 | + Level myLevel = Level.forName("NEW_LEVEL", 350); |
| 75 | + logger.log(myLevel, "Custom Level Message"); |
| 76 | + |
| 77 | + logger.log(Level.getLevel("NEW_XML_LEVEL"), "Custom XML Level Message"); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments