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.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.ObjectOutputStream;
22  import java.io.OutputStream;
23  
24  import org.apache.logging.log4j.core.Layout;
25  import org.apache.logging.log4j.core.LogEvent;
26  import org.apache.logging.log4j.core.config.Node;
27  import org.apache.logging.log4j.core.config.plugins.Plugin;
28  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
29  
30  /**
31   * Formats a {@link LogEvent} in its Java serialized form.
32   */
33  @Plugin(name = "SerializedLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
34  public final class SerializedLayout extends AbstractLayout<LogEvent> {
35  
36      private static byte[] serializedHeader;
37  
38      static {
39          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
40          try {
41              new ObjectOutputStream(baos).close();
42              serializedHeader = baos.toByteArray();
43          } catch (final Exception ex) {
44              LOGGER.error("Unable to generate Object stream header", ex);
45          }
46      }
47  
48      private SerializedLayout() {
49          super(null, null, null);
50      }
51  
52      /**
53       * Formats a {@link org.apache.logging.log4j.core.LogEvent} as a serialized byte array of the LogEvent object.
54       *
55       * @param event The LogEvent.
56       * @return the formatted LogEvent.
57       */
58      @Override
59      public byte[] toByteArray(final LogEvent event) {
60          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
61          try (final ObjectOutputStream oos = new PrivateObjectOutputStream(baos)) {
62              oos.writeObject(event);
63              oos.reset();
64          } catch (final IOException ioe) {
65              LOGGER.error("Serialization of LogEvent failed.", ioe);
66          }
67          return baos.toByteArray();
68      }
69  
70      /**
71       * Returns the LogEvent.
72       *
73       * @param event The Logging Event.
74       * @return The LogEvent.
75       */
76      @Override
77      public LogEvent toSerializable(final LogEvent event) {
78          return event;
79      }
80  
81      /**
82       * Creates a SerializedLayout.
83       * @return A SerializedLayout.
84       */
85      @PluginFactory
86      public static SerializedLayout createLayout() {
87          return new SerializedLayout();
88      }
89  
90      @Override
91      public byte[] getHeader() {
92          return serializedHeader;
93      }
94  
95      /**
96       * SerializedLayout returns a binary stream.
97       * @return The content type.
98       */
99      @Override
100     public String getContentType() {
101         return "application/octet-stream";
102     }
103 
104     /**
105      * The stream header will be written in the Manager so skip it here.
106      */
107     private class PrivateObjectOutputStream extends ObjectOutputStream {
108 
109         public PrivateObjectOutputStream(final OutputStream os) throws IOException {
110             super(os);
111         }
112 
113         @Override
114         protected void writeStreamHeader() {
115             // do nothing
116         }
117     }
118 }