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