001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.core.layout; 018 019 import java.io.ByteArrayOutputStream; 020 import java.io.IOException; 021 import java.io.ObjectOutputStream; 022 import java.io.OutputStream; 023 import java.util.HashMap; 024 import java.util.Map; 025 026 import org.apache.logging.log4j.core.LogEvent; 027 import org.apache.logging.log4j.core.config.plugins.Plugin; 028 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 029 030 /** 031 * Formats a {@link LogEvent} in its Java serialized form. 032 */ 033 @Plugin(name = "SerializedLayout", category = "Core", elementType = "layout", printObject = true) 034 public final class SerializedLayout extends AbstractLayout<LogEvent> { 035 036 private static byte[] serializedHeader; 037 038 static { 039 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 040 try { 041 final ObjectOutputStream oos = new ObjectOutputStream(baos); 042 oos.close(); 043 serializedHeader = baos.toByteArray(); 044 } catch (final Exception ex) { 045 LOGGER.error("Unable to generate Object stream header", ex); 046 } 047 } 048 049 private SerializedLayout() { 050 } 051 052 /** 053 * Formats a {@link org.apache.logging.log4j.core.LogEvent} as a serialized byte array of the LogEvent object. 054 * 055 * @param event The LogEvent. 056 * @return the formatted LogEvent. 057 */ 058 @Override 059 public byte[] toByteArray(final LogEvent event) { 060 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 061 try { 062 final ObjectOutputStream oos = new PrivateObjectOutputStream(baos); 063 try { 064 oos.writeObject(event); 065 oos.reset(); 066 } finally { 067 oos.close(); 068 } 069 } catch (final IOException ioe) { 070 LOGGER.error("Serialization of LogEvent failed.", ioe); 071 } 072 return baos.toByteArray(); 073 } 074 075 /** 076 * Returns the LogEvent. 077 * 078 * @param event The Logging Event. 079 * @return The LogEvent. 080 */ 081 @Override 082 public LogEvent toSerializable(final LogEvent event) { 083 return event; 084 } 085 086 /** 087 * Creates a SerializedLayout. 088 * @return A SerializedLayout. 089 */ 090 @PluginFactory 091 public static SerializedLayout createLayout() { 092 return new SerializedLayout(); 093 } 094 095 @Override 096 public byte[] getHeader() { 097 return serializedHeader; 098 } 099 100 /** 101 * SerializedLayout's format is sufficiently specified via the content type, use empty Map/unspecified. 102 * @return empty Map 103 */ 104 @Override 105 public Map<String, String> getContentFormat() { 106 return new HashMap<String, String>(); 107 } 108 109 /** 110 * SerializedLayout returns a binary stream. 111 * @return The content type. 112 */ 113 @Override 114 public String getContentType() { 115 return "application/octet-stream"; 116 } 117 118 /** 119 * The stream header will be written in the Manager so skip it here. 120 */ 121 private class PrivateObjectOutputStream extends ObjectOutputStream { 122 123 public PrivateObjectOutputStream(final OutputStream os) throws IOException { 124 super(os); 125 } 126 127 @Override 128 protected void writeStreamHeader() { 129 } 130 } 131 }