-->

스프링

 

 

스프링부트 3.0  이하 

 

1.p6spy

		<dependency>
		    <groupId>com.github.gavlyukovskiy</groupId>
		    <artifactId>p6spy-spring-boot-starter</artifactId>
		    <version>1.8.0</version>
		</dependency>

 

 

2.

application.yml

 

  datasource:    
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mariadb://localhost:3306/test    
    username: test
    password: 1234


logging:
  level:
    p6spy: info
      

 

 

3, P6spyPrettySqlFormatter

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.hibernate.engine.jdbc.internal.FormatStyle;

import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

public class P6spyPrettySqlFormatter implements MessageFormattingStrategy {

	@Override
	public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared,
			String sql, String url) {
		sql = formatSql(category, sql);
		Date currentDate = new Date();

		SimpleDateFormat format1 = new SimpleDateFormat("yy.MM.dd HH:mm:ss");

		// return now + "|" + elapsed + "ms|" + category + "|connection " + connectionId
		// + "|" + P6Util.singleLine(prepared) + sql;
		return format1.format(currentDate) + " | " + "OperationTime : " + elapsed + "ms" + sql;
	}

	private String formatSql(String category,String sql) {
        if(sql ==null || sql.trim().equals("")) return sql;

        // Only format Statement, distinguish DDL And DML
        if (Category.STATEMENT.getName().equals(category)) {
            String tmpsql = sql.trim().toLowerCase(Locale.ROOT);
            if(tmpsql.startsWith("create") || tmpsql.startsWith("alter") || tmpsql.startsWith("comment")) {
                sql = FormatStyle.DDL.getFormatter().format(sql);
            }else {
                sql = FormatStyle.BASIC.getFormatter().format(sql);
            }
            sql = "|\nHeFormatSql(P6Spy sql,Hibernate format):"+ sql;
        }

        return sql;
    }
	
}

 

 

 

 

4.P6spyConfig

import javax.annotation.PostConstruct;

import org.springframework.context.annotation.Configuration;

import com.p6spy.engine.spy.P6SpyOptions;

@Configuration
public class P6spyConfig { 
	
    @PostConstruct
    public void setLogMessageFormat() {
        P6SpyOptions.getActiveInstance().setLogMessageFormat(P6spyPrettySqlFormatter.class.getName());
    }    
}

 

 

 

 

 

참고:

 

https://p6spy.readthedocs.io/en/latest/integration.html

https://github.com/HomoEfficio/dev-tips/blob/master/p6spy-%EC%84%A4%EC%A0%95.md

 

https://devkuka.tistory.com/303

 

https://macaronics.net/index.php/m01/spring/view/2055

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 1  라이트

댓글 ( 4)

댓글 남기기

작성