1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.log4j.spi;
18
19 import org.apache.log4j.Logger;
20 import org.apache.log4j.ULogger;
21
22
23 /**
24 * Most log4j components derive from this class.
25 *
26 * @author Ceki Gulcu
27 */
28 public class ComponentBase implements Component {
29
30 /**
31 * Error count limit.
32 */
33 private static final int ERROR_COUNT_LIMIT = 3;
34
35 /**
36 * Logger repository.
37 */
38 protected LoggerRepository repository;
39 /**
40 * Logger.
41 */
42 private ULogger logger;
43 /**
44 * Error count.
45 */
46 private int errorCount = 0;
47
48 /**
49 * Construct a new instance.
50 */
51 protected ComponentBase() {
52 super();
53 }
54
55
56 /**
57 * Called by derived classes when they deem that the component has recovered
58 * from an erroneous state.
59 */
60 protected void resetErrorCount() {
61 errorCount = 0;
62 }
63
64 /**
65 * Set the owning repository. The owning repository cannot be set more than
66 * once.
67 *
68 * @param repository repository
69 */
70 public void setLoggerRepository(final LoggerRepository repository) {
71 if (this.repository == null) {
72 this.repository = repository;
73 } else if (this.repository != repository) {
74 throw new IllegalStateException("Repository has been already set");
75 }
76 }
77
78 /**
79 * Return the LoggerRepository to which this component is attached.
80 *
81 * @return Owning LoggerRepository
82 */
83 protected LoggerRepository getLoggerRepository() {
84 return repository;
85 }
86
87 /**
88 * Return an instance specific logger to be used by the component itself.
89 * This logger is not intended to be accessed by the end-user, hence the
90 * protected keyword.
91 * <p>
92 * <p>In case the repository for this component is not set,
93 * this implementations returns a {@link SimpleULogger} instance.
94 *
95 * @return A ULogger instance.
96 */
97 protected ULogger getLogger() {
98 if (logger == null) {
99 if (repository != null) {
100 Logger l = repository.getLogger(this.getClass().getName());
101 if (l instanceof ULogger) {
102 logger = (ULogger) l;
103 } else {
104 logger = new Log4JULogger(l);
105 }
106 } else {
107 logger = SimpleULogger.getLogger(this.getClass().getName());
108 }
109 }
110 return logger;
111 }
112
113 /**
114 * Frequently called methods in log4j components can invoke this method in
115 * order to avoid flooding the output when logging lasting error conditions.
116 *
117 * @return a regular logger, or a NOPLogger if called too frequently.
118 */
119 protected ULogger getNonFloodingLogger() {
120 if (errorCount++ >= ERROR_COUNT_LIMIT) {
121 return NOPULogger.NOP_LOGGER;
122 } else {
123 return getLogger();
124 }
125 }
126 }