eclipse 使用maven 构建springboot +log4j分析

虾米哥 阅读:237 2021-03-31 21:21:33 评论:0

 Spring boot 默认是使用logback来进行日志记录的,但是好多时候,还是喜欢使用log4j,当然Spring boot 也是支持Log4j记录日志的。

1、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
	<modelVersion>4.0.0</modelVersion> 
	<groupId>com.zzg</groupId> 
	<artifactId>myboot</artifactId> 
	<packaging>war</packaging> 
	<version>0.0.1-SNAPSHOT</version> 
	<name>myboot Maven Webapp</name> 
	<url>http://maven.apache.org</url> 
 
	<parent> 
		<groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-starter-parent</artifactId> 
		<version>1.4.3.RELEASE</version> 
	</parent> 
 
	<dependencies> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-web</artifactId> 
			<exclusions> 
				<exclusion> 
					<groupId>org.springframework.boot</groupId> 
					<artifactId>spring-boot-starter-logging</artifactId> 
				</exclusion> 
			</exclusions> 
		</dependency> 
		<dependency> 
			<groupId>junit</groupId> 
			<artifactId>junit</artifactId> 
			<version>3.8.1</version> 
			<scope>test</scope> 
		</dependency> 
		<!--引入swagger --> 
		<dependency> 
			<groupId>io.springfox</groupId> 
			<artifactId>springfox-swagger2</artifactId> 
			<version>2.2.2</version> 
		</dependency> 
		<dependency> 
			<groupId>io.springfox</groupId> 
			<artifactId>springfox-swagger-ui</artifactId> 
			<version>2.2.2</version> 
		</dependency> 
		<!--集成springJDBC 与JPA 开始 --> 
		<!-- 与数据库操作相关的依赖 --> 
		<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-jdbc</artifactId> 
		</dependency> 
 
		<!-- 使用数据源 --> 
		<dependency> 
			<groupId>com.alibaba</groupId> 
			<artifactId>druid</artifactId> 
			<version>1.0.14</version> 
		</dependency> 
 
		<!-- mysql --> 
		<dependency> 
			<groupId>mysql</groupId> 
			<artifactId>mysql-connector-java</artifactId> 
			<scope>runtime</scope> 
		</dependency> 
 
		<!-- mybatis --> 
		<dependency> 
			<groupId>org.mybatis</groupId> 
			<artifactId>mybatis</artifactId> 
			<version>3.2.8</version> 
		</dependency> 
		<dependency> 
			<groupId>org.mybatis</groupId> 
			<artifactId>mybatis-spring</artifactId> 
			<version>1.2.2</version> 
		</dependency> 
		<!--集成springJDBC 与JPA 结尾 --> 
		<!--集成 FastJson 开始 --> 
		<dependency> 
			<groupId>com.alibaba</groupId> 
			<artifactId>fastjson</artifactId> 
			<version>1.2.7</version> 
		</dependency> 
		<!--集成 FastJson 结尾 --> 
		<!--集成日志logback+slf4j --> 
		  <!-- Log4j -->   
     <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-log4j</artifactId> 
    <version>1.3.2.RELEASE</version> 
</dependency>  
 
	</dependencies> 
	<build> 
		<finalName>myboot</finalName> 
	</build> 
</project> 


重点提示:移除SpringBoot中默认的logback依赖,添加Log4j依赖。

<dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
      <exclusions> 
        <exclusion>  
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-logging</artifactId> 
        </exclusion> 
    </exclusions> 
    </dependency> 
    <!-- Log4j --> 
    <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-log4j</artifactId> 
    </dependency> 

spring-boot-starter-web依赖中需要exclude掉log4j-over-slf4j,因为spring-boot-starter-log4j中已经有了log4j的slf4j依赖,如果不去掉,会出现重复依赖的情况。这个时候,在初始化log4j的时候就会报出:

         Detected bothlog4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting


2.添加log4j的配置文件,在src的根目录下新建log4j.properties文件,springboot会自动加载该文件为log4j的配置文件,简单的配置如下:

# LOG4J 
log4j.rootCategory=INFO, stdout,file 
 
# print console 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender   
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout   
log4j.appender.stdout.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n 
 
# print file 
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log' 
log4j.appender.file.File=./logs/serial 
log4j.appender.file.layout=org.apache.log4j.PatternLayout  
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %p [%c]: %m%n

注意:其中‘./’表示根目录,日志文件名‘seial-2016-11-25.log’,当天日志文件名为‘serial’,logs文件夹缺省会自动创建(高版本(本人使用1.2.17)的log4j会创建缺省文件夹)

具体log4j的配置与使用,可以参考:http://blog.csdn.NET/yeruby/article/details/51719333

 

3.使用log4j记录打印日志

package com.zzg.myboot.controller; 
 
 
import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.zzg.myboot.domain.User; 
import com.zzg.myboot.service.UserService; 
 
import io.swagger.annotations.Api; 
import io.swagger.annotations.ApiOperation; 
 
@RestController 
@RequestMapping("/user") 
@Api("UserController相关api") 
public class UserController { 
	    private Logger logger = Logger.getLogger(UserController.class);   
		@Autowired 
	    private UserService service; 
		 
		@ApiOperation("获取user信息,测试多数据源") 
	    @RequestMapping(value = "/getUser", method = RequestMethod.GET) 
	    public User getShop(@RequestParam("id") String id) { 
			logger.info("传递参数:id ="+id); 
	        return service.selectByPrimaryKey(Long.parseLong(id)); 
	    } 
		 
		@ApiOperation("插入user信息,测试多数据源") 
	    @RequestMapping(value = "/insertUser", method = RequestMethod.GET) 
	    public int insertUser(@RequestParam("loginname") String loginname,@RequestParam("password") String password) { 
			User user = new User(); 
			user.setLoginname(loginname); 
			user.setPassword(password); 
	        return service.insertSelective(user); 
	    } 
		 
		 
 
} 

4.运行SpringBoot启动类的main方法,访问:http://localhost:8080/user/getUser?id=1,查看控制打印信息。




声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号