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