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.util.HashMap; 020 import java.util.Map; 021 import org.apache.logging.log4j.core.LogEvent; 022 import org.apache.logging.log4j.core.config.plugins.Plugin; 023 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 024 025 import java.io.ByteArrayOutputStream; 026 import java.io.IOException; 027 import java.io.ObjectOutputStream; 028 import java.io.OutputStream; 029 030 /** 031 * Format a LogEvent in its serialized form. 032 */ 033 @Plugin(name = "SerializedLayout", type = "Core", elementType = "layout", printObject = true) 034 public final class SerializedLayout extends AbstractLayout<LogEvent> { 035 036 private static byte[] header; 037 038 static { 039 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 040 try { 041 final ObjectOutputStream oos = new ObjectOutputStream(baos); 042 oos.close(); 043 header = 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 public byte[] toByteArray(final LogEvent event) { 059 final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 060 try { 061 final ObjectOutputStream oos = new PrivateObjectOutputStream(baos); 062 try { 063 oos.writeObject(event); 064 oos.reset(); 065 } finally { 066 oos.close(); 067 } 068 } catch (final IOException ioe) { 069 LOGGER.error("Serialization of LogEvent failed.", ioe); 070 } 071 return baos.toByteArray(); 072 } 073 074 /** 075 * Returns the LogEvent. 076 * 077 * @param event The Logging Event. 078 * @return The LogEvent. 079 */ 080 public LogEvent toSerializable(final LogEvent event) { 081 return event; 082 } 083 084 /** 085 * Create a SerializedLayout. 086 * @return A SerializedLayout. 087 */ 088 @PluginFactory 089 public static SerializedLayout createLayout() { 090 091 return new SerializedLayout(); 092 } 093 094 @Override 095 public byte[] getHeader() { 096 return header; 097 } 098 099 /** 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 }