Skip to content

Commit

Permalink
2016-04-21日志输出框架改为Logback
Browse files Browse the repository at this point in the history
  • Loading branch information
judasn committed Apr 21, 2016
1 parent c3ed992 commit 5d7380f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 15 deletions.
45 changes: 33 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<mysql-connector.version>5.1.21</mysql-connector.version>
<druid.version>1.0.4</druid.version>

<slf4j.version>1.7.7</slf4j.version>
<!--<slf4j.version>1.7.7</slf4j.version>-->
<log4j.version>1.2.17</log4j.version>

<javaee-api.version>7.0</javaee-api.version>
Expand Down Expand Up @@ -270,23 +270,44 @@

<!-- 日志文件管理包 start -->
<!--下面这三个是配套使用:http://blog.csdn.net/woshiwxw765/article/details/7624556-->
<!--使用log4j start-->
<!--<dependency>-->
<!--<groupId>log4j</groupId>-->
<!--<artifactId>log4j</artifactId>-->
<!--<version>${log4j.version}</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-api</artifactId>-->
<!--<version>${slf4j.version}</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>org.slf4j</groupId>-->
<!--<artifactId>slf4j-log4j12</artifactId>-->
<!--<version>${slf4j.version}</version>-->
<!--</dependency>-->
<!--使用log4j end-->

<!--使用logback start-->
<!--导入这个的时候会自动导入:slf4j-api、logback-core,所以我们还需要导入:log4j和logback-ext-spring-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.4</version>
</dependency>
<!--使用logback end-->
<!-- 日志文件管理包 end -->


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import com.youmeek.ssm.module.user.pojo.SysUser;
import com.youmeek.ssm.module.user.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@RequestMapping("/sysUserController")
public class SysUserController {

static final Logger LOG = LoggerFactory.getLogger(SysUserController.class);

@Resource
private SysUserService sysUserService;

Expand All @@ -31,6 +37,17 @@ public SysUser showUser(@PathVariable("userId") Long userId){
}


@RequestMapping("/test-logback")
@ResponseBody
public Date testLogback(){
LOG.trace("-----------------------------------trace");
LOG.debug("-----------------------------------debug");
LOG.info("-----------------------------------info");
LOG.warn("-----------------------------------warn");
LOG.error("-----------------------------------error");
return new Date();
}




Expand Down
File renamed without changes.
77 changes: 77 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<!--参考文章:-->
<!--http://blog.csdn.net/wangjunjun2008/article/details/18732019-->
<!--http://blog.csdn.net/evankaka/article/details/50637994-->


<!--======================================================-->


<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
<!--Windows 可以使用类似:D:/log-->
<!--Linux 可以使用类似:/opt/tomcat/logs-->
<property name="LOG_HOME" value="D:/" />

<!--======================================================-->


<!--输出到控制台的设置-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] -- [%p] -- [%thread >>>> %F:%L >>>> Method = %M] -- [Content = %m]%n</Pattern>
</layout>
</appender>


<!-- 按照每天生成日志文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的路径+文件名-->
<FileNamePattern>${LOG_HOME}/logbackOutFile.log.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] -- [%p] -- [%thread >>>> %F:%L >>>> Method = %M] -- [Content = %m]%n</pattern>
</encoder>
<!--日志文件最大的大小-->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>

<!--======================================================-->

<!--级别:ALL > TRACE > DEBUG > INFO > WARN > ERROR-->
<!--对特定目录或是类进行指定级别输出,而不使用root的级别-->
<logger name="com.youmeek" level="TRACE"/>

<!--myibatis log configure-->
<logger name="com.apache.ibatis" level="TRACE"/>
<logger name="java.sql.Connection" level="DEBUG"/>
<logger name="java.sql.Statement" level="DEBUG"/>
<logger name="java.sql.PreparedStatement" level="DEBUG"/>


<!-- show parameters for hibernate sql 专为 Hibernate 定制 -->
<!--
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" />
<logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" />
<logger name="org.hibernate.SQL" level="DEBUG" />
<logger name="org.hibernate.engine.QueryParameters" level="DEBUG" />
<logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" />
-->

<!--======================================================-->

<!--默认所有级别是 debug,使用控制台和文件两种类型都进行输出输出,如果只要使用一种控制台输出的话,则下面把FILE那一行去掉即可-->
<root level="TRACE">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>



</configuration>
4 changes: 1 addition & 3 deletions src/test/java/com/youmeek/ssm/SSMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.youmeek.ssm.module.user.pojo.SysUser;
import com.youmeek.ssm.module.user.service.SysUserService;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -14,14 +13,13 @@
@ContextConfiguration(locations = {"classpath*:spring/applicationContext*.xml"})
public class SSMTest {

private Logger logger = Logger.getLogger(this.getClass().getName());

@Resource
private SysUserService sysUserService;

@Test
public void test1() {
SysUser sysUser = sysUserService.getById(1L);
logger.info("--------------------------------" + sysUser.toString());
System.out.println("--------------------------------" + sysUser.toString());
}
}

0 comments on commit 5d7380f

Please sign in to comment.