/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.ds.single.druid.filter;

import com.alibaba.druid.filter.logging.LogFilter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 田平 create 2021/5/21 10:22
 */
@Slf4j
public class ConnLogFilter extends LogFilter {
	private Logger dataSourceLogger = LoggerFactory.getLogger(ConnLogFilter.class);
	private Logger connectionLogger = LoggerFactory.getLogger(ConnLogFilter.class);
	private Logger statementLogger = LoggerFactory.getLogger(ConnLogFilter.class);
	private Logger resultSetLogger = LoggerFactory.getLogger(ConnLogFilter.class);

	@Override
	public String getDataSourceLoggerName() {
		return dataSourceLoggerName;
	}

	@Override
	public void setDataSourceLoggerName(String dataSourceLoggerName) {
		this.dataSourceLoggerName = dataSourceLoggerName;
		dataSourceLogger = LoggerFactory.getLogger(dataSourceLoggerName);
	}

	public void setDataSourceLogger(Logger dataSourceLogger) {
		this.dataSourceLogger = dataSourceLogger;
		this.dataSourceLoggerName = dataSourceLogger.getName();
	}

	@Override
	public String getConnectionLoggerName() {
		return connectionLoggerName;
	}

	@Override
	public void setConnectionLoggerName(String connectionLoggerName) {
		this.connectionLoggerName = connectionLoggerName;
		connectionLogger = LoggerFactory.getLogger(connectionLoggerName);
	}

	public void setConnectionLogger(Logger connectionLogger) {
		this.connectionLogger = connectionLogger;
		this.connectionLoggerName = connectionLogger.getName();
	}

	@Override
	public String getStatementLoggerName() {
		return statementLoggerName;
	}

	@Override
	public void setStatementLoggerName(String statementLoggerName) {
		this.statementLoggerName = statementLoggerName;
		statementLogger = LoggerFactory.getLogger(statementLoggerName);
	}

	public void setStatementLogger(Logger statementLogger) {
		this.statementLogger = statementLogger;
		this.statementLoggerName = statementLogger.getName();
	}

	@Override
	public String getResultSetLoggerName() {
		return resultSetLoggerName;
	}

	@Override
	public void setResultSetLoggerName(String resultSetLoggerName) {
		this.resultSetLoggerName = resultSetLoggerName;
		resultSetLogger = LoggerFactory.getLogger(resultSetLoggerName);
	}

	public void setResultSetLogger(Logger resultSetLogger) {
		this.resultSetLogger = resultSetLogger;
		this.resultSetLoggerName = resultSetLogger.getName();
	}

	@Override
	public boolean isConnectionLogErrorEnabled() {
		return connectionLogger.isErrorEnabled() && super.isConnectionLogErrorEnabled();
	}

	@Override
	public boolean isDataSourceLogEnabled() {
		return dataSourceLogger.isDebugEnabled() && super.isDataSourceLogEnabled();
	}

	@Override
	public boolean isConnectionLogEnabled() {
		return connectionLogger.isDebugEnabled() && super.isConnectionLogEnabled();
	}

	@Override
	public boolean isStatementLogEnabled() {
		return statementLogger.isDebugEnabled() && super.isStatementLogEnabled();
	}

	@Override
	public boolean isResultSetLogEnabled() {
		return resultSetLogger.isDebugEnabled() && super.isResultSetLogEnabled();
	}

	@Override
	public boolean isResultSetLogErrorEnabled() {
		return resultSetLogger.isErrorEnabled() && super.isResultSetLogErrorEnabled();
	}

	@Override
	public boolean isStatementLogErrorEnabled() {
		return statementLogger.isErrorEnabled() && super.isStatementLogErrorEnabled();
	}

	@Override
	protected void connectionLog(String message) {
		connectionLogger.info(message);
	}

	@Override
	protected void statementLog(String message) {
		statementLogger.debug(message);
	}

	@Override
	protected void resultSetLog(String message) {
		resultSetLogger.debug(message);
	}

	@Override
	protected void resultSetLogError(String message, Throwable error) {
		resultSetLogger.error(message, error);
	}

	@Override
	protected void statementLogError(String message, Throwable error) {
		statementLogger.error(message, error);
	}

}
