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 */ 017package org.apache.log4j.bridge; 018 019import org.apache.log4j.Layout; 020import org.apache.logging.log4j.core.LogEvent; 021import org.apache.logging.log4j.core.layout.ByteBufferDestination; 022 023import java.util.HashMap; 024import java.util.Map; 025 026/** 027 * Class Description goes here. 028 */ 029public class LayoutAdapter implements org.apache.logging.log4j.core.Layout<String> { 030 private Layout layout; 031 032 public LayoutAdapter(Layout layout) { 033 this.layout = layout; 034 } 035 036 037 @Override 038 public byte[] getFooter() { 039 return layout.getFooter() == null ? null : layout.getFooter().getBytes(); 040 } 041 042 @Override 043 public byte[] getHeader() { 044 return layout.getHeader() == null ? null : layout.getHeader().getBytes(); 045 } 046 047 @Override 048 public byte[] toByteArray(LogEvent event) { 049 String result = layout.format(new LogEventAdapter(event)); 050 return result == null ? null : result.getBytes(); 051 } 052 053 @Override 054 public String toSerializable(LogEvent event) { 055 return layout.format(new LogEventAdapter(event)); 056 } 057 058 @Override 059 public String getContentType() { 060 return layout.getContentType(); 061 } 062 063 @Override 064 public Map<String, String> getContentFormat() { 065 return new HashMap<>(); 066 } 067 068 @Override 069 public void encode(LogEvent event, ByteBufferDestination destination) { 070 final byte[] data = toByteArray(event); 071 destination.writeBytes(data, 0, data.length); 072 } 073}