• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jhannes / logevents / #122

30 Sep 2025 08:22PM UTC coverage: 90.896% (-0.02%) from 90.912%
#122

push

jhannes-test
[maven-release-plugin] prepare for next development iteration

5841 of 6426 relevant lines covered (90.9%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/logevents-servlets-demo/src/main/java/org/logeventsdemo/servlets/DemoServer.java
1
package org.logeventsdemo.servlets;
2

3
import org.eclipse.jetty.server.Server;
4
import org.eclipse.jetty.util.resource.Resource;
5
import org.eclipse.jetty.webapp.WebAppContext;
6
import org.logevents.optional.servlets.LogEventsServlet;
7
import org.slf4j.LoggerFactory;
8
import org.slf4j.MDC;
9
import org.slf4j.Marker;
10
import org.slf4j.MarkerFactory;
11
import org.slf4j.event.Level;
12
import org.slf4j.spi.LocationAwareLogger;
13

14
import javax.servlet.DispatcherType;
15
import javax.servlet.ServletContextEvent;
16
import javax.servlet.ServletContextListener;
17
import java.io.File;
18
import java.io.IOException;
19
import java.net.URI;
20
import java.net.URL;
21
import java.util.EnumSet;
22
import java.util.Random;
23

24
public class DemoServer {
25

26
    private static final Marker OPS = MarkerFactory.getMarker("OPS");
1✔
27
    private static final Marker LIFECYCLE = MarkerFactory.getMarker("LIFECYCLE");
1✔
28
    private static final Marker SECRET = MarkerFactory.getMarker("SECRET");
1✔
29
    private static final Marker HTTP = MarkerFactory.getMarker("HTTP");
1✔
30
    static {
31
        OPS.add(LIFECYCLE); OPS.add(HTTP);
1✔
32
    }
33

34
    private static final LocationAwareLogger logger = (LocationAwareLogger) LoggerFactory.getLogger(DemoServer.class);
1✔
35
    private final Server server;
36

37
    public DemoServer(int httpPort) {
1✔
38
        server = new Server(httpPort);
1✔
39
        server.setHandler(demoContext());
1✔
40
        server.setRequestLog(new Slf4RequestLog());
1✔
41
    }
1✔
42

43
    public static void main(String[] args) throws Exception {
44
        DemoServer server = new DemoServer(Integer.parseInt(System.getProperty("httpPort", "4000")));
×
45
        server.start();
×
46
        logger.warn(LIFECYCLE, "Started server {}", server.getURI());
×
47
    }
×
48

49
    public URI getURI() {
50
        return server.getURI();
1✔
51
    }
52

53
    void start() throws Exception {
54
        new Thread(DemoServer::makeNoise).start();
1✔
55
        server.start();
1✔
56
    }
1✔
57

58
    private static WebAppContext demoContext() {
59
        WebAppContext context = new WebAppContext();
1✔
60
        context.setContextPath("/");
1✔
61

62
        URL webAppResource = DemoServer.class.getResource("/webapp-logevents");
1✔
63
        File webAppSource = new File(webAppResource.getPath().replaceAll("/target/classes/", "/src/main/resources/"));
1✔
64
        if (webAppSource.isDirectory()) {
1✔
65
            context.setBaseResource(Resource.newResource(webAppSource));
1✔
66
            context.getInitParams().put("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
1✔
67
        } else {
68
            context.setBaseResource(Resource.newResource(webAppResource));
×
69
        }
70

71
        context.addEventListener(new ApplicationContext());
1✔
72
        return context;
1✔
73
    }
74

75
    private static final Random random = new Random();
1✔
76

77
    @SafeVarargs
78
    private static <T> T pickOne(T... options) {
79
        return options[random.nextInt(options.length)];
1✔
80
    }
81

82
    private static void makeNoise() {
83
        try {
84
            while (true) {
85
                try {
86
                    if (random.nextInt(100) < 80) {
1✔
87
                        MDC.put("clientIp", pickOne("127.0.0.1", "10.10.0." + random.nextInt(255)));
1✔
88
                    }
89
                    if (random.nextInt(100) < 50) {
1✔
90
                        MDC.put("request", pickOne("/api/operation", "/api/resources", "/orders", "/customer/orders"));
×
91
                    }
92
                    if (random.nextInt(100) < 30) {
1✔
93
                        MDC.put("user", pickOne("alice", "bob", "charlie", "diana") + "@example." + pickOne("com", "org", "net"));
1✔
94
                    }
95
                    logger.log(
1✔
96
                            pickOne(OPS, SECRET, HTTP, null),
1✔
97
                            null,
98
                            pickOne(Level.ERROR, Level.WARN, Level.INFO).toInt(),
1✔
99
                            pickOne("Test message {}", "Other {} test message", "Even more", "Message with <a href='#'>Embedded HTML</a>"),
1✔
100
                            new Object[] { random.nextBoolean() ? random.nextInt(100) : "<a href='#'>A link</a>"},
1✔
101
                            random.nextInt(100) < 20 ? new IOException("Some error") : null);
1✔
102
                } finally {
103
                    MDC.clear();
×
104
                }
105
                Thread.sleep(random.nextInt(10000));
×
106
            }
107
        } catch (InterruptedException ignore) {
×
108

109
        }
110
    }
×
111

112
    private static class ApplicationContext implements ServletContextListener {
113
        @Override
114
        public void contextInitialized(ServletContextEvent sce) {
115
            sce.getServletContext().addFilter("logFilter", new LogEventServletFilter())
1✔
116
                    .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
1✔
117
            sce.getServletContext().addServlet("test", new TestServlet()).addMapping("/test/*");
1✔
118
            sce.getServletContext().addServlet("logs", new LogEventsServlet()).addMapping("/logs/*");
1✔
119

120
            sce.getServletContext().addServlet("swagger", new WebJarServlet("swagger-ui"))
1✔
121
                    .addMapping("/swagger/*");
1✔
122
        }
1✔
123

124
        @Override
125
        public void contextDestroyed(ServletContextEvent sce) {
126

127
        }
×
128
    }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc