PLC解密网-PLC培训学习-工控自动化人才技术交流

超级管理员

453

帖子

1378

回复

3110

积分

楼主
发表于 2020-03-29 13:49:52 | 查看: 13155 | 回复: 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



超级管理员

453

帖子

1378

回复

3110

积分
沙发
发表于 2020-03-29 13:51:46

但是我们在调试的模式,想看到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


超级管理员

453

帖子

1378

回复

3110

积分
板凳
发表于 2020-03-29 13:53:15

同时在pom.xml加上jar包


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

    <dependency>

        <groupId>log4j</groupId>

        <artifactId>log4j</artifactId>

        <version>1.2.17</version>

    </dependency>


超级管理员

453

帖子

1378

回复

3110

积分
地板
发表于 2020-03-29 13:55:27

调试可以有两种方法:

在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> 


超级管理员

453

帖子

1378

回复

3110

积分
4#
发表于 2020-03-29 13:58:05

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());

}

}


超级管理员

453

帖子

1378

回复

3110

积分
5#
发表于 2020-03-29 13:58:53

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());

}


}


超级管理员

453

帖子

1378

回复

3110

积分
6#
发表于 2020-03-29 14:07:09

测试效果:

image.png


image.png


image.png

超级管理员

453

帖子

1378

回复

3110

积分
7#
发表于 2020-03-29 17:43:46

插件 Log4E的使用:


Eclipse2019上安装的Log4E插件包de.jayefem.log4e_2.0.1,当前最新版本了。
安装前先解压缩,将de.jayefem.log4e_2.0.1文件夹里面的两个文件夹直接复制到Eclipse2019的【dropins】目录,完成Log4E插件的安装。

文件在此下载: 

de.jayefem.log4e_pro_2.0.1.zip


image.png

超级管理员

453

帖子

1378

回复

3110

积分
8#
发表于 2020-03-29 17:46:28

image.png

超级管理员

453

帖子

1378

回复

3110

积分
9#
发表于 2020-03-29 17:47:31

弹出窗口 :

image.png

超级管理员

453

帖子

1378

回复

3110

积分
10#
发表于 2020-03-29 17:48:58

image.png

您需要登录后才可以回帖 登录 | 立即注册

技术支持 KZYPLC V2.1 © 2020-2027

欢迎光临昆山中宇工控PLC论坛!您是第 6525439 位访问者, 日访问量: 1837 总访问量: 16045683,当前 2024-04-26 01:28:00 在线人数:99

ICP备案证书号: 苏ICP备14003016-2号