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.mapred; |
---|
20 | |
---|
21 | import java.util.LinkedList; |
---|
22 | import java.util.Queue; |
---|
23 | |
---|
24 | import org.apache.log4j.FileAppender; |
---|
25 | import org.apache.log4j.spi.LoggingEvent; |
---|
26 | |
---|
27 | /** |
---|
28 | * A simple log4j-appender for the task child's |
---|
29 | * map-reduce system logs. |
---|
30 | * |
---|
31 | */ |
---|
32 | public class TaskLogAppender extends FileAppender { |
---|
33 | private String taskId; //taskId should be managed as String rather than TaskID object |
---|
34 | //so that log4j can configure it from the configuration(log4j.properties). |
---|
35 | private int maxEvents; |
---|
36 | private Queue<LoggingEvent> tail = null; |
---|
37 | |
---|
38 | @Override |
---|
39 | public void activateOptions() { |
---|
40 | synchronized (this) { |
---|
41 | if (maxEvents > 0) { |
---|
42 | tail = new LinkedList<LoggingEvent>(); |
---|
43 | } |
---|
44 | setFile(TaskLog.getTaskLogFile(TaskAttemptID.forName(taskId), |
---|
45 | TaskLog.LogName.SYSLOG).toString()); |
---|
46 | setAppend(true); |
---|
47 | super.activateOptions(); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | @Override |
---|
52 | public void append(LoggingEvent event) { |
---|
53 | synchronized (this) { |
---|
54 | if (tail == null) { |
---|
55 | super.append(event); |
---|
56 | } else { |
---|
57 | if (tail.size() >= maxEvents) { |
---|
58 | tail.remove(); |
---|
59 | } |
---|
60 | tail.add(event); |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | public void flush() { |
---|
66 | qw.flush(); |
---|
67 | } |
---|
68 | |
---|
69 | @Override |
---|
70 | public synchronized void close() { |
---|
71 | if (tail != null) { |
---|
72 | for(LoggingEvent event: tail) { |
---|
73 | super.append(event); |
---|
74 | } |
---|
75 | } |
---|
76 | super.close(); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Getter/Setter methods for log4j. |
---|
81 | */ |
---|
82 | |
---|
83 | public String getTaskId() { |
---|
84 | return taskId; |
---|
85 | } |
---|
86 | |
---|
87 | public void setTaskId(String taskId) { |
---|
88 | this.taskId = taskId; |
---|
89 | } |
---|
90 | |
---|
91 | private static final int EVENT_SIZE = 100; |
---|
92 | |
---|
93 | public long getTotalLogFileSize() { |
---|
94 | return maxEvents * EVENT_SIZE; |
---|
95 | } |
---|
96 | |
---|
97 | public void setTotalLogFileSize(long logSize) { |
---|
98 | maxEvents = (int) logSize / EVENT_SIZE; |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|