使用hibernate连接配置logback

时间:2016-11-09 13:17:02

标签: java hibernate logback

我在项目中新添加了slf4j的logback。我想在db中写日志,但我应该在logback.xml文件中定义db connection和username。我希望logback使用hibernate的连接,而不是创建另一个连接。

我当前的logback.xml文件:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            [mtd] %5level [%d{yyyy-MM-dd HH:mm:ss}] %X{sessionId} %X{username} %logger{1} | %msg%n
        </Pattern>
    </layout>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>mtdRolling.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>mtdRolling.%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>180</maxHistory>
        <totalSizeCap>3GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <Pattern>
            [mtd] %5level [%d{yyyy-MM-dd HH:mm:ss}] %X{sessionId} %X{username} %logger{1} | %msg%n
        </Pattern>
    </encoder>
</appender>

<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
    <connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
        <driverClass>oracle.jdbc.OracleDriver</driverClass>
        <url>jdbc:oracle:thin:@DB_URL</url>
        <user>username</user>
        <password>password</password>
        <sqlDialect class="ch.qos.logback.core.db.dialect.OracleDialect" /> 
    </connectionSource>
</appender>

<root level="INFO">
    <appender-ref ref="STDOUT" />
</root>

<root level="INFO">
    <appender-ref ref="FILE" />
</root>

<root level="INFO">
    <appender-ref ref="DB" />
</root>

任何帮助都将非常感谢。

0 个答案:

没有答案