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