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.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.nio.charset.Charset;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Locale;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import org.apache.logging.log4j.core.LogEvent;
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.helpers.Charsets;
35 import org.apache.logging.log4j.core.net.Facility;
36 import org.apache.logging.log4j.core.net.Priority;
37
38
39
40
41
42 @Plugin(name = "SyslogLayout", category = "Core", elementType = "layout", printObject = true)
43 public class SyslogLayout extends AbstractStringLayout {
44
45
46
47 public static final Pattern NEWLINE_PATTERN = Pattern.compile("\\r?\\n");
48
49 private final Facility facility;
50 private final boolean includeNewLine;
51 private final String escapeNewLine;
52
53
54
55
56 private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss ", Locale.ENGLISH);
57
58
59
60 private final String localHostname = getLocalHostname();
61
62
63
64 protected SyslogLayout(final Facility facility, final boolean includeNL, final String escapeNL, final Charset charset) {
65 super(charset);
66 this.facility = facility;
67 this.includeNewLine = includeNL;
68 this.escapeNewLine = escapeNL == null ? null : Matcher.quoteReplacement(escapeNL);
69 }
70
71
72
73
74
75
76
77 @Override
78 public String toSerializable(final LogEvent event) {
79 final StringBuilder buf = new StringBuilder();
80
81 buf.append("<");
82 buf.append(Priority.getPriority(facility, event.getLevel()));
83 buf.append(">");
84 addDate(event.getMillis(), buf);
85 buf.append(" ");
86 buf.append(localHostname);
87 buf.append(" ");
88
89 String message = event.getMessage().getFormattedMessage();
90 if (null != escapeNewLine) {
91 message = NEWLINE_PATTERN.matcher(message).replaceAll(escapeNewLine);
92 }
93 buf.append(message);
94
95 if (includeNewLine) {
96 buf.append("\n");
97 }
98 return buf.toString();
99 }
100
101
102
103
104
105
106
107
108 private String getLocalHostname() {
109 try {
110 final InetAddress addr = InetAddress.getLocalHost();
111 return addr.getHostName();
112 } catch (final UnknownHostException uhe) {
113 LOGGER.error("Could not determine local host name", uhe);
114 return "UNKNOWN_LOCALHOST";
115 }
116 }
117
118 private synchronized void addDate(final long timestamp, final StringBuilder buf) {
119 final int index = buf.length() + 4;
120 buf.append(dateFormat.format(new Date(timestamp)));
121
122 if (buf.charAt(index) == '0') {
123 buf.setCharAt(index, ' ');
124 }
125 }
126
127
128
129
130
131
132
133
134
135 @Override
136 public Map<String, String> getContentFormat()
137 {
138 final Map<String, String> result = new HashMap<String, String>();
139 result.put("structured", "false");
140 result.put("formatType", "logfilepatternreceiver");
141 result.put("dateFormat", dateFormat.toPattern());
142 result.put("format", "<LEVEL>TIMESTAMP PROP(HOSTNAME) MESSAGE");
143 return result;
144 }
145
146
147
148
149
150
151
152
153
154 @PluginFactory
155 public static SyslogLayout createLayout(
156 @PluginAttribute("facility") final String facility,
157 @PluginAttribute("newLine") final String includeNL,
158 @PluginAttribute("newLineEscape") final String escapeNL,
159 @PluginAttribute("charset") final String charsetName) {
160 final Charset charset = Charsets.getSupportedCharset(charsetName);
161 final boolean includeNewLine = Boolean.parseBoolean(includeNL);
162 final Facility f = Facility.toFacility(facility, Facility.LOCAL0);
163 return new SyslogLayout(f, includeNewLine, escapeNL, charset);
164 }
165 }