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