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.util.HashMap;
20  import java.util.Map;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.io.ObjectOutputStream;
28  import java.io.OutputStream;
29  
30  /**
31   * Format a LogEvent in its serialized form.
32   */
33  @Plugin(name = "SerializedLayout", type = "Core", elementType = "layout", printObject = true)
34  public final class SerializedLayout extends AbstractLayout<LogEvent> {
35  
36      private static byte[] header;
37  
38      static {
39          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
40          try {
41              final ObjectOutputStream oos = new ObjectOutputStream(baos);
42              oos.close();
43              header = baos.toByteArray();
44          } catch (final Exception ex) {
45              LOGGER.error("Unable to generate Object stream header", ex);
46          }
47      }
48  
49      private SerializedLayout() {
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      public byte[] toByteArray(final LogEvent event) {
59          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
60          try {
61              final ObjectOutputStream oos = new PrivateObjectOutputStream(baos);
62              try {
63                  oos.writeObject(event);
64                  oos.reset();
65              } finally {
66                  oos.close();
67              }
68          } catch (final IOException ioe) {
69              LOGGER.error("Serialization of LogEvent failed.", ioe);
70          }
71          return baos.toByteArray();
72      }
73  
74      /**
75       * Returns the LogEvent.
76       *
77       * @param event The Logging Event.
78       * @return The LogEvent.
79       */
80      public LogEvent toSerializable(final LogEvent event) {
81          return event;
82      }
83  
84      /**
85       * Create a SerializedLayout.
86       * @return A SerializedLayout.
87       */
88      @PluginFactory
89      public static SerializedLayout createLayout() {
90  
91          return new SerializedLayout();
92      }
93  
94      @Override
95      public byte[] getHeader() {
96          return header;
97      }
98  
99      /**
100      * SerializedLayout's format is sufficiently specified via the content type, use empty Map/unspecified.
101      * @return empty Map
102      */
103     public Map<String, String> getContentFormat() {
104         return new HashMap<String, String>();    
105     }
106 
107     /**
108      * SerializedLayout returns a binary stream.
109      * @return The content type.
110      */
111     public String getContentType() {
112         return "application/octet-stream";
113     }
114 
115     /**
116      * The stream header will be written in the Manager so skip it here.
117      */
118     private class PrivateObjectOutputStream extends ObjectOutputStream {
119 
120         public PrivateObjectOutputStream(final OutputStream os) throws IOException {
121             super(os);
122         }
123 
124         @Override
125         protected void writeStreamHeader() {
126         }
127     }
128 }