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 //so that log4j can configure it from the configuration(log4j.properties). 042 private int maxEvents; 043 private Queue<LoggingEvent> tail = null; 044 private boolean closing = false; 045 046 @Override 047 public void activateOptions() { 048 synchronized (this) { 049 if (maxEvents > 0) { 050 tail = new LinkedList<LoggingEvent>(); 051 } 052 setFile(new File(this.containerLogDir, "syslog").toString()); 053 setAppend(true); 054 super.activateOptions(); 055 } 056 } 057 058 @Override 059 public void append(LoggingEvent event) { 060 synchronized (this) { 061 if (closing) { // When closing drop any new/transitive CLA appending 062 return; 063 } 064 if (tail == null) { 065 super.append(event); 066 } else { 067 if (tail.size() >= maxEvents) { 068 tail.remove(); 069 } 070 tail.add(event); 071 } 072 } 073 } 074 075 @Override 076 public void flush() { 077 if (qw != null) { 078 qw.flush(); 079 } 080 } 081 082 @Override 083 public synchronized void close() { 084 closing = true; 085 if (tail != null) { 086 for (LoggingEvent event : tail) { 087 super.append(event); 088 } 089 } 090 super.close(); 091 } 092 093 /** 094 * Getter/Setter methods for log4j. 095 */ 096 097 public String getContainerLogDir() { 098 return this.containerLogDir; 099 } 100 101 public void setContainerLogDir(String containerLogDir) { 102 this.containerLogDir = containerLogDir; 103 } 104 105 private static final int EVENT_SIZE = 100; 106 107 public long getTotalLogFileSize() { 108 return maxEvents * EVENT_SIZE; 109 } 110 111 public void setTotalLogFileSize(long logSize) { 112 maxEvents = (int) logSize / EVENT_SIZE; 113 } 114}