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 org.apache.logging.log4j.core.LogEvent;
20 import org.apache.logging.log4j.core.config.plugins.Plugin;
21 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
22 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
23 import org.apache.logging.log4j.core.net.Facility;
24 import org.apache.logging.log4j.core.net.Priority;
25 import org.apache.logging.log4j.util.EnglishEnums;
26
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.nio.charset.Charset;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.Locale;
33
34
35
36
37
38 @Plugin(name = "SyslogLayout", type = "Core", elementType = "layout", printObject = true)
39 public class SyslogLayout extends AbstractStringLayout {
40
41 private final Facility facility;
42 private final boolean includeNewLine;
43
44
45
46
47 private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss ", Locale.ENGLISH);
48
49
50
51 private final String localHostname = getLocalHostname();
52
53
54 protected SyslogLayout(Facility facility, boolean includeNL, Charset c) {
55 super(c);
56 this.facility = facility;
57 this.includeNewLine = includeNL;
58 }
59
60
61
62
63
64
65
66 public String toSerializable(final LogEvent event) {
67 StringBuilder buf = new StringBuilder();
68
69 buf.append("<");
70 buf.append(Priority.getPriority(facility, event.getLevel()));
71 buf.append(">");
72 addDate(event.getMillis(), buf);
73 buf.append(" ");
74 buf.append(localHostname);
75 buf.append(" ");
76 buf.append(event.getMessage().getFormattedMessage());
77 if (includeNewLine) {
78 buf.append("\n");
79 }
80 return buf.toString();
81 }
82
83
84
85
86
87
88
89
90 private String getLocalHostname() {
91 try {
92 InetAddress addr = InetAddress.getLocalHost();
93 return addr.getHostName();
94 } catch (UnknownHostException uhe) {
95 LOGGER.error("Could not determine local host name", uhe);
96 return "UNKNOWN_LOCALHOST";
97 }
98 }
99
100 private synchronized void addDate(final long timestamp, StringBuilder buf) {
101 int index = buf.length() + 4;
102 buf.append(dateFormat.format(new Date(timestamp)));
103
104 if (buf.charAt(index) == '0') {
105 buf.setCharAt(index, ' ');
106 }
107 }
108
109
110
111
112
113
114
115
116 @PluginFactory
117 public static SyslogLayout createLayout(@PluginAttr("facility") String facility,
118 @PluginAttr("newLine") String includeNL,
119 @PluginAttr("charset") String charset) {
120
121 Charset c = Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset();
122 if (charset != null) {
123 if (Charset.isSupported(charset)) {
124 c = Charset.forName(charset);
125 } else {
126 LOGGER.error("Charset " + charset + " is not supported for layout, using " + c.displayName());
127 }
128 }
129 boolean includeNewLine = includeNL == null ? false : Boolean.valueOf(includeNL);
130 Facility f = Facility.toFacility(facility, Facility.LOCAL0);
131 return new SyslogLayout(f, includeNewLine, c);
132 }
133 }