1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
51
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
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 }