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.io.IOException;
20  import java.io.Writer;
21  import java.nio.charset.Charset;
22  
23  import org.apache.logging.log4j.core.LogEvent;
24  import org.apache.logging.log4j.core.config.Configuration;
25  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
26  import org.apache.logging.log4j.core.impl.MutableLogEvent;
27  import org.apache.logging.log4j.core.util.StringBuilderWriter;
28  import org.apache.logging.log4j.util.Strings;
29  
30  import com.fasterxml.jackson.core.JsonGenerationException;
31  import com.fasterxml.jackson.databind.JsonMappingException;
32  import com.fasterxml.jackson.databind.ObjectWriter;
33  
34  abstract class AbstractJacksonLayout extends AbstractStringLayout {
35  
36      protected static final String DEFAULT_EOL = "\r\n";
37      protected static final String COMPACT_EOL = Strings.EMPTY;
38  
39      protected final String eol;
40      protected final ObjectWriter objectWriter;
41      protected final boolean compact;
42      protected final boolean complete;
43  
44      protected AbstractJacksonLayout(final Configuration config, final ObjectWriter objectWriter, final Charset charset,
45              final boolean compact, final boolean complete, final boolean eventEol, final Serializer headerSerializer,
46              final Serializer footerSerializer) {
47          super(config, charset, headerSerializer, footerSerializer);
48          this.objectWriter = objectWriter;
49          this.compact = compact;
50          this.complete = complete;
51          this.eol = compact && !eventEol ? COMPACT_EOL : DEFAULT_EOL;
52      }
53  
54      /**
55       * Formats a {@link org.apache.logging.log4j.core.LogEvent}.
56       *
57       * @param event The LogEvent.
58       * @return The XML representation of the LogEvent.
59       */
60      @Override
61      public String toSerializable(final LogEvent event) {
62          final StringBuilderWriter writer = new StringBuilderWriter();
63          try {
64              toSerializable(event, writer);
65              return writer.toString();
66          } catch (final IOException e) {
67              // Should this be an ISE or IAE?
68              LOGGER.error(e);
69              return Strings.EMPTY;
70          }
71      }
72  
73      private static LogEvent convertMutableToLog4jEvent(final LogEvent event) {
74          // TODO Jackson-based layouts have certain filters set up for Log4jLogEvent.
75          // TODO Need to set up the same filters for MutableLogEvent but don't know how...
76          // This is a workaround.
77          return event instanceof MutableLogEvent
78                  ? ((MutableLogEvent) event).createMemento()
79                  : event;
80      }
81  
82      public void toSerializable(final LogEvent event, final Writer writer)
83              throws JsonGenerationException, JsonMappingException, IOException {
84          objectWriter.writeValue(writer, convertMutableToLog4jEvent(event));
85          writer.write(eol);
86          markEvent();
87      }
88  
89  }