JDBCSqlLogger.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.generic_project.dao.jdbc.utils;


  21. import java.net.URI;
  22. import java.sql.Timestamp;
  23. import java.text.SimpleDateFormat;
  24. import java.util.Calendar;
  25. import java.util.Date;

  26. import org.openspcoop2.generic_project.beans.IEnumeration;
  27. import org.openspcoop2.utils.Utilities;
  28. import org.openspcoop2.utils.date.DateUtils;

  29. /**
  30.  * JDBCSqlLogger
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class JDBCSqlLogger {

  37.     private org.slf4j.Logger log = null;
  38.     private SimpleDateFormat dateformat = DateUtils.getDefaultDateTimeFormatter("yyyy-MM-dd HH:mm:ss.SSS");

  39.     /**
  40.      * Costruttore della classe di utilita' per gestire i log sia di debug che di errore delle query effettuate su database
  41.      *
  42.      * @param log logger
  43.      */
  44.     public JDBCSqlLogger(org.slf4j.Logger log){
  45.         this.log = log;
  46.     }

  47.     /**
  48.      * Messaggio di debug
  49.      *
  50.      * @param msg messaggio di debug
  51.      */
  52.     public void debug( String msg ) {
  53.         this.log.debug(msg);
  54.     }

  55.     /**
  56.      * Registrazione del comando sql eseguita con livello DEBUG
  57.      *
  58.      * @param sql comando sql
  59.      */
  60.     public void debugSql( String sql ) {
  61.         if (this.log.isDebugEnabled()) {
  62.             this.log.debug( sqlLog( sql ));
  63.         }
  64.     }

  65.     /**
  66.      * Registrazione del comando sql eseguita con livello DEBUG
  67.      *
  68.      * @param sql comando sql
  69.      * @param param parametro richiesto dal comando sql
  70.      */
  71.     public void debugSql( String sql, JDBCObject ... param ) {
  72.         if (this.log.isDebugEnabled()) {
  73.             this.log.debug( sqlLog( sql , param ) );
  74.         }
  75.     }
  76.    
  77.    
  78.     /**
  79.      * Messaggio di info
  80.      *
  81.      * @param msg messaggio di info
  82.      */
  83.     public void info( String msg ) {
  84.         this.log.info(msg);
  85.     }

  86.     /**
  87.      * Registrazione del comando sql eseguita con livello INFO
  88.      *
  89.      * @param sql comando sql
  90.      */
  91.     public void infoSql( String sql ) {
  92.         if (this.log.isInfoEnabled()) {
  93.             this.log.info( sqlLog( sql ));
  94.         }
  95.     }

  96.     /**
  97.      * Registrazione del comando sql eseguita con livello INFO
  98.      *
  99.      * @param sql comando sql
  100.      * @param param parametro richiesto dal comando sql
  101.      */
  102.     public void infoSql( String sql, JDBCObject ... param ) {
  103.         if (this.log.isInfoEnabled()) {
  104.             this.log.info( sqlLog( sql , param ) );
  105.         }
  106.     }

  107.     /**
  108.      * Messaggio di errore
  109.      *
  110.      * @param msg messaggio di errore
  111.      */
  112.     public void error( String msg ) {
  113.         this.log.error(msg );
  114.     }

  115.     /**
  116.      * Messaggio di errore con corrispettiva eccezione
  117.      *
  118.      * @param msg messaggio di errore
  119.      * @param t eccezione
  120.      */
  121.     public void error( String msg, Throwable t ) {
  122.         this.log.error(msg, t );
  123.     }

  124.     /**
  125.      * Registrazione del comando sql eseguita con livello ERROR
  126.      *
  127.      * @param t eccezione
  128.      * @param sql comando sql
  129.      */
  130.     public void errorSql( Throwable t, String sql ) {
  131.         this.log.error( sqlLog( sql ), t );
  132.     }

  133.     /**
  134.      * Registrazione del comando sql eseguita con livello ERROR
  135.      *
  136.      * @param t eccezione
  137.      * @param sql comando sql
  138.      * @param param parametro richiesto dal comando
  139.      */
  140.     public void errorSql( Throwable t, String sql, JDBCObject ... param ) {
  141.         this.log.error( sqlLog( sql, param ), t );
  142.     }



  143.     /**
  144.      * Trasformazione in string 'human readable' del comando sql con i corrispettivi parametri
  145.      *
  146.      * @param sql comando sql
  147.      * @param params parametri richiesti dal comando
  148.      * @return SQL
  149.      */
  150.     public String sqlLog( String sql, JDBCObject ... params) {
  151.         String ret = "SQL: " + sql;

  152.         if (params == null || params.length == 0) {
  153.             return ret;
  154.         }

  155.         StringBuilder sb = new StringBuilder();

  156.         for (int i = 0; i < params.length; i++) {
  157.             if (params[i] != null) {
  158.                 if (sb.length() != 0) {
  159.                     sb.append( ",\n" );
  160.                 }
  161.                                
  162.                 Object object = params[i].getObject();
  163.                 Class<?> type = params[i].getTypeObject();
  164.                 sb.append( "\t- param("+(i+1)+") type("+type+") value(" );
  165.                
  166.                 logParam(object, type, sb);
  167.                
  168.                 sb.append(")");
  169.             }
  170.         }

  171.         return  ret + "; PARAMS: \n" + sb.toString();

  172.     }

  173.     private void logParam(Object object,Class<?> type,StringBuilder sb){

  174.         String typeName = object!=null ? type.getName()+"" : "";
  175.        
  176.         if (object == null){
  177.             sb.append("null");
  178.         }

  179.         else if(typeName.equals(String.class.getName())){
  180.             sb.append('\'').append( object ).append( '\'' );
  181.         }

  182.         else if(typeName.equals(Character.class.getName())){
  183.             Character valueWrapped = (Character) object;
  184.             char charPrimitiveValue = valueWrapped.charValue();
  185.             String charValue = valueWrapped.charValue()+"";
  186.             if(charPrimitiveValue==0){ // == ''
  187.                 // ERROR: invalid byte sequence for encoding "UTF8": 0x00
  188.                 // Postgresql non supporta il carattere 'vuoto'. Si deve usare un null value
  189.                 charValue = null;
  190.             }
  191.             if(charValue!=null)
  192.                 sb.append('\'').append( charValue ).append( '\'' );
  193.             else
  194.                 sb.append("null");
  195.         }
  196.         else if(typeName.equals(char.class.getName())){
  197.             Character valueWrapped = (Character) object;
  198.             char charPrimitiveValue = valueWrapped.charValue();
  199.             String charValue = valueWrapped.charValue()+"";
  200.             if(charPrimitiveValue==0){ // == ''
  201.                 // ERROR: invalid byte sequence for encoding "UTF8": 0x00
  202.                 // Postgresql non supporta il carattere 'vuoto'. Si deve usare un null value
  203.                 charValue = null;
  204.             }
  205.             if(charValue!=null)
  206.                 sb.append('\'').append( charValue ).append( '\'' );
  207.             else
  208.                 sb.append("null");
  209.         }
  210.        
  211.         else if(typeName.equals(Boolean.class.getName())){
  212.             sb.append( object.toString() );
  213.         }
  214.         else if(typeName.equals(boolean.class.getName())){
  215.             sb.append( object );
  216.         }
  217.        
  218.         else if(typeName.equals(Byte.class.getName())){
  219.             sb.append( object.toString() );
  220.         }
  221.         else if(typeName.equals(byte.class.getName())){
  222.             sb.append( object );
  223.         }
  224.        
  225.         else if(typeName.equals(Short.class.getName())){
  226.             sb.append( object.toString() );
  227.         }
  228.         else if(typeName.equals(short.class.getName())){
  229.             sb.append( object );
  230.         }
  231.        
  232.         else if(typeName.equals(Integer.class.getName())){
  233.             sb.append( object.toString() );
  234.         }
  235.         else if(typeName.equals(int.class.getName())){
  236.             sb.append( object );
  237.         }
  238.                        
  239.         else if(typeName.equals(Long.class.getName())){
  240.             sb.append( object.toString() );
  241.         }
  242.         else if(typeName.equals(long.class.getName())){
  243.             sb.append( object );
  244.         }
  245.        
  246.         else if(typeName.equals(Double.class.getName())){
  247.             sb.append( object.toString() );
  248.         }
  249.         else if(typeName.equals(double.class.getName())){
  250.             sb.append( object );
  251.         }
  252.        
  253.         else if(typeName.equals(Float.class.getName())){
  254.             sb.append( object.toString() );
  255.         }
  256.         else if(typeName.equals(float.class.getName())){
  257.             sb.append( object );
  258.         }
  259.        
  260.         else if(typeName.equals(Date.class.getName())){
  261.             sb.append('\'').append( this.dateformat.format((Date)object) ).append( '\'' );
  262.         }
  263.         else if(typeName.equals(java.sql.Date.class.getName())){
  264.             sb.append('\'').append( this.dateformat.format((java.sql.Date)object) ).append( '\'' );
  265.         }
  266.         else if(typeName.equals(Timestamp.class.getName())){
  267.             sb.append('\'').append( this.dateformat.format( new Date( ((Timestamp)object).getTime() ) )).append( '\'' );
  268.         }
  269.         else if(typeName.equals(Calendar.class.getName())){
  270.             sb.append('\'').append( this.dateformat.format( new Date( ((Calendar)object).getTime().getTime() ) )).append( '\'' );
  271.         }
  272.        
  273.         else if(typeName.equals(byte[].class.getName())){
  274.              // 1024 = 1K
  275.              // Visualizzo al massimo 5K
  276.              int max = 5 * 1024;
  277.              try{
  278.                  sb.append('\'').append(Utilities.convertToPrintableText((byte[])object, max)).append('\'');
  279.              }catch(Exception e){
  280.                  sb.append( "bytes[] not printable ("+e.getMessage()+")" );
  281.              }
  282.         }
  283.        
  284.         else if(typeName.equals(URI.class.getName())){
  285.             sb.append('\'').append( object ).append( '\'' );
  286.         }
  287.        
  288.         else if(object!=null && object instanceof IEnumeration){
  289.             IEnumeration enumObject = (IEnumeration) object;
  290.             Object value = enumObject.getValue();
  291.             Class<?> cValue = null;
  292.             if(value!=null){
  293.                 cValue = value.getClass();
  294.             }
  295.             sb.append("IEnumeration ");
  296.             this.logParam(value, cValue, sb);
  297.         }
  298.        
  299.         else {
  300.             sb.append( "!!ERROR Print not supported for this type" );
  301.         }
  302.     }
  303. }