1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.hadoop.chukwa.inputtools.log4j;
2021import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
22import org.apache.hadoop.chukwa.datacollection.controller.ClientFinalizer;
23import org.apache.hadoop.chukwa.util.RecordConstants;
24import org.apache.log4j.Logger;
25import org.apache.log4j.spi.LoggingEvent;
2627publicclassTaskLogAppenderextends28 org.apache.hadoop.mapred.TaskLogAppender {
29static Logger log = Logger.getLogger(TaskLogAppender.class);
30staticfinal String adaptorType = ChukwaAgentController.CharFileTailUTF8NewLineEscaped;
31ChukwaAgentController chukwaClient;
32 String recordType = null;
33staticboolean chukwaClientIsNull = true;
34staticfinal Object chukwaLock = new Object();
35privateClientFinalizer clientFinalizer = null;
3637public String getRecordType() {
38if (recordType != null)
39return recordType;
40else41return"unknown";
42 }
4344publicvoid setRecordType(String recordType) {
45this.recordType = recordType;
46 }
4748publicvoid subAppend(LoggingEvent event) {
49this.qw.write(RecordConstants.escapeAllButLastRecordSeparator("\n",this.layout.format(event)));
50// Make sure only one thread can do this51// and use the boolean to avoid the first level locking52if (chukwaClientIsNull) {
53synchronized (chukwaLock) {
54if (chukwaClient == null) {
55 String log4jFileName = getFile();
56 String recordType = getRecordType();
57long currentLength = 0L;
58 chukwaClient = newChukwaAgentController();
59 chukwaClientIsNull = false;
60 String adaptorID = chukwaClient.add(ChukwaAgentController.CharFileTailUTF8NewLineEscaped,
61 recordType,currentLength + " " + log4jFileName, currentLength);
6263// Setup a shutdownHook for the controller64 clientFinalizer = newClientFinalizer(chukwaClient);
65 Runtime.getRuntime().addShutdownHook(clientFinalizer);
66if (adaptorID != null) {
67 log.debug("Added file tailing adaptor to chukwa agent for file "68 + log4jFileName + ", adaptorId:" + adaptorID
69 + " using this recordType :" + recordType
70 + ", starting at offset:" + currentLength);
71 } else {
72 log.debug("Chukwa adaptor not added, addFile(" + log4jFileName
73 + ") returned " + adaptorID
74 + ", current offset:" + currentLength);
75 }
76 }
77 }
78 }
79 }
80 }