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