1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.web.appender;
18
19 import java.io.Serializable;
20
21 import javax.servlet.ServletContext;
22
23 import org.apache.logging.log4j.core.Filter;
24 import org.apache.logging.log4j.core.Layout;
25 import org.apache.logging.log4j.core.LogEvent;
26 import org.apache.logging.log4j.core.appender.AbstractAppender;
27 import org.apache.logging.log4j.core.config.Property;
28 import org.apache.logging.log4j.core.config.plugins.Plugin;
29 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
30 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
31 import org.apache.logging.log4j.core.layout.AbstractStringLayout;
32 import org.apache.logging.log4j.core.layout.PatternLayout;
33 import org.apache.logging.log4j.web.WebLoggerContextUtils;
34
35
36
37
38 @Plugin(name = "Servlet", category = "Core", elementType = "appender", printObject = true)
39 public class ServletAppender extends AbstractAppender {
40
41 public static class Builder<B extends Builder<B>> extends AbstractAppender.Builder<B>
42 implements org.apache.logging.log4j.core.util.Builder<ServletAppender> {
43
44 @PluginBuilderAttribute
45 private boolean logThrowables;
46
47 @Override
48 public ServletAppender build() {
49 final String name = getName();
50 if (name == null) {
51 LOGGER.error("No name provided for ServletAppender");
52 }
53 final ServletContext servletContext = WebLoggerContextUtils.getServletContext();
54 if (servletContext == null) {
55 LOGGER.error("No servlet context is available");
56 return null;
57 }
58 Layout<? extends Serializable> layout = getLayout();
59 if (layout == null) {
60 layout = PatternLayout.createDefaultLayout();
61 } else if (!(layout instanceof AbstractStringLayout)) {
62 LOGGER.error("Layout must be a StringLayout to log to ServletContext");
63 return null;
64 }
65 return new ServletAppender(name, layout, getFilter(), servletContext, isIgnoreExceptions(), logThrowables);
66 }
67
68
69
70
71
72
73 public boolean isLogThrowables() {
74 return logThrowables;
75 }
76
77
78
79
80 public void setLogThrowables(final boolean logThrowables) {
81 this.logThrowables = logThrowables;
82 }
83
84 }
85
86 @PluginBuilderFactory
87 public static <B extends Builder<B>> B newBuilder() {
88 return new Builder<B>().asBuilder();
89 }
90
91 private final ServletContext servletContext;
92 private final boolean logThrowables;
93
94 private ServletAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
95 final ServletContext servletContext, final boolean ignoreExceptions, final boolean logThrowables) {
96 super(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY);
97 this.servletContext = servletContext;
98 this.logThrowables = logThrowables;
99 }
100
101 @Override
102 public void append(final LogEvent event) {
103 final String serialized = ((AbstractStringLayout) getLayout()).toSerializable(event);
104 if (logThrowables) {
105 servletContext.log(serialized, event.getThrown());
106 } else {
107 servletContext.log(serialized);
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121 @Deprecated
122 public static ServletAppender createAppender(final Layout<? extends Serializable> layout, final Filter filter,
123 final String name, final boolean ignoreExceptions) {
124
125 return newBuilder().setFilter(filter).setIgnoreExceptions(ignoreExceptions).setLayout(layout).setName(name)
126 .build();
127
128 }
129
130 }