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 package org.apache.hadoop.log.metrics;
19 
20 import org.apache.hadoop.classification.InterfaceAudience;
21 import org.apache.hadoop.classification.InterfaceStability;
22 
23 import org.apache.log4j.AppenderSkeleton;
24 import org.apache.log4j.Level;
25 import org.apache.log4j.spi.LoggingEvent;
26 
27 /**
28  * A log4J Appender that simply counts logging events in three levels:
29  * fatal, error and warn. The class name is used in log4j.properties
30  */
31 @InterfaceAudience.Public
32 @InterfaceStability.Stable
33 public class EventCounter extends AppenderSkeleton {
34   private static final int FATAL = 0;
35   private static final int ERROR = 1;
36   private static final int WARN = 2;
37   private static final int INFO = 3;
38 
39   private static class EventCounts {
40     private final long[] counts = {0, 0, 0, 0};
41 
incr(int i)42     private synchronized void incr(int i) {
43       ++counts[i];
44     }
45 
get(int i)46     private synchronized long get(int i) {
47       return counts[i];
48     }
49   }
50 
51   private static EventCounts counts = new EventCounts();
52 
53   @InterfaceAudience.Private
getFatal()54   public static long getFatal() {
55     return counts.get(FATAL);
56   }
57 
58   @InterfaceAudience.Private
getError()59   public static long getError() {
60     return counts.get(ERROR);
61   }
62 
63   @InterfaceAudience.Private
getWarn()64   public static long getWarn() {
65     return counts.get(WARN);
66   }
67 
68   @InterfaceAudience.Private
getInfo()69   public static long getInfo() {
70     return counts.get(INFO);
71   }
72 
73   @Override
append(LoggingEvent event)74   public void append(LoggingEvent event) {
75     Level level = event.getLevel();
76     // depends on the api, == might not work
77     // see HADOOP-7055 for details
78     if (level.equals(Level.INFO)) {
79       counts.incr(INFO);
80     }
81     else if (level.equals(Level.WARN)) {
82       counts.incr(WARN);
83     }
84     else if (level.equals(Level.ERROR)) {
85       counts.incr(ERROR);
86     }
87     else if (level.equals(Level.FATAL)) {
88       counts.incr(FATAL);
89     }
90   }
91 
92   @Override
close()93   public void close() {
94   }
95 
96   @Override
requiresLayout()97   public boolean requiresLayout() {
98     return false;
99   }
100 }
101