1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.layout;
18
19 import java.nio.charset.Charset;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.Map;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import org.apache.logging.log4j.core.Layout;
29 import org.apache.logging.log4j.core.LogEvent;
30 import org.apache.logging.log4j.core.config.Node;
31 import org.apache.logging.log4j.core.config.plugins.Plugin;
32 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
33 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
34 import org.apache.logging.log4j.core.net.Facility;
35 import org.apache.logging.log4j.core.net.Priority;
36 import org.apache.logging.log4j.core.util.NetUtils;
37
38
39
40
41
42 @Plugin(name = "SyslogLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
43 public final class SyslogLayout extends AbstractStringLayout {
44
45 private static final long serialVersionUID = 1L;
46
47
48
49
50 public static final Pattern NEWLINE_PATTERN = Pattern.compile("\\r?\\n");
51
52 private final Facility facility;
53 private final boolean includeNewLine;
54 private final String escapeNewLine;
55
56
57
58
59 private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss ", Locale.ENGLISH);
60
61
62
63 private final String localHostname = NetUtils.getLocalHostname();
64
65
66
67 protected SyslogLayout(final Facility facility, final boolean includeNL, final String escapeNL, final Charset charset) {
68 super(charset);
69 this.facility = facility;
70 this.includeNewLine = includeNL;
71 this.escapeNewLine = escapeNL == null ? null : Matcher.quoteReplacement(escapeNL);
72 }
73
74
75
76
77
78
79
80 @Override
81 public String toSerializable(final LogEvent event) {
82 final StringBuilder buf = new StringBuilder();
83
84 buf.append('<');
85 buf.append(Priority.getPriority(facility, event.getLevel()));
86 buf.append('>');
87 addDate(event.getTimeMillis(), buf);
88 buf.append(' ');
89 buf.append(localHostname);
90 buf.append(' ');
91
92 String message = event.getMessage().getFormattedMessage();
93 if (null != escapeNewLine) {
94 message = NEWLINE_PATTERN.matcher(message).replaceAll(escapeNewLine);
95 }
96 buf.append(message);
97
98 if (includeNewLine) {
99 buf.append('\n');
100 }
101 return buf.toString();
102 }
103
104 private synchronized void addDate(final long timestamp, final StringBuilder buf) {
105 final int index = buf.length() + 4;
106 buf.append(dateFormat.format(new Date(timestamp)));
107
108 if (buf.charAt(index) == '0') {
109 buf.setCharAt(index, ' ');
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 @Override
126 public Map<String, String> getContentFormat() {
127 final Map<String, String> result = new HashMap<String, String>();
128 result.put("structured", "false");
129 result.put("formatType", "logfilepatternreceiver");
130 result.put("dateFormat", dateFormat.toPattern());
131 result.put("format", "<LEVEL>TIMESTAMP PROP(HOSTNAME) MESSAGE");
132 return result;
133 }
134
135
136
137
138
139
140
141
142
143 @PluginFactory
144 public static SyslogLayout createLayout(
145 @PluginAttribute(value = "facility", defaultString = "LOCAL0") final Facility facility,
146 @PluginAttribute(value = "newLine", defaultBoolean = false) final boolean includeNewLine,
147 @PluginAttribute("newLineEscape") final String escapeNL,
148 @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charset) {
149 return new SyslogLayout(facility, includeNewLine, escapeNL, charset);
150 }
151 }