logback 项目应用

 

1、gradle引用:

compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.0.13'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.13'
compile group: 'org.logback-extensions', name: 'logback-ext-spring', version: '0.1.1'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.5'

2、logback.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

   <!-- 时间滚动输出 level为 DEBUG 日志 -->
    <appender name="file-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${catalina.base}/export/data/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
 
    <!-- 时间滚动输出 level为 INFO 日志 -->
    <appender name="file-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${catalina.base}/export/data/logs/info.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <!-- 时间滚动输出 level为 INFO 日志 -->
    <appender name="file-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY </onMismatch>
        </filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${catalina.base}/export/data/logs/error.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <!-- 很多人使用Mybatis的时候,控制台不能输出SQL语句,造成调试困难。只需要让DAO层的日志级别调整为DEBUG就可以了 -->
    <logger name="com.zhcx.*.*.mapper" level="DEBUG" />
    <logger name="com.zhcx.*.*.service.impl" level="DEBUG" />   
    <logger name="org.apache.activemq" level="ERROR" />
    <logger name="com.google.code" level="ERROR" />
    <logger name="net.rubyeye.xmemcached" level="ERROR" />
    <logger name="org.quartz.core" level="ERROR" />
    

    <!-- 指定项目可输出的最低级别日志 -->
    <root level="DEBUG">
        <appender-ref ref="console" />
        <appender-ref ref="file-debug" />
        <appender-ref ref="file-info" />
        <appender-ref ref="file-error" />
    </root>
</configuration>

 

3、java调用:

package com.dayhro.role.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayhr.web.util.PropertiesCommonUtil;
import com.dayhro.auth.response.UserInfo;
import com.dayhro.auth.util.UserUtil;
import com.dayhro.common.base.Data;
import com.dayhro.common.base.MessageResp;
import com.dayhro.common.pagination.PageUtil;
import com.dayhro.common.util.CommonUtils;
import com.dayhro.common.util.UUIDUtil;
import com.dayhro.role.entity.HroRole;
import com.dayhro.role.param.HroRoleParam;
import com.dayhro.role.response.HroRoleResp;
import com.dayhro.role.service.IHroRoleService;

/**
 * 
 * @ClassName:HroRoleController
 * @Description: 角色控制器
 * @author: 
 * @date:2016年9月1日 下午7:19:51 
 * @version
 */
@Controller
@RequestMapping("/hrorole")
public class HroRoleController {
    
    protected final Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    private IHroRoleService hroRoleService; //角色信息
    
    /**
     * 
     * @Title: queryList 
     * @Description: 查询角色信息 
     * @param @param param
     * @param @param request
     * @param @return
     * @return Data
     * @throws
     */
    @ResponseBody
    @RequestMapping("/queryList")
    public Data queryList(HroRoleParam param, HttpServletRequest request){
        
        Data data = null;
        UserInfo user = UserUtil.getUser(request.getSession());
        param.setCorpId(user.getCorpId());
        param.setModelId(Long.valueOf(user.getCorpType()));
        
        try{
            
            int totalCount = hroRoleService.queryListCount(param); //查询角色总数
            
            // 获取偏移量offSet
            int offSet = PageUtil.setParameter(totalCount,param.getPageBean());
            // 获取每一页显示的条数
            int pageSize = Integer.valueOf(param.getPageBean().getPageSize());
            data = new Data(param.getPageBean());
            
            //排序处理,防止SQL注入
            if(!"".equals(param.getOrderBy()) && param.getOrderBy()!=null){
                param.setOrderBy(PropertiesCommonUtil.getValue("/properties/order.properties", "hrorole."+param.getOrderBy()));
                param.setSortType("ASC");
            }
            RowBounds row = new RowBounds(offSet, pageSize);
            List<HroRoleResp> list = hroRoleService.queryList(param, row); //查询角色信息
            
            data.setBeans(list);
        
        } catch (Exception e) {
            log.error("查询异常", e);
        }
    
        return data; 
    }
}

 

posted @ 2016-10-19 20:43  mingyue1818  阅读(910)  评论(0编辑  收藏  举报