Java web项目创建笔记11 之《配置logback.xml》

一、日志打印需要首先统一模块包名
webapp2_base:com.study.base
webapp2_domain:com.study.domain
webapp2_web:com.study.web
webapp2_module1:com.study.module1
webapp2_module2:com.sutdy.module2

二、日志实现原理
commons-logging -- jcl-over-slf4j(桥接到slf4j) -- slf4j-api -- 1.log4j日志实现
                                                                                           -- 2.logback日志实现(此项目用这个)

日志门面:
apache commons logging
slf4j

日志实现:
log4j
logback

三、webapp2-web模块下
在src/main/resources下建立,logback.xml

<configuration>
	<property name="log.base" value="/appserver/logs/webapp2" />
	<property name="pattern"
		value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{IP}] [%X{sessionId}] [%X{logId}] [%X{bizId}] %-5level [%thread] %logger{80} [%line]:%msg%n" />

	<!-- 控制台输出 -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>


	<!--logback根 -->
	<appender name="root"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.base}/webapp2-root.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.base}/%d{yyyy-MM-dd}/webapp2-root-%i.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>30MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>

	<!--异常信息 -->
	<appender name="webapp2-exception"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.base}/webapp2-exception.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.base}/%d{yyyy-MM-dd}/webapp2-exception-%i.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>30MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>

	<!-- web模块 -->
	<appender name="webapp2-web"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.base}/webapp2-web.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.base}/%d{yyyy-MM-dd}/webapp2-web-%i.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>30MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>

	<!-- module1模块 -->
	<appender name="webapp2-module1"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.base}/webapp2-module1.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.base}/%d{yyyy-MM-dd}/webapp2-module1-%i.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>30MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>

	<!-- module2模块 -->
	<appender name="webapp2-module2"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.base}/webapp2-module2.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${log.base}/%d{yyyy-MM-dd}/webapp2-module2-%i.%d{yyyy-MM-dd}.log.gz
			</fileNamePattern>
			<TimeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<MaxFileSize>30MB</MaxFileSize>
			</TimeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>

	<!-- 日志过滤 -->
	<logger name="org.springframework">
		<level value="error" />
	</logger>

	<logger name="org.apache">
		<level value="error" />
	</logger>

	<logger name="java.sql">
		<level value="error" />
	</logger>

	<logger name="org.mybatis">
		<level value="error" />
	</logger>


	<logger name="com.study.web" additivity="false">
		<level value="info" />
		<appender-ref ref="webapp2-web" />
	</logger>

	<logger name="com.sutdy.module1" additivity="false">
		<level value="info" />
		<appender-ref ref="webapp2_module1" />
	</logger>

	<logger name="com.sutdy.module2" additivity="false">
		<level value="info" />
		<appender-ref ref="webapp2_module2" />
	</logger>

	<logger name="com.study.base.exception" additivity="false">
		<level value="error" />
		<appender-ref ref="webapp2-exception" />
	</logger>

	<!-- root级别控制 -->
	<root level="trace">
		<appender-ref ref="root" />
	</root>
</configuration>

四、修改MyTest.java
引入Logger类,注意要引入org.slf4j包中的类

引入LoggerFactory类,注意要引入org.slf4j包中的类

package webapp;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.study.base.dao.TeacherMapper;
import com.study.base.util.ApplicationContextHolder;
import com.study.base.util.PropertiesUtil;
import com.study.base.util.ReflectInvokeUtil;
import com.study.base.vo.TeacherVo;
import com.study.domain.testBean.Student;
import com.study.domain.testBean.Teacher;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:spring-mvc.xml"})
@ActiveProfiles("dev")
public class MyTest {
	protected static Logger logger = LoggerFactory.getLogger(MyTest.class);

	@Value("${flag}")
	String flag;
	
	@Autowired
	Student stu;
	
	@Autowired
	TeacherMapper teacherMapper;
	
	@Test
	public void testInvoke() throws Exception {
		System.out.println("第一个测试方法*******");
		ApplicationContext ac = ApplicationContextHolder.getApplicationContext();
		ReflectInvokeUtil.invokeMethod("com.study.testBean.Student", "sayHello", null);
	}
	
	@Test
	public void testById() {
		System.out.println("第二个测试方法*******");
		ApplicationContext ac = ApplicationContextHolder.getApplicationContext();
		Student stu = (Student)ApplicationContextHolder.getBean("id.student1");
		stu.sayHello();
		System.out.println("**********");
	}
	
	@Test
	public void testProperties() {
		System.out.println("第三个测试方法*******");
		System.out.println("flag is:" + flag);
		System.out.println("appName is:" + PropertiesUtil.getProperties("appName"));
	}
	
	@Test
	public void testAutowired() {
		System.out.println("第四个测试方法*******");
		stu.sayHello();
		System.out.println("**********");
	}
	
	@Test
	public void testMybatis() {
		System.out.println("第五个测试方法*******");
		Teacher teacher = teacherMapper.getTeacherById(1);
		System.out.println("id is: " + teacher.getId());
		System.out.println("name is: " + teacher.getName());
	}
	
	@Test
	public void testPageable() {
		System.out.println("第六个测试方法*******");
		TeacherVo teacherVo = new TeacherVo();
		teacherVo.setId(2);
		teacherVo.setPageNum(5);
		teacherVo.setPageSize(4);
		List<Teacher> list = teacherMapper.getTeacherPageable(teacherVo);
		System.out.println("list size is:" + list.size());
	}
	
	@Test
	public void testLog() {
		int i = 1;
		logger.info("日志测试1: {}", i);
	}
}

五、查看日志打印
执行testLog方法

2019-06-19 22:09:12.142 [] [] [] [] DEBUG [main] com.study.base.util.PropertiesUtil [174]:Loading properties file from class path resource [env/application-dev.properties]
2019-06-19 22:09:12.763 [] [] [] [] INFO  [main] webapp.MyTest [93]:日志测试1: 1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值