java*工程 slf4j+logback实现日志记录

系统 1575 0

1.目录结构
java*工程 slf4j+logback实现日志记录

2.java测试码
    package com.test.main;     
     
import java.net.URL;

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;

     
public final class Boot {     
  
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{     
    	Logger logger = LoggerFactory.getLogger(Boot.class);
    	//从web.xml读配置
        ApplicationContext ctx = new ClassPathXmlApplicationContext("config/applicationContext.xml");     
       // A a = (A) ctx.getBean("a");     
       // a.sayHello();     
        EntityTestTwo a = (EntityTestTwo)ctx.getBean("entityTwo"); 
        //SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-ss");
        //a.setBeginDate(new Date());
       // a.setSex(222);
        a.setTel("11111111111");
       
        //logger.
        //String path = System.getProperty("user.dir");
        //BasicConfigurator.configure();
       // PropertyConfigurator.configure(new ClassPathResource("config/logback.properties").getURL()); 
       // System.out.println(new ClassPathResource("config/logback.properties").getURL());
        String logbackCfg = "config/logback.xml";   
        URL logURL = new ClassPathResource(logbackCfg).getURL();   
        System.out.println(logURL);
        ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();   
        LoggerContext loggerContext = (LoggerContext) loggerFactory;   
        //loggerContext.reset();   
        JoranConfigurator configurator = new JoranConfigurator();   
        configurator.setContext(loggerContext);   
        configurator.doConfigure(logURL);
        logger.debug("dddd");
        logger.info("wowowow");
        logger.error("yanzhengosss");
       // PropertyConfigurator.configure("/config/log4j.properties");
       
        //B b = (B) ctx.getBean("b");     
        //b.sayHi();  
        
        //
        /*String className="com.test.main.EntityTest";
        Class test = null;
		try {
			test = Class.forName(className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Method[] methods = test.getMethods();
        Set<Method> set = new HashSet<Method>(); 
        for(int i=0;i<methods.length;i++) 
        { 
        	boolean otherFlag = methods[i].isAnnotationPresent(AnotationTest.class); 
        	if(otherFlag) set.add(methods[i]); 
      	} 
      	for(Method m: set) 
      	{ 
      		AnotationTest anotationTest = m.getAnnotation(AnotationTest.class); 
      		System.out.println(anotationTest.regExp()); 
      		System.out.println("创建的社区:"+anotationTest.ifTip());
      	}*/
    }     
     
}
  

3.配置文件
    <?xml version="1.0" encoding="UTF-8" ?> 
<configuration>
  <Property name="log.base" value="./logs/anttesttools" /> 
 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <File>${log.base}.log</File> 
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
  <FileNamePattern>${log.base}.%i.log</FileNamePattern> 
  <MinIndex>1</MinIndex> 
  <MaxIndex>5</MaxIndex> 
  </rollingPolicy>
 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  <MaxFileSize>5MB</MaxFileSize> 
  </triggeringPolicy>
 <layout class="ch.qos.logback.classic.PatternLayout">
  <Pattern>%date [%thread] %-5level %logger{80} - %msg%n</Pattern> 
  </layout>
  </appender>
  <logger name="anttesttools" level="debug" /> 
 <root level="debug">
  <appender-ref ref="FILE" /> 
  </root>
  </configuration>
  

4.日志输出
    2009-11-22 17:27:57,192 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 17:30:16,470 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 17:30:16,470 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 17:30:16,470 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 17:37:58,262 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 17:37:58,278 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 17:37:58,278 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 17:38:15,049 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 17:38:15,064 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 17:38:15,064 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 17:43:37,366 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 17:43:37,366 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 17:43:37,366 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 17:44:09,270 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 17:44:09,270 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 17:44:09,286 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 17:44:58,741 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 17:44:58,741 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 17:44:58,741 [main] ERROR com.test.main.Boot - yanzhengosss
2009-11-22 18:26:29,220 [main] DEBUG com.test.main.Boot - dddd
2009-11-22 18:26:29,236 [main] INFO  com.test.main.Boot - wowowow
2009-11-22 18:26:29,236 [main] ERROR com.test.main.Boot - yanzhengosss

  

java*工程 slf4j+logback实现日志记录


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论