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.logging.log4j.core.async; 018 019import java.util.Map; 020 021import org.apache.logging.log4j.Level; 022import org.apache.logging.log4j.Marker; 023import org.apache.logging.log4j.ThreadContext.ContextStack; 024import org.apache.logging.log4j.message.Message; 025 026import com.lmax.disruptor.EventTranslator; 027 028/** 029 * This class is responsible for writing elements that make up a log event into 030 * the ringbuffer {@code RingBufferLogEvent}. After this translator populated 031 * the ringbuffer event, the disruptor will update the sequence number so that 032 * the event can be consumed by another thread. 033 */ 034public class RingBufferLogEventTranslator implements 035 EventTranslator<RingBufferLogEvent> { 036 037 private AsyncLogger asyncLogger; 038 private String loggerName; 039 private Marker marker; 040 private String fqcn; 041 private Level level; 042 private Message message; 043 private Throwable thrown; 044 private Map<String, String> contextMap; 045 private ContextStack contextStack; 046 private String threadName; 047 private StackTraceElement location; 048 private long currentTimeMillis; 049 private long nanoTime; 050 051 // @Override 052 @Override 053 public void translateTo(final RingBufferLogEvent event, final long sequence) { 054 event.setValues(asyncLogger, loggerName, marker, fqcn, level, message, 055 thrown, contextMap, contextStack, threadName, location, 056 currentTimeMillis, nanoTime); 057 clear(); 058 } 059 060 /** 061 * Release references held by this object to allow objects to be 062 * garbage-collected. 063 */ 064 private void clear() { 065 setValues(null, // asyncLogger 066 null, // loggerName 067 null, // marker 068 null, // fqcn 069 null, // level 070 null, // data 071 null, // t 072 null, // map 073 null, // contextStack 074 null, // threadName 075 null, // location 076 0, // currentTimeMillis 077 0 // nanoTime 078 ); 079 } 080 081 public void setValues(final AsyncLogger asyncLogger, final String loggerName, 082 final Marker marker, final String fqcn, final Level level, final Message message, 083 final Throwable thrown, final Map<String, String> contextMap, 084 final ContextStack contextStack, final String threadName, 085 final StackTraceElement location, final long currentTimeMillis, final long nanoTime) { 086 this.asyncLogger = asyncLogger; 087 this.loggerName = loggerName; 088 this.marker = marker; 089 this.fqcn = fqcn; 090 this.level = level; 091 this.message = message; 092 this.thrown = thrown; 093 this.contextMap = contextMap; 094 this.contextStack = contextStack; 095 this.threadName = threadName; 096 this.location = location; 097 this.currentTimeMillis = currentTimeMillis; 098 this.nanoTime = nanoTime; 099 } 100}