View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.layout;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
24  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25  import org.apache.logging.log4j.core.helpers.Charsets;
26  import org.apache.logging.log4j.core.net.Facility;
27  import org.apache.logging.log4j.core.net.Priority;
28  
29  import java.net.InetAddress;
30  import java.net.UnknownHostException;
31  import java.nio.charset.Charset;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  import java.util.Locale;
35  import java.util.regex.Matcher;
36  import java.util.regex.Pattern;
37  
38  
39  /**
40   * Formats a log event as a BSD Log record.
41   */
42  @Plugin(name = "SyslogLayout", type = "Core", elementType = "layout", printObject = true)
43  public class SyslogLayout extends AbstractStringLayout {
44      /**
45       * Match newlines in a platform-independent manner.
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       * Date format used if header = true.
55       */
56      private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd HH:mm:ss ", Locale.ENGLISH);
57      /**
58       * Host name used to identify messages from this appender.
59       */
60      private final String localHostname = getLocalHostname();
61  
62  
63  
64      protected SyslogLayout(final Facility facility, final boolean includeNL, final String escapeNL, final Charset c) {
65          super(c);
66          this.facility = facility;
67          this.includeNewLine = includeNL;
68          this.escapeNewLine = escapeNL == null ? null : Matcher.quoteReplacement(escapeNL);
69      }
70  
71      /**
72       * Formats a {@link org.apache.logging.log4j.core.LogEvent} in conformance with the BSD Log record format.
73       *
74       * @param event The LogEvent
75       * @return the event formatted as a String.
76       */
77      public String toSerializable(final LogEvent event) {
78          final StringBuilder buf = new StringBuilder();
79  
80          buf.append("<");
81          buf.append(Priority.getPriority(facility, event.getLevel()));
82          buf.append(">");
83          addDate(event.getMillis(), buf);
84          buf.append(" ");
85          buf.append(localHostname);
86          buf.append(" ");
87  
88          String message = event.getMessage().getFormattedMessage();
89          if (null != escapeNewLine) {
90              message = NEWLINE_PATTERN.matcher(message).replaceAll(escapeNewLine);
91          }
92          buf.append(message);
93  
94          if (includeNewLine) {
95              buf.append("\n");
96          }
97          return buf.toString();
98      }
99  
100     /**
101      * This method gets the network name of the machine we are running on.
102      * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name
103      * cannot be found.
104      *
105      * @return String the name of the local host
106      */
107     private String getLocalHostname() {
108         try {
109             final InetAddress addr = InetAddress.getLocalHost();
110             return addr.getHostName();
111         } catch (final UnknownHostException uhe) {
112             LOGGER.error("Could not determine local host name", uhe);
113             return "UNKNOWN_LOCALHOST";
114         }
115     }
116 
117     private synchronized void addDate(final long timestamp, final StringBuilder buf) {
118         final int index = buf.length() + 4;
119         buf.append(dateFormat.format(new Date(timestamp)));
120         //  RFC 3164 says leading space, not leading zero on days 1-9
121         if (buf.charAt(index) == '0') {
122             buf.setCharAt(index, ' ');
123         }
124     }
125 
126     /**
127      * SyslogLayout's content format is specified by:<p/>
128      * Key: "structured" Value: "false"<p/>
129      * Key: "dateFormat" Value: "MMM dd HH:mm:ss "<p/>
130      * Key: "format" Value: "<LEVEL>TIMESTAMP PROP(HOSTNAME) MESSAGE"<p/>
131      * Key: "formatType" Value: "logfilepatternreceiver" (format uses the keywords supported by LogFilePatternReceiver)
132      * @return Map of content format keys supporting SyslogLayout
133      */
134     public Map<String, String> getContentFormat()
135     {
136         Map<String, String> result = new HashMap<String, String>();
137         result.put("structured", "false");
138         result.put("formatType", "logfilepatternreceiver");
139         result.put("dateFormat", dateFormat.toPattern());
140         result.put("format", "<LEVEL>TIMESTAMP PROP(HOSTNAME) MESSAGE");
141         return result;
142     }
143 
144     /**
145      * Create a SyslogLayout.
146      * @param facility The Facility is used to try to classify the message.
147      * @param includeNL If true a newline will be appended to the result.
148      * @param escapeNL Pattern to use for replacing newlines.
149      * @param charsetName The character set.
150      * @return A SyslogLayout.
151      */
152     @PluginFactory
153     public static SyslogLayout createLayout(@PluginAttr("facility") final String facility,
154                                             @PluginAttr("newLine") final String includeNL,
155                                             @PluginAttr("newLineEscape") final String escapeNL,
156                                             @PluginAttr("charset") final String charsetName) {
157         final Charset charset = Charsets.getSupportedCharset(charsetName);
158         final boolean includeNewLine = includeNL == null ? false : Boolean.valueOf(includeNL);
159         final Facility f = Facility.toFacility(facility, Facility.LOCAL0);
160         return new SyslogLayout(f, includeNewLine, escapeNL, charset);
161     }
162 }