0

我正在春季 4.3.25 构建开发环境。我想在 Eclipse 控制台中查看 ms-sqlserver sql 日志。所以我设置了 log4j2 和 log4jdbc-log4j2 但是控制台中的日志没有显示参数绑定和多行 sql 格式。

我的控制台日志

15:48:06.100 [http-nio-8080-exec-3] DEBUG - Creating a new SqlSession

15:48:06.107 [http-nio-8080-exec-3] DEBUG - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c632135]

15:48:06.117 [http-nio-8080-exec-3] DEBUG - JDBC Connection [ConnectionID:1 ClientConnectionId: 14db49d2-ed3c-4ff8-854a-dded463f9162] will be managed by Spring

15:48:06.123 [http-nio-8080-exec-3] DEBUG - ==>  Preparing: SELECT ID, NAME, DESCRIPTION, USE_YN FROM SAMPLE WHERE 'value'=?     

15:48:06.178 [http-nio-8080-exec-3] DEBUG - ==> Parameters: value(String)

15:48:06.245 [http-nio-8080-exec-3] DEBUG - <==      Total: 3

15:48:06.254 [http-nio-8080-exec-3] DEBUG - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c632135]

15:48:06.254 [http-nio-8080-exec-3] DEBUG - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c632135]

15:48:06.254 [http-nio-8080-exec-3] DEBUG - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c632135]

15:48:06.254 [http-nio-8080-exec-3] DEBUG - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c632135]

我不知道为什么?字符没有被实际参数值替换,为什么记录的 sql 字符串只显示在一行中。

我的配置如下:

#pom.xml

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.14.1</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
</dependency>

<dependency> 
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>

<dependency>    
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.30</version>
    <scope>test</scope>
</dependency>

<dependency>    
    <groupId>org.bgee.log4jdbc-log4j2</groupId> 
    <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>    
    <version>1.16</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId> 
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.14.1</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>9.2.1.jre8</version>
</dependency>

#log4j2.xml

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %level - %m%n%ex%n"/>
    </Console>
    <RollingFile name="File_Appender" fileName="logs/${logNm}.log" filePattern="logs/${logNm}_%d{yyyy-MM-dd}_%i.log.gz">
        <PatternLayout pattern="${layoutPattern}"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="200KB"/>
            <TimeBasedTriggeringPolicy interval="1"/>
        </Policies>
        <DefaultRolloverStrategy max="10" fileIndex="min"/>
    </RollingFile>
</Appenders>

<Loggers>
    <Root level="trace" additivity="false">
        <AppenderRef ref ="Console"/>
    </Root>
    <Logger name="org.springframework.web" level="error" additivity="false">
        <AppenderRef ref="Console" />
    </Logger>

<Logger name="com" level="DEBUG" additivity="false">
        <AppenderRef ref="Console" />
    </Logger>
    
    <logger name="log4jdbc.log4j2" level="error" additivity="false"> 
        <MarkerFilter marker="LOG4JDBC_NON_STATEMENT" onMatch="DENY" onMismatch="NEUTRAL"/>
        <appender-ref ref="Console" /> 
    </logger>
    
    <logger name="jdbc.sqlonly" level="off" additivity="false"> 
        <appender-ref ref="Console" />
    </logger>

    <Logger name="jdbc.sqltiming" level="info" additivity="false">
        <AppenderRef ref="Console"/>

    </Logger>
    <Logger name="jdbc.audit" level="off" /> 
    <Logger name="jdbc.resultset" level="off" /> 
    <Logger name="jdbc.resultsettable" level="info" /> 
    <Logger name="jdbc.connection" level="off" />   
</Loggers>

#log4jdbc.log4j2.properties

log4jdbc.drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver

log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelega
tor

log4jdbc.dump.sql.maxlinelength = 0

#数据源配置

<bean id="dataSourceSqlServer" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
    <property name="url" value="jdbc:log4jdbc:sqlserver://ebs-dev:1433;databaseName=test" />
    <property name="username" value="sa" />
    <property name="password" value="*******" />
</bean>
4

0 回答 0