Besides its default logger
      com.mysql.cj.log.StandardLogger, which logs to
      stderr, Connector/J supports the SLF4J logging
      facade, allowing end users of applications using Connector/J to
      plug in logging frameworks of their own choices at deployment
      time. Popular logging frameworks such as
      java.util.logging, logback,
      and log4j are supported by SLF4J. Follow these
      requirements to use a logging framework with SLF4J and
      Connector/J:
    
- 
In the development environment: - Install on your system - slf4j-api-x.y.z.jar(available at https://www.slf4j.org/download.html) and add it to the Java classpath.
- In the code of your application, obtain an - SLF4JLoggeras a- Loginstantiated within a- MysqlConnection- Session, and then use the- Loginstance for your logging.
 
- 
On the deployment system: - Install on your system - slf4j-api-x.y.z.jarand add it to the Java classpath
- 
Install on your system the SLF4J binding for the logging framework of your choice and add it to your Java classpath. SLF4J bindings are available at, for example, https://www.slf4j.org/manual.html#swapping. NoteDo not put more than one SLF4J binding in you Java classpath. Switch from one logging framework to another by removing a binding and adding a new one to the classpath. 
- Install the logging framework of your choice on your system and add it to the Java classpath. 
- Configure the logging framework of your choice. This often consists of setting up appenders or handlers for log messages using a configuration file; see your logging framework's documentation for details. 
- When connecting the application to the MySQL Server, set the Connector/J connection property - loggerto- Slf4JLogger.
 
      The log category name used by Connector/J with SLF4J is
      MySQL. See the
      SLF4J user
      manual for more details about using SLF4J, including
      discussions on Maven dependency and bindings. Here is a sample
      code for using SLF4J with Connector/J:
    
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.log.Log;
public class JDBCDemo {
 
  public static void main(String[] args) {
 
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;
    Log logger = null;
 
  try {
     // Database parameters
     String url = "jdbc:mysql://myexample.com:3306/pets?logger=Slf4JLogger&explainSlowQueries=true";
     String user = "user";
     String password = "password";
     // create a connection to the database
     conn = DriverManager.getConnection(url, user, password);
     logger = ((JdbcConnection)conn).getSession().getLog();
  }
  catch (SQLException e) {
     System.err.println(e.getMessage());
     System.exit(1);
   }
  try {
     statement = conn.createStatement();
     resultSet = statement.executeQuery("SELECT * FROM pets.dogs");
     while(resultSet.next()){
       System.out.printf("%d\t%s\t%s\t %4$ty.%4$tm.%4$td \n",
       resultSet.getInt(1),
       resultSet.getString(2),
       resultSet.getString(3),
       resultSet.getDate(4));
     }
  } 
  catch(SQLException e) {
     logger.logWarn("Warning: Select failed!");
  } 
}
}
If you want to use, for example, Log4j 2.17.1 as your logging framework when running this program, put these JAR files in your Java classpath:
- slf4j-api-2.0.3.jar(SLF4J API module, available at, for example, https://central.sonatype.com/artifact/org.slf4j/slf4j-api/2.0.3/jar).
- log4j-api-2.17.1.jarand- log4j-core-2.17.1.jar(Log4J library, available at, for example, https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-api/2.17.1/jar and https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-core/2.17.1/jar).
- log4j-slf4j-impl-2.17.1.jar(SLF4J's binding for Log4J 2.17.1, available at, for example, https://central.sonatype.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl/2.17.1/jar).
Here is output of the program when the SELECT statement failed:
[2021-09-05 12:06:19,624] WARN     0[main] - WARN MySQL - Warning: Select failed!