关于logback:
http://www.cnblogs.com/warking/p/5710303.html
官方文档
使用logback:
idea创建maven项目
修改pom.xml添加依赖:
4.0.0
com.dylan.java
javacore
1.0-SNAPSHOT
org.slf4j
slf4j-api
1.7.21
ch.qos.logback
logback-core
1.1.7
ch.qos.logback
logback-classic
1.1.7
test
配置logback.xml:
[%-5level][%d{yyyy-MM-dd HH:mm:ss}][%F:%L] - %msg%n
UTF-8
${log.base}startup.log
${log.base}startup_%d{yyyy-MM-dd}.log
30
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
创建测试类:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Random;
/**
* Created by dylan-pc on 2017/10/23.
*/
public class TestLogback {
private static final transient Logger log= LoggerFactory.getLogger(TestLogback.class);
public static void main(String args[]){
//System.out.println("Hello World!");
log.info("程序开始!");
try {
for (int i=1; i<=20; i++){
int r1=(int)(Math.random()*10); //被除数
int r2=(int)(Math.random()*100+99); //除数
log.debug("第"+i+"次计算表达式为:"+r2+"/"+r1);
log.debug("计算结果:"+(r2/r1));
log.info("----------------------------------");
}
}catch (ArithmeticException e){
log.error("程序异常!除数等于0!");
//log.error(e.getStackTrace().toString());
}catch (Exception e){
log.error("程序异常!"+e.getMessage());
}
log.trace("程序结束!");
}
}
查看控制台和日志文件:
"C:\Program Files\Java\jdk1.7.0_80\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\program\IntelliJ IDEA 14.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_80\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext\zipfs.jar;E:\ideaProjects\test\javacore\target\test-classes;E:\ideaProjects\test\javacore\target\classes;D:\program\apache-maven-3.5.0\local-repo\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;D:\program\apache-maven-3.5.0\local-repo\ch\qos\logback\logback-core\1.1.7\logback-core-1.1.7.jar;D:\program\apache-maven-3.5.0\local-repo\ch\qos\logback\logback-classic\1.1.7\logback-classic-1.1.7.jar;D:\program\IntelliJ IDEA 14.1.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain TestLogback
22:37:31,675 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
22:37:31,675 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
22:37:31,675 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/ideaProjects/test/javacore/target/classes/logback.xml]
22:37:31,765 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
22:37:31,777 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/E:/ideaProjects/test/javacore/target/classes/logback.xml]
22:37:31,777 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 30 seconds
22:37:31,789 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
22:37:31,793 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [startup.console]
22:37:31,863 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
22:37:31,866 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [startup.file]
22:37:31,871 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@13:19 - no applicable action for [Encoding], current ElementPath is [[configuration][appender][Encoding]]
22:37:31,880 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1026546903 - No compression will be used
22:37:31,882 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1026546903 - Will use the pattern E:/ideaProjects/log/startup_%d{yyyy-MM-dd}.log for the active file
22:37:31,887 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'E:/ideaProjects/log/startup_%d{yyyy-MM-dd}.log'.
22:37:31,887 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
22:37:31,890 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Mon Oct 23 22:36:58 CST 2017
22:37:31,894 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - This appender no longer admits a layout as a sub-component, set an encoder instead.
22:37:31,894 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
22:37:31,894 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
22:37:31,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - Active log file name: E:/ideaProjects/log/startup.log
22:37:31,895 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[startup.file] - File property is set to [E:/ideaProjects/log/startup.log]
22:37:31,897 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
22:37:31,897 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [startup.console] to Logger[ROOT]
22:37:31,898 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
22:37:31,898 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [startup.console] to Logger[ROOT]
22:37:31,898 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
22:37:31,898 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [startup.file] to Logger[ROOT]
22:37:31,898 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
22:37:31,899 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@114bc6a3 - Registering current configuration as safe fallback point
[INFO ][2017-10-23 22:37:31][TestLogback.java:14] - 程序开始!
[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第1次计算表达式为:107/9
[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:11
[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------
[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第2次计算表达式为:143/9
[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:15
[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------
[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第3次计算表达式为:192/7
[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:27
[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------
[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第4次计算表达式为:163/4
[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:40
[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------
[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第5次计算表达式为:148/4
[DEBUG][2017-10-23 22:37:31][TestLogback.java:20] - 计算结果:37
[INFO ][2017-10-23 22:37:31][TestLogback.java:21] - ----------------------------------
[DEBUG][2017-10-23 22:37:31][TestLogback.java:19] - 第6次计算表达式为:193/0
[ERROR][2017-10-23 22:37:31][TestLogback.java:25] - 程序异常!除数等于0!
[DEBUG][2017-10-23 22:37:31][TestLogback.java:31] - 程序结束!
Process finished with exit code 0
start
...
19:39:14.928 [main] DEBUG TestLogback - 第9次计算表达式为:194/5
19:39:14.928 [main] DEBUG TestLogback - 计算结果:38
19:39:14.928 [main] INFO TestLogback - ----------------------------------
19:39:14.928 [main] DEBUG TestLogback - 第10次计算表达式为:160/2
19:39:14.928 [main] DEBUG TestLogback - 计算结果:80
19:39:14.928 [main] INFO TestLogback - ----------------------------------
19:44:39.451 [main] INFO TestLogback - 程序开始!
19:44:39.459 [main] DEBUG TestLogback - 第1次计算表达式为:116/8
19:44:39.459 [main] DEBUG TestLogback - 计算结果:14
19:44:39.459 [main] INFO TestLogback - ----------------------------------
19:44:39.459 [main] DEBUG TestLogback - 第2次计算表达式为:194/5
19:44:39.459 [main] DEBUG TestLogback - 计算结果:38
19:44:39.459 [main] INFO TestLogback - ----------------------------------
19:44:39.459 [main] DEBUG TestLogback - 第3次计算表达式为:109/3
19:44:39.459 [main] DEBUG TestLogback - 计算结果:36
19:44:39.459 [main] INFO TestLogback - ----------------------------------
19:44:39.459 [main] DEBUG TestLogback - 第4次计算表达式为:109/0
19:44:39.459 [main] ERROR TestLogback - 程序异常!
...