I am new to logging and I am having an issue where the Logger stacks up its output in my console (NetBeans). This normally isn't a problem because the java program only is called one time, but out of curiosity from this point forward I am curious what I am doing wrong.
To duplicate the problem, if I run the JUnit test file for my program, I only initialize the class in:
MyClassTest{
MyClass instance = new MyClass(); // INITIALIZE MY CLASS WITH LOGGER
MyClassTest(){}
testMethod1(){}
testMethod2(){}
}
But for each my MyClass's methods (which have logging in them) when the test executes method n, n duplicates of the logging messages appear. This is clearly wrong.
Example:
Method1() {
logger.info("Applying Resources");
}
Method2() {
logger.info("Getting the entity of the URL response");
}
testMethod1 Output:
INFO root - Applying Resources (Strings), (Region)
0 [main] INFO root - Applying Resources
testMethod2 Output:
INFO root - Getting the entity of the URL response
455 [main] INFO root - Getting the entity of the URL response
455 [main] INFO root - Getting the entity of the URL response
This continues the more times I test the n'th method.
What am I missing here?
Code:
public class MyClass {
public Logger logger;
public MyClass() {
logger = null; //Debug, info, warning, error, fatal
logger = Logger.getRootLogger();
BasicConfigurator.configure();
logger.setLevel(Level.INFO);
}
}
Just in case it wasn't obvious from my comment above:
<Logger name="com.foo.Bar" level="trace" additivity="false">
Or
public void setAdditivity(boolean additive)
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html#setAdditivity(boolean)
Turns out that deleting this line solved my problem:
BasicConfigurator.configure();
With regard to setting additivity to false, this was a solution to removing the initial
INFO root - Applying Resources
Which was being written despite the stacking of the logging messages.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.