View Javadoc

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