Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
c955c4eadf
Binary file not shown.
|
@ -0,0 +1,35 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<name>logback</name>
|
||||
<artifactId>logback</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.logback;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Example {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Example.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
logger.info("Example log from {}", Example.class.getSimpleName());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.logback;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
private final Map<String, ILoggingEvent> eventMap = new HashMap<>();
|
||||
|
||||
private String prefix;
|
||||
|
||||
@Override
|
||||
protected void append(final ILoggingEvent event) {
|
||||
if (prefix == null || "".equals(prefix)) {
|
||||
addError("Prefix is not set for MapAppender.");
|
||||
return;
|
||||
}
|
||||
|
||||
eventMap.put(prefix + System.currentTimeMillis(), event);
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public void setPrefix(final String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public Map<String, ILoggingEvent> getEventMap() {
|
||||
return eventMap;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<configuration>
|
||||
|
||||
<appender name="map" class="com.baeldung.logback.MapAppender">
|
||||
<prefix>test</prefix>
|
||||
</appender>
|
||||
|
||||
<appender name="out" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="map"/>
|
||||
<appender-ref ref="out"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.logback;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MapAppenderIntegrationTest {
|
||||
|
||||
private Logger rootLogger;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
rootLogger = (Logger) LoggerFactory.getLogger("ROOT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception {
|
||||
rootLogger.info("Test from {}", this.getClass().getSimpleName());
|
||||
MapAppender appender = (MapAppender) rootLogger.getAppender("map");
|
||||
assertEquals(appender.getEventMap().size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoPrefixSet_whenLoggerEmitsEvent_thenAppenderReceivesNoEvent() throws Exception {
|
||||
rootLogger.info("Test from {}", this.getClass().getSimpleName());
|
||||
MapAppender appender = (MapAppender) rootLogger.getAppender("badMap");
|
||||
assertEquals(appender.getEventMap().size(), 0);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.logback;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import ch.qos.logback.core.BasicStatusManager;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class MapAppenderTest {
|
||||
|
||||
private LoggerContext ctx;
|
||||
|
||||
private MapAppender mapAppender = new MapAppender();
|
||||
|
||||
private LoggingEvent event;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
ctx = new LoggerContext();
|
||||
ctx.setName("test context");
|
||||
ctx.setStatusManager(new BasicStatusManager());
|
||||
mapAppender.setContext(ctx);
|
||||
mapAppender.setPrefix("prefix");
|
||||
event = new LoggingEvent("fqcn", ctx.getLogger("logger"), Level.INFO, "Test message for logback appender", null, new Object[0]);
|
||||
ctx.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
ctx.stop();
|
||||
mapAppender.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPrefixIsNull_thenMapAppenderDoesNotLog() throws Exception {
|
||||
mapAppender.setPrefix(null);
|
||||
mapAppender.append(event);
|
||||
assertTrue(mapAppender.getEventMap().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPrefixIsEmpty_thenMapAppenderDoesNotLog() throws Exception {
|
||||
mapAppender.setPrefix("");
|
||||
mapAppender.append(event);
|
||||
assertTrue(mapAppender.getEventMap().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLogMessageIsEmitted_thenMapAppenderReceivesMessage() throws Exception {
|
||||
mapAppender.append(event);
|
||||
assertEquals(mapAppender.getEventMap().size(), 1);
|
||||
mapAppender.getEventMap().forEach((k, v) -> assertTrue(k.startsWith("prefix")));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<configuration debug="true">
|
||||
|
||||
<appender name="map" class="com.baeldung.logback.MapAppender">
|
||||
<prefix>test</prefix>
|
||||
</appender>
|
||||
|
||||
<appender name="badMap" class="com.baeldung.logback.MapAppender"/>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="map"/>
|
||||
<appender-ref ref="badMap"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
1
pom.xml
1
pom.xml
|
@ -107,6 +107,7 @@
|
|||
<module>logging-modules/log-mdc</module>
|
||||
<module>logging-modules/log4j</module>
|
||||
<module>logging-modules/log4j2</module>
|
||||
<module>logging-modules/logback</module>
|
||||
<module>lombok</module>
|
||||
<!-- <module>kotlin</module>-->
|
||||
<module>mapstruct</module>
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<XMLTutorials>
|
||||
<tutorial tutId="01" type="xml">
|
||||
<title>XML with Dom4J</title>
|
||||
<description>XML handling with Dom4J</description>
|
||||
<date>14/06/2016</date>
|
||||
<author>Dom4J tech writer</author>
|
||||
</tutorial>
|
||||
</XMLTutorials>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<tutorials>
|
||||
<tutorial tutId="01" type="XML">
|
||||
<author>Jaxb author</author>
|
||||
<date>04/02/2015</date>
|
||||
<description>XML Binding with Jaxb</description>
|
||||
<title>XML with Jaxb</title>
|
||||
</tutorial>
|
||||
</tutorials>
|
Loading…
Reference in New Issue