发表于: 2020-07-11 21:18:15
1 1253
今天完成的事情:解决昨天一直没有运行的部署成功的问题。学习了logback和redis的java应用
明天计划的事情:深度思考
遇到的问题:暂无
收获:
logback配置学习
主要模块:Logback-access Logback-calssic Logback-core
主要标签:logger appender layout
1.在idea中引入依赖
<!--logback日志相关包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
2.编写logback.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!--定义参数常量-->
<!--TRACE<DEBUG<INFO<WARN<ERROR-->
<property name="log.level" value="debug" />
<property name="log.maxHistory" value="30"/>
<property name="log.filePath" value="${catalina.base}/logs/webapps"/>
<property name="log.pattern"
value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thead] %-5level %logger{50} - %msg%n"/>
<!--appender告诉输出的媒介-->
<!--控制台设置-->
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!--DEBUG-->
<appender name="debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--文件路径-->
<file>${log.filePath}/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件名称-->
<fileNamePattern>${log.filePath}/debug/debug.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!--文件最大保存历史数量-天-->
<maxHistory>${log.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--INFO-->
<appender name="infoAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--文件路径-->
<file>${log.filePath}/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件名称-->
<fileNamePattern>${log.filePath}/info/info.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!--文件最大保存历史数量-天-->
<maxHistory>${log.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--ERROR-->
<appender name="errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--文件路径-->
<file>${log.filePath}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--文件名称-->
<fileNamePattern>${log.filePath}/error/error.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!--文件最大保存历史数量-天-->
<maxHistory>${log.maxHistory}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<logger name="com.hyx" level="log.level" additivity="true">
<appender-ref ref="debugAppender"/>
<appender-ref ref="infoAppender"/>
<appender-ref ref="errorAppender"/>
</logger>
<root level="info">
<appender-ref ref="consoleAppender"/>
</root>
</configuration>
3.测试
@Test
public void memcachedTest(){
log.info("====start===");
long startTime = System.currentTimeMillis();
List list = studentService.selectAll();
System.out.println(list);
long endTime = System.currentTimeMillis();
log.info("执行时间:[{}ms]",endTime-startTime);
log.info("====end====");
}
java中应用redis
使用redis的流程介绍
4.1 导入redis客户端依赖
<!--redis客户端:Jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
4.2 引入spring中对redis连接池相关配置配置
redis.properties
redis.hostname=localhost
redis.port=6379
redis.database=0
redis.pool.maxActive=100
redis.pool.maxIdle=20
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
application.xml中配置
<!-- Redis连接池的设置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxTotal" value="${redis.pool.maxActive}" />
<!-- 连接池中最多可空闲maxIdle个连接 ,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常 -->
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<!-- 在获取连接的时候检查有效性 -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- 创建Redis连接池,并做相关配置 -->
<bean id="jedisWritePool" class="com.hyx.util.JedisPoolWriper"
depends-on="jedisPoolConfig">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.hostname}" />
<constructor-arg index="2" value="${redis.port}" type="int" />
</bean>
<!-- 创建Redis工具类,封装好Redis的连接以进行相关的操作 -->
<bean id="jedisUtil" class="com.hyx.util.JedisUtil" scope="singleton">
<property name="jedisPool">
<ref bean="jedisWritePool" />
</property>
</bean>
<!-- Redis的key操作 -->
<bean id="jedisKeys" class="com.hyx.util.JedisUtil$Keys" scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Strings操作 -->
<bean id="jedisStrings" class="com.hyx.util.JedisUtil$Strings"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
<!-- Redis的Lists操作 -->
<bean id="jedisLists" class="com.hyx.util.JedisUtil$Lists"
scope="singleton">
<constructor-arg ref="jedisUtil"></constructor-arg>
</bean>
评论