001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.yarn; 020 021import java.io.File; 022import java.io.Flushable; 023import java.util.LinkedList; 024import java.util.Queue; 025 026import org.apache.hadoop.classification.InterfaceAudience.Public; 027import org.apache.hadoop.classification.InterfaceStability.Unstable; 028import org.apache.log4j.FileAppender; 029import org.apache.log4j.spi.LoggingEvent; 030 031/** 032 * A simple log4j-appender for container's logs. 033 * 034 */ 035@Public 036@Unstable 037public class ContainerLogAppender extends FileAppender 038 implements Flushable 039{ 040 private String containerLogDir; 041 private String containerLogFile; 042 //so that log4j can configure it from the configuration(log4j.properties). 043 private int maxEvents; 044 private Queue<LoggingEvent> tail = null; 045 private boolean closing = false; 046 047 @Override 048 public void activateOptions() { 049 synchronized (this) { 050 if (maxEvents > 0) { 051 tail = new LinkedList<LoggingEvent>(); 052 } 053 setFile(new File(this.containerLogDir, containerLogFile).toString()); 054 setAppend(true); 055 super.activateOptions(); 056 } 057 } 058 059 @Override 060 public void append(LoggingEvent event) { 061 synchronized (this) { 062 if (closing) { // When closing drop any new/transitive CLA appending 063 return; 064 } 065 if (tail == null) { 066 super.append(event); 067 } else { 068 if (tail.size() >= maxEvents) { 069 tail.remove(); 070 } 071 tail.add(event); 072 } 073 } 074 } 075 076 @Override 077 public void flush() { 078 if (qw != null) { 079 qw.flush(); 080 } 081 } 082 083 @Override 084 public synchronized void close() { 085 closing = true; 086 if (tail != null) { 087 for (LoggingEvent event : tail) { 088 super.append(event); 089 } 090 } 091 super.close(); 092 } 093 094 /** 095 * Getter/Setter methods for log4j. 096 */ 097 098 public String getContainerLogDir() { 099 return this.containerLogDir; 100 } 101 102 public void setContainerLogDir(String containerLogDir) { 103 this.containerLogDir = containerLogDir; 104 } 105 106 public String getContainerLogFile() { 107 return containerLogFile; 108 } 109 110 public void setContainerLogFile(String containerLogFile) { 111 this.containerLogFile = containerLogFile; 112 } 113 114 private static final int EVENT_SIZE = 100; 115 116 public long getTotalLogFileSize() { 117 return maxEvents * EVENT_SIZE; 118 } 119 120 public void setTotalLogFileSize(long logSize) { 121 maxEvents = (int) logSize / EVENT_SIZE; 122 } 123}