[java论坛]log4j.properties文件配置及说明 查看:15419  回复:45

log4j.properties一般配置文件如下:


注意INFO是发布到网站站点时是INFO模式


log4j.rootLogger=INFO,Console,File


log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c]%m%n


log4j.appender.File=org.apache.log4j.RollingFileAppender 

log4j.appender.File.File=mybatis.log

log4j.appender.File.MaxFileSize=10MB

log4j.appender.File.Threshold=ALL

log4j.appender.File.layout=org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n


spring自身模块的测试功能如下:


package sy.test;


import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import sy.model.User;

import sy.service.UserServiceI;


//spring结合junit利用spring-test包测试


//1、加上注解或public class TestMybitsSpring extends SpringJUnit4ClassRunner

@RunWith(SpringJUnit4ClassRunner.class)

//2、或者@ContextConfiguration(locations = {"spring.xml" , "spring-mybatis.xml"})

//下面是更精确的写法:

//有了这两个注解,就不需要@Before和private ApplicationContext ac;

@ContextConfiguration(locations = {"classpath:spring.xml" , "classpath:spring-mybatis.xml"})

public class TestMybitsSpring {

//3、定义userService属性

//4、Alt+Shift+S生成get和set方法

private UserServiceI userService;

public UserServiceI getUserService() {

return userService;

}

//5、加@Autowired表示自动注入

@Autowired

public void setUserService(UserServiceI userService) {

this.userService = userService;

}


//6、@Test是junit测试方法的定义

@Test

public void test1() {

//7、通过User模型接受userService获取对象u

User u = userService.getUserById(1);

//8、利用java的System类打印u对象

System.out.println(u);

System.out.println(u.getName());

}


}


junit4测试方法如下:


package sy.test;



import org.junit.Before;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import sy.model.User;

import sy.service.UserServiceI;


public class TestMybits {

//1、定义ac和userService属性

private ApplicationContext ac;

private UserServiceI userService;

//2、@Before是junit测试所有测试方法前执行一次的方法,用他可以生成定义ac和userService对象

@Before

public void Before() {

//3、通过spring的上下文获得spring的service

//手动创建ApplicationContext对象ac

ac = new ClassPathXmlApplicationContext(new String[] {"spring.xml","spring-mybatis.xml"});

userService = (UserServiceI) ac.getBean("userService");

}

//4、@Test是junit测试方法的定义

@Test

public void test1() {

//5、通过User模型接受userService获取对象u

User u = userService.getUserById(1);

//6、利用java的System类打印u对象

System.out.println(u);

}

@Test

public void test2() {

//7、通过User模型接受userService获取对象u

User u = userService.getUserById(2);

//8、利用java的System类打印u对象

System.out.println(u);

//9、利用java的System类打印u对象的name

System.out.println(u.getName());

}

}


调试可以有两种方法:

在pom.xml加上下面两个配置


<!--junit4测试 -->

    <dependency>

  <groupId>junit</groupId>

  <artifactId>junit</artifactId>

  <version>4.13</version>

  <scope>test</scope>

</dependency> 


     <!--spring自身模块的测试功能 -->

     <dependency>

       <groupId>org.springframework</groupId>

       <artifactId>spring-test</artifactId>

       <version>${spring.version}</version>

     </dependency> 


同时在pom.xml加上jar包


<!--log4j/log4j 打印sql语句等调试用-->

    <dependency>

        <groupId>log4j</groupId>

        <artifactId>log4j</artifactId>

        <version>1.2.17</version>

    </dependency>


但是我们在调试的模式,想看到mibits打印SQL语句,已验证程序的正确性,及数据

一般如下配置:


log4j.rootLogger=DEBUG,Console,File


log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c]%m%n


log4j.appender.File=org.apache.log4j.RollingFileAppender 

log4j.appender.File.File=mybatis.log

log4j.appender.File.MaxFileSize=10MB

log4j.appender.File.Threshold=ALL

log4j.appender.File.layout=org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n


当前第5页/总5页 首页 上一页
ICP备案证书号: 苏ICP备14003016-2号