DriverManager初始化的日志怎么打印?

现状

  • 加载DriverManager的时候,logWriter==null
  • 首先执行静态代码块
  • 静态代码块会打印日志
    #

问题

  • 如何在类加载前设置logWriter?????

方案一

  • 以下代码放在第一行
DriverManager.setLogWriter(new PrintWriter(System.out));
  • 后续的日志能打印,初始化日志还是不能打印

方案二

  • 写个Driver
package com.zby.driver;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

public class LogDriver implements Driver {

	static {
		DriverManager.setLogWriter(new PrintWriter(System.out));
	}

	@Override
	public Connection connect(String url, Properties info) throws SQLException {
		return null;
	}

	@Override
	public boolean acceptsURL(String url) throws SQLException {
		return false;
	}

	@Override
	public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
		return null;
	}

	@Override
	public int getMajorVersion() {
		return 0;
	}

	@Override
	public int getMinorVersion() {
		return 0;
	}

	@Override
	public boolean jdbcCompliant() {
		return false;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return null;
	}

}

  • 创建META-INF/services/java.sql.Driver文件,写入内容com.zby.driver.LogDriver
posted @ 2020-12-22 10:49  java拌饭  阅读(403)  评论(0编辑  收藏  举报