The whole idea Community Edition, I hope you also use the Community version. Features are not much less. Develop the habit of paying with the genuine, we are all programmers. Respect others ' success is respect oneself.
1. Create a new Mavena project
2, write several classes, and then create a new test class.
At this time Open Module Setting, to see libaries, found that has automatically introduced several JUnit maven dependency
At this moment, we want to use the log output, to slf4j, how to do
public class Hello {public
static Logger Logger = Loggerfactory.getlogger (hello.class);
public void xxx (String abc) {
logger.error (ABC);
}
} "
3, first put the above code about the log part of the code commented out
to Pom.xml, to add dependencies
<dependency>
<groupId>org.slf4j</groupId>
<artifactid>slf4j-log4j12</ artifactid>
<version>1.7.21</version>
<scope>compile</scope>
</ Dependency>
The above code, I input groupid, code hints automatically pop-up, the second is automatically pop-up. Version number: I found a random on the internet, as long as the right can. Scope: I chose to compile. can choose their own.
4, run test at this time, test can pass. But there is a clear hint that the appender can not be found. Because log4j is not configured, it is also configured.
5, create a new Log4j.propertier file, placed under the resource file (other tutorials can be placed directly under the SRC package, I tried not to), then run the test can be.
# # # set log Levels # # # Log4j.rootlogger = Debug, stdout, D, E # # # output to console # # # # log4j.appender.stdout = org.apache.log4j . Consoleappender Log4j.appender.stdout.Target = System.out Log4j.appender.stdout.layout = Org.apache.log4j.PatternLayout # # #log4j. Appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n # #
# Output to log file # # # LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender Log4j.appender.d.file = Logs/log.log Log4j.appender.d.append = True log4j.appender.d.threshold = Debug # # Output debug level above log log4j.appender.d.layout = Org.apache . log4j. Patternlayout Log4j.appender.d.layout.conversionpattern =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n # # # Save the Debug letter To a separate file # # LOG4J.APPENDER.D = Org.apache.log4j.DailyRollingFileAppender log4j.appender.d.file = logs/error.log # #
Exception Log file name Log4j.appender.d.append = True Log4j.appender.d.threshold = error # # Output only logs above the error level!!! Log4j.appender.d.layout = Org.apache.log4j.PatternLayout Log4j.appender.d.layout.conversionpattErn =%-d{yyyy-mm-dd HH:mm:ss} [%t:%r]-[%p]%m%n # # # Save exception information to a separate file # # # LOG4J.APPENDER.E = Org.apache.log4j.DailyRo Llingfileappender Log4j.appender.e.file = Logs/error.log Log4j.appender.e.append = True Log4j.appender.E.Threshold = ERROR log4j.appender.e.layout = org.apache.log4j.PatternLayout Log4j.appender.e.layout.conversionpattern =%d{ Yyyy-mm-dd HH:MM:SS} [%t:%r]-[%p]-%m%n
The logs are now working correctly