JDBC Logs from Database Statements

SAP Commerce sends database statements to query and modify data in the database. You can create a record of these statements. The process of recording the statements is called JDBC logging.

SAP Commerce uses JDBC to connect to databases. As a JDBC log contains information on statements, execution times, and other information, you can use it to identify and remedy performance bottlenecks on the database.

You can decide how you want to store JDBC logs by choosing one of the following implementations of the logger class:

  • FileLogger

  • JDBCSLF4JAwareLogger

Understanding a JDBC Log File (FileLogger)

In a JDBC log file, each line is an individual logged statement. Each line consists of a number of bits of information, delimited by the pipe character ( | ). This example full statement is broken down into multiple lines so that it’s easier to read:

34|master|091106-15:01:04:136|1 ms|statement|SELECT  item_t0.PK  FROM tasks item_t0 WHERE item_t0.p_failed  = ?|
SELECT  item_t0.PK  FROM tasks item_t0 WHERE item_t0.p_failed  =0 |
PreparedStatementImpl:57:DataSourceImplFactory:108:ConnectionImpl:338:ConnectionImpl:627:FlexibleSearchExecutor:115:
FlexibleSearch:1854:FlexibleSearch:1515:FlexibleSearch:1366:FlexibleSearch:1357:DefaultFlexibleSearchService$2:468:DefaultFlexibleSearchService$2:1:
DefaultSessionService:89:DefaultFlexibleSearchService:446:DefaultFlexibleSearchService:179:QueryBasedTasksProvider:224:QueryBasedTasksProvider:125:
QueryBasedTasksProvider:102:ShufflingTasksProvider:31:BufferedTasksProvider:51:DefaultTasksProvider:45:DefaultTaskService:1170:
DefaultTaskService:1142:DefaultTaskService$Poll:1018:DefaultTaskService$Poll:969:AbstractTenant$5:2484:Thread:834:RegistrableThread:141:RegistrableThread:131:END

Each line of the JDBC log file consists of the following fields (from left to right):

Field Name

Field Description

Value in Sample Line

Thread ID

The ID of the thread that executed the statement.

34

Datasource ID

The ID of the used datasource.

master

Start Time

Date and time when the statement was started.

091106-15:01:04:136

Elapsed Time

Amount of time that the execution of the statement took.

1 ms

Category

The category of the statement. Possible values: statement, commit, rollback.

statement

Prepared statement

Each statement sent to the database has to be compiled. When executing numerous similar statements (for example, when the only difference is in parameters) prepared statements are used for better performance. The prepared statement is precompiled without parameters and then can be reused many times.

SELECT  item_t0.PK
   FROM tasks item_t0
   WHERE item_t0.p_failed  = ?

SQL statement

The actual SQL statement that was executed on the database. Disabled by default. Set the db.log.sql.parameters=true property to enable it.

SELECT  item_t0.PK
   FROM tasks item_t0
   WHERE item_t0.p_failed  = 0

Stack trace

The actual stack trace. Disabled by default. Set the db.log.appendStackTrace=true property to enable it.

PreparedStatementImpl:57:DataSourceImplFactory:108:ConnectionImpl:338:ConnectionImpl:627:FlexibleSearchExecutor:115:
FlexibleSearch:1854:FlexibleSearch:1515:FlexibleSearch:1366:FlexibleSearch:1357:DefaultFlexibleSearchService$2:468:DefaultFlexibleSearchService$2:1:
DefaultSessionService:89:DefaultFlexibleSearchService:446:DefaultFlexibleSearchService:179:QueryBasedTasksProvider:224:QueryBasedTasksProvider:125:
QueryBasedTasksProvider:102:ShufflingTasksProvider:31:BufferedTasksProvider:51:DefaultTasksProvider:45:DefaultTaskService:1170:
DefaultTaskService:1142:DefaultTaskService$Poll:1018:DefaultTaskService$Poll:969:AbstractTenant$5:2484:Thread:834:RegistrableThread:141:RegistrableThread:131:END

JDBC SLF4J Logger (JDBCSLF4JAwareLogger)

JDBCSLF4JAwareLogger acts as a wrapper and delegates all logging to SLF4J, which in turn uses Log4j 2 as an implementation. That allows you to customize log layout and define where you want to store logs. There are two appenders configured for this logger: file, and console. In contrast to the JDBC Log File, some of the parameters are passed in context (by MDC - Mapped Diagnostic Context).

Here are the names of the parameters passed in context:

Field name

Parameter name

Datasource ID

jdbc-dataSourceId

Elapsed Time

jdbc-elapsedTime

Category

jdbc-category

Start Time

jdbc-startTime

The information stored in the MDC can be exposed in the set layout, for example, in PatternLayout using the %X{parameterName} placeholder (see https://logging.apache.org/log4j/2.x/manual/layouts.htmlInformation published on non-SAP site):

log4j2.appender.console.layout.pattern = %-5p [%t] %X{jdbc-dataSourceId} %c{1}] %m%n

To enable or disable JDBC through SLF4J logging, use the db.log.active property. All messages are logged with the debug level. The debug level is enabled for JDBCSLF4JAwareLogger.

Here is the current log4j2 configuration for JDBCSLF4JAwareLogger:

log4j2.appender.jdbcConsoleLogger.type = Console
log4j2.appender.jdbcConsoleLogger.name=JdbcConsoleLogger
log4j2.appender.jdbcConsoleLogger.layout.type=PatternLayout
log4j2.appender.jdbcConsoleLogger.layout.pattern=%-5p [%t] %d{HH:mm:ss.SSS} [%X{jdbc-dataSourceId}|%X{jdbc-category}|%X{jdbc-timeElapsed} ms] %X{RemoteAddr} %X{Tenant} %m%n
 
log4j2.appender.jdbcFileLogger.type = File
log4j2.appender.jdbcFileLogger.fileName=${HYBRIS_LOG_DIR}/jdbc.log
log4j2.appender.jdbcFileLogger.name=JdbcFileLogger
log4j2.appender.jdbcFileLogger.layout.type=PatternLayout
log4j2.appender.jdbcFileLogger.layout.pattern=%tid|%X{jdbc-dataSourceId}|%X{jdbc-startTime}|%X{jdbc-timeElapsed} ms|%X{jdbc-category}|%m%n
 
log4j2.logger.jdbc-sl4j.name = de.hybris.platform.jdbcwrapper.logger.JDBCSLF4JAwareLogger
log4j2.logger.jdbc-sl4j.level = debug
log4j2.logger.jdbc-sl4j.additivity = false
log4j2.logger.jdbc-sl4j.appenderRef.jdbcFileLogger.ref = JdbcFileLogger
log4j2.logger.jdbc-sl4j.appenderRef.jdbcConsoleLogger.ref = JdbcConsoleLogger

The configured JdbcFileLogger appender must be enabled and must have a defined layout pattern in order to use the JDBC Log Analysis function from SAP Commerce Administration Console:

%tid|%X{jdbc-dataSourceId}|%X{jdbc-startTime}|%X{jdbc-timeElapsed} ms|%X{jdbc-category}|%m%n

JDBC SLF4J Logger Tip

With db.log.active enabled with the default configured appenders, the log is stored on console and in the jdbc.log file. You can disable logging to these appenders and define your own appender with, for example, the JSON format:

log4j2.logger.jdbc-sl4j.appenderRef.jdbcFileLogger.level=OFF
log4j2.logger.jdbc-sl4j.appenderRef.jdbcConsoleLogger.level=OFF
 
 
log4j2.appender.jdbcFileJsonLogger.type = File
log4j2.appender.jdbcFileJsonLogger.fileName=${HYBRIS_LOG_DIR}/jdbc.log
log4j2.appender.jdbcFileJsonLogger.name=JdbcFileJsonLogger
log4j2.appender.jdbcFileJsonLogger.layout.type=JsonLayout
log4j2.appender.jdbcFileJsonLogger.layout.properties=true
 
log4j2.logger.jdbc-sl4j.appenderRef.jdbcFileJsonLogger.ref = JdbcFileJsonLogger

Enabling and Disabling JDBC Logging

You can enable or disable JDBC logging by using SAP Commerce Administration Console, Java code, or through the configuration properties file.

Enabling or disabling JDBC logging in SAP Commerce Administration Console has a run-time effect only - logging is set back to default after an SAP Commerce restart.

Enabling or disabling JDBC logging by using Java code has a runtime effect only - logging is set back to default after an SAP Commerce restart.

Enabling or disabling JDBC logging through the configuration properties file has a persistent effect - logging is enabled or disabled after an SAP Commerce restart.

Enabling JDBC Logging in SAP Commerce Administration Console

In SAP Commerce Administration Console, you can enable or disable JDBC logging on the Start of the navigation pathMonitoring Next navigation step Database Next navigation step JDBC loggingEnd of the navigation path page.

Enabling JDBC Logging Using Java Code

To enable JDBC logging in your Java code, add the following line:

de.hybris.platform.util.Config.setParameter("db.log.active", "true");

To disable JDBC logging in your Java code, add the following line:

de.hybris.platform.util.Config.setParameter("db.log.active", "false");

See an example of how to enable and disable JDBC logging:

private void myMethod()
   {
         de.hybris.platform.util.Config.setParameter("db.log.active", "true");
         myProductModel.setCode("ModelCode");
         modelService.save(myProductModel);
         de.hybris.platform.util.Config.setParameter("db.log.active", "false");
   }

Enabling JDBC Logging Through a Properties File

To ensure that the logging setting remains enabled or disabled persistently, set an appropriate property value in the local.properties file.

To enable JDBC logging, use db.log.active=true.

To disable JDBC logging, use db.log.active=false.

Specify a fully qualified class name of the logger class to the db.log.loggerclass config property. You can choose one of the following implementations:

  • de.hybris.platform.jdbcwrapper.logger.FileLogger

  • de.hybris.platform.jdbcwrapper.logger.JDBCSLF4JAwareLogger

Here is an example:

db.log.loggerclass=de.hybris.platform.jdbcwrapper.logger.FileLogger

For details about the local.properties file, see Configuring the Behavior of SAP Commerce.

Configuration Properties

Use properties to customize JDBC logging:

Property

Possible Value

Description

db.log.sql.parameters

true / false

When enabled, it logs a full sql statement with actual parameters. It’s disabled in the default configuration to prevent leaking of confidential data, such as passwords, into logs.