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