View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.monitoring;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  import java.io.PrintWriter;
27  import java.nio.channels.FileChannel;
28  import java.util.Enumeration;
29  import java.util.Set;
30  
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.io.IOUtils;
33  import org.apache.log4j.Appender;
34  import org.apache.log4j.FileAppender;
35  import org.apache.log4j.Logger;
36  
37  import com.google.common.collect.Sets;
38  
39  /**
40   * Utility functions for reading the log4j logs that are
41   * being written by HBase.
42   */
43  @InterfaceAudience.Private
44  public abstract class LogMonitoring {
45    public static Set<File> getActiveLogFiles() throws IOException {
46      Set<File> ret = Sets.newHashSet();
47      Appender a;
48      @SuppressWarnings("unchecked")
49      Enumeration<Appender> e = Logger.getRootLogger().getAllAppenders();
50      while (e.hasMoreElements()) {
51        a = e.nextElement();
52        if (a instanceof FileAppender) {
53          FileAppender fa = (FileAppender) a;
54          String filename = fa.getFile();
55          ret.add(new File(filename));
56        }
57      }
58      return ret;
59    }
60    
61  
62    public static void dumpTailOfLogs(
63        PrintWriter out, long tailKb) throws IOException {
64      Set<File> logs = LogMonitoring.getActiveLogFiles();
65      for (File f : logs) {
66        out.println("+++++++++++++++++++++++++++++++");
67        out.println(f.getAbsolutePath());
68        out.println("+++++++++++++++++++++++++++++++");
69        try {
70          dumpTailOfLog(f, out, tailKb);
71        } catch (IOException ioe) {
72          out.println("Unable to dump log at " + f);
73          ioe.printStackTrace(out);
74        }
75        out.println("\n\n");
76      }
77    }
78  
79    private static void dumpTailOfLog(File f, PrintWriter out, long tailKb)
80        throws IOException {
81      FileInputStream fis = new FileInputStream(f);
82      BufferedReader r = null;
83      try {
84        FileChannel channel = fis.getChannel();
85        channel.position(Math.max(0, channel.size() - tailKb*1024));
86        r = new BufferedReader(new InputStreamReader(fis));
87        r.readLine(); // skip the first partial line
88        String line;
89        while ((line = r.readLine()) != null) {
90          out.println(line);
91        }
92      } finally {
93        if (r != null) IOUtils.closeStream(r);
94        IOUtils.closeStream(fis);
95      }
96    }
97  }