This project has retired. For details please refer to its Attic page.
TaskLogAppender xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.inputtools.log4j;
20  
21  import org.apache.hadoop.chukwa.datacollection.controller.ChukwaAgentController;
22  import org.apache.hadoop.chukwa.datacollection.controller.ClientFinalizer;
23  import org.apache.hadoop.chukwa.util.RecordConstants;
24  import org.apache.log4j.Logger;
25  import org.apache.log4j.spi.LoggingEvent;
26  
27  public class TaskLogAppender extends 
28      org.apache.hadoop.mapred.TaskLogAppender {
29    static Logger log = Logger.getLogger(TaskLogAppender.class);
30    static final String adaptorType = ChukwaAgentController.CharFileTailUTF8NewLineEscaped;
31    ChukwaAgentController chukwaClient;
32    String recordType = null;
33    static boolean chukwaClientIsNull = true;
34    static final Object chukwaLock = new Object();
35    private ClientFinalizer clientFinalizer = null;
36  
37    public String getRecordType() {
38      if (recordType != null)
39        return recordType;
40      else
41        return "unknown";
42    }
43  
44    public void setRecordType(String recordType) {
45      this.recordType = recordType;
46    }
47  
48    public void subAppend(LoggingEvent event) {
49        this.qw.write(RecordConstants.escapeAllButLastRecordSeparator("\n",this.layout.format(event)));
50        // Make sure only one thread can do this
51        // and use the boolean to avoid the first level locking
52        if (chukwaClientIsNull) {
53          synchronized (chukwaLock) {
54            if (chukwaClient == null) {
55              String log4jFileName = getFile();
56              String recordType = getRecordType();
57              long currentLength = 0L;
58              chukwaClient = new ChukwaAgentController();
59              chukwaClientIsNull = false;
60              String adaptorID = chukwaClient.add(ChukwaAgentController.CharFileTailUTF8NewLineEscaped,
61                recordType,currentLength + " " + log4jFileName, currentLength);
62  
63              // Setup a shutdownHook for the controller
64              clientFinalizer = new ClientFinalizer(chukwaClient);
65              Runtime.getRuntime().addShutdownHook(clientFinalizer);
66              if (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  }