1. 程式人生 > >jFinal 使用slf4j列印sql及其它相關日誌

jFinal 使用slf4j列印sql及其它相關日誌

首先本例是使用Druid連結資料庫,並且是maven專案,使用logback 如果你和我一樣可以參考下 ~ - ~

在config類中配置如下:

 public void configConstant(Constants me) { 
        ...
        me.setLogFactory(new Slf4jLogFactory());

    }
Slf4jLogFactory類是繼承ILogFactory
public class Slf4jLogFactory implements ILogFactory {

    @Override
    public Log getLog(Class<?> aClass) {
        return new Slf4jLog(aClass);
    }

    @Override
    public Log getLog(String name) {
        return new Slf4jLog(name);
    }
}
Slf4jLog繼承Log
public class Slf4jLog extends Log{
    private Logger log;

    Slf4jLog(Class<?> clazz) {
        log = LoggerFactory.getLogger(clazz);
    }

    Slf4jLog(String name) {
        log = LoggerFactory.getLogger(name);
    }


    public void info(String message) {
        log.info(message);
    }

    public void info(String message, Throwable t) {
        log.info(message, t);
    }

    public void debug(String message) {
        log.debug(message);
    }

    public void debug(String message, Throwable t) {
        log.debug(message, t);
    }

    public void warn(String message) {
        log.warn(message);
    }

    public void warn(String message, Throwable t) {
        log.warn(message, t);
    }

    public void error(String message) {
        log.error(message);
    }

    public void error(String message, Throwable t) {
        log.error(message, t);
    }

    public void fatal(String message) {
        log.error(message);
    }

    public void fatal(String message, Throwable t) {
        log.error(message, t);
    }

    public boolean isDebugEnabled() {
        return log.isDebugEnabled();
    }

    public boolean isInfoEnabled() {
        return log.isInfoEnabled();
    }

    public boolean isWarnEnabled() {
        return log.isWarnEnabled();
    }

    public boolean isErrorEnabled() {
        return log.isErrorEnabled();
    }

    public boolean isFatalEnabled() {
        return log.isErrorEnabled();
    }
}
繼續回到config類:
public void configPlugin(Plugins me) {

        DruidPlugin dbPlugin = new DruidPlugin(
                PropKit.get("jdbcUrl"),
                PropKit.get("user"),
                PropKit.get("password"),
                PropKit.get("driverClass"));
        dbPlugin.addFilter(new StatFilter());
        dbPlugin.addFilter(new Slf4jLogFilter());
//設定引數,如果你想的話
        dbPlugin.setInitialSize(...);
        dbPlugin.setMaxActive(...);
...

        WallFilter wall = new WallFilter();
        wall.setDbType("你所使用的資料庫");//mysql等
        wall.setLogViolation(true);

        dbPlugin.addFilter(wall);
	
	ActiveRecordPlugin arp=new ActiveRecordPlugin(dbPlugin);	

	arp.setDialect(new SqlServerDialect());
	arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
	// 配置Model 對映//
	arp.addMapping("xxx",xxx.class);

        //新增到外掛列表中
	me.add(dbPlugin);
	me.add(arp);
    }

pom.xml 新增jar包

<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.9</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.9</version>
        </dependency>


logback.xml 可按日期每天生成日誌,如果日誌過多當達到設定大小後會建立一個新的
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="logHome" value="存放路徑"/> 例如/data/log/xx

    <!-- 過濾 -->
    <include resource="logback-application.xml" />

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <prudent>true</prudent>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${logHome}/你的檔名.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>100MB</maxFileSize>
            <maxHistory>60</maxHistory>
            <totalSizeCap>20GB</totalSizeCap>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>.honeylovelycom
        </layout>
    </appender>


    <logger name="切入的路徑" level="DEBUG"> //例如 com.baidu 
        <appender-ref ref="console"/>
        <appender-ref ref="rollingFile"/>
    </logger>

    <logger name="切入的路徑=" level="DEBUG">
        <appender-ref ref="console" />
        <appender-ref ref="rollingFile"/>
    </logger>

    <logger name="切入的路徑" level="INFO">
        <appender-ref ref="console" />
        <appender-ref ref="rollingFile" />
    </logger>

    <root level="DEBUG">
         <appender-ref ref="console" />
         <appender-ref ref="rollingFile" />
    </root>

    <logger level="debug" additivity="false">
    </logger>
</configuration>

logback-application.xml 過濾

<?xml version="1.0" encoding="UTF-8"?>
<included>
    <logger name="jdbc.sqltiming" level="DEBUG" />
    <logger name="jdbc" level="WARN" />
    <logger name="org.eclipse" level="WARN" />
    <logger name="druid.sql.ResultSet" level="WARN" />
    <logger name="druid.sql.Connection" level="WARN" />
    <logger name="io.netty" level="WARN" />
    <logger name="net.sf.ehcache" level="WARN" />
</included>