ServiceLogger.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.utils.service.logger;

  21. import org.openspcoop2.utils.service.context.MD5Constants;
  22. import org.slf4j.Logger;
  23. import org.slf4j.MDC;

  24. /**
  25.  * ServiceLogger
  26.  *
  27.  * @author Andrea Poli (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class ServiceLogger {

  32.     private String idOperazione;
  33.     private String methodName;
  34.     private String prefix;
  35.     private Logger log;

  36.    
  37.     public ServiceLogger(String idOperazione, String methodName, String className, Logger log, boolean usePrefix) {
  38.         this.log = log;
  39.         this.idOperazione = idOperazione;
  40.         this.methodName = methodName;
  41.         String classe = className;
  42.         if(className.contains(".")) {
  43.             String [] tmp = className.split("\\.");
  44.             classe = tmp[tmp.length-1];
  45.         }
  46.         if(usePrefix) {
  47.             this.prefix = String.format("<%s> (%s.%s) ", this.idOperazione,classe,this.methodName);
  48.         }
  49.         else {
  50.             this.prefix = "";
  51.         }
  52.         MDC.put(MD5Constants.SERVICE_ID, classe);
  53.         MDC.put(MD5Constants.OPERATION_ID, this.methodName);
  54.     }
  55.    
  56.     private String appendPrefix(String message) {
  57.         StringBuilder bf = new StringBuilder(this.prefix);
  58.         bf.append(message);
  59.         return bf.toString();
  60.     }
  61.    
  62.     public void error(String message, Object ...params) {
  63.         this._error(message, null, params);
  64.     }
  65.     public void error(String message, Throwable t) {
  66.         this._error(message, t, null);
  67.     }
  68.     public void error(String message, Throwable t, Object ...params) {
  69.         this._error(message, t, params);
  70.     }
  71.     private void _error(String message, Throwable t, Object [] params) {
  72.         String msgWithPrefix = this.appendPrefix(message);
  73.         if(params!=null && params.length>0) {
  74.             String msg = String.format(msgWithPrefix,params);
  75.             if(t!=null) {
  76.                 this.log.error(msg,t);
  77.             }
  78.             else {
  79.                 this.log.error(msg);
  80.             }
  81.         }
  82.         else {
  83.             if(t!=null) {
  84.                 this.log.error(msgWithPrefix,t);
  85.             }
  86.             else {
  87.                 this.log.error(msgWithPrefix);
  88.             }
  89.         }
  90.     }
  91.    
  92.     public void error_except404(String message, Throwable t) {
  93.         this._error_except404(message, t, null);
  94.     }
  95.     public void error_except404(String message, Throwable t, Object ...params) {
  96.         this._error_except404(message, t, params);
  97.     }
  98.     private void _error_except404(String message, Throwable t, Object [] params) {
  99.        
  100.         boolean error = true;
  101.         if(t!=null && t instanceof javax.ws.rs.WebApplicationException) {
  102.             javax.ws.rs.WebApplicationException we = (javax.ws.rs.WebApplicationException) t;
  103.             if(we.getResponse()!=null && we.getResponse().getStatus()==404) {
  104.                 error = false;
  105.             }
  106.         }
  107.        
  108.         String msgWithPrefix = this.appendPrefix(message);
  109.         if(params!=null && params.length>0) {
  110.             String msg = String.format(msgWithPrefix,params);
  111.             if(t!=null) {
  112.                 if(error)
  113.                     this.log.error(msg,t); else this.log.debug(msg,t);
  114.             }
  115.             else {
  116.                 // e' per forza un errore
  117.                 //if(error)
  118.                 this.log.error(msg);
  119.                 //else this.log.debug(msg);
  120.             }
  121.         }
  122.         else {
  123.             if(t!=null) {
  124.                 if(error)
  125.                     this.log.error(msgWithPrefix,t); else this.log.debug(msgWithPrefix,t);
  126.             }
  127.             else {
  128.                 // e' per forza un errore
  129.                 //if(error)
  130.                 this.log.error(msgWithPrefix);
  131.                 //else this.log.debug(msgWithPrefix,t);
  132.             }
  133.         }
  134.     }
  135.    
  136.    
  137.     public void warn(String message, Object ...params) {
  138.         this._warn(message, null, params);
  139.     }
  140.     public void warn(String message, Throwable t) {
  141.         this._warn(message, t, null);
  142.     }
  143.     public void warn(String message, Throwable t, Object ...params) {
  144.         this._warn(message, t, params);
  145.     }
  146.     private void _warn(String message, Throwable t, Object [] params) {
  147.         String msgWithPrefix = this.appendPrefix(message);
  148.         if(params!=null && params.length>0) {
  149.             String msg = String.format(msgWithPrefix,params);
  150.             if(t!=null) {
  151.                 this.log.warn(msg,t);
  152.             }
  153.             else {
  154.                 this.log.warn(msg);
  155.             }
  156.         }
  157.         else {
  158.             if(t!=null) {
  159.                 this.log.warn(msgWithPrefix,t);
  160.             }
  161.             else {
  162.                 this.log.warn(msgWithPrefix);
  163.             }
  164.         }
  165.     }
  166.    
  167.    
  168.     public void info(String message, Object ...params) {
  169.         this._info(message, null, params);
  170.     }
  171.     public void info(String message, Throwable t) {
  172.         this._info(message, t, null);
  173.     }
  174.     public void info(String message, Throwable t, Object ...params) {
  175.         this._info(message, t, params);
  176.     }
  177.     private void _info(String message, Throwable t, Object [] params) {
  178.         String msgWithPrefix = this.appendPrefix(message);
  179.         if(params!=null && params.length>0) {
  180.             String msg = String.format(msgWithPrefix,params);
  181.             if(t!=null) {
  182.                 this.log.info(msg,t);
  183.             }
  184.             else {
  185.                 this.log.info(msg);
  186.             }
  187.         }
  188.         else {
  189.             if(t!=null) {
  190.                 this.log.info(msgWithPrefix,t);
  191.             }
  192.             else {
  193.                 this.log.info(msgWithPrefix);
  194.             }
  195.         }
  196.     }
  197.    

  198.     public void debug(String message, Object ...params) {
  199.         this._debug(message, null, params);
  200.     }
  201.     public void debug(String message, Throwable t) {
  202.         this._debug(message, t, null);
  203.     }
  204.     public void debug(String message, Throwable t, Object ...params) {
  205.         this._debug(message, t, params);
  206.     }
  207.     private void _debug(String message, Throwable t, Object [] params) {
  208.         String msgWithPrefix = this.appendPrefix(message);
  209.         if(params!=null && params.length>0) {
  210.             String msg = String.format(msgWithPrefix,params);
  211.             if(t!=null) {
  212.                 this.log.debug(msg,t);
  213.             }
  214.             else {
  215.                 this.log.debug(msg);
  216.             }
  217.         }
  218.         else {
  219.             if(t!=null) {
  220.                 this.log.debug(msgWithPrefix,t);
  221.             }
  222.             else {
  223.                 this.log.debug(msgWithPrefix);
  224.             }
  225.         }
  226.     }
  227.    

  228.     public void trace(String message, Object ...params) {
  229.         this._trace(message, null, params);
  230.     }
  231.     public void trace(String message, Throwable t) {
  232.         this._trace(message, t, null);
  233.     }
  234.     public void trace(String message, Throwable t, Object ...params) {
  235.         this._trace(message, t, params);
  236.     }
  237.     private void _trace(String message, Throwable t, Object [] params) {
  238.         String msgWithPrefix = this.appendPrefix(message);
  239.         if(params!=null && params.length>0) {
  240.             String msg = String.format(msgWithPrefix,params);
  241.             if(t!=null) {
  242.                 this.log.trace(msg,t);
  243.             }
  244.             else {
  245.                 this.log.trace(msg);
  246.             }
  247.         }
  248.         else {
  249.             if(t!=null) {
  250.                 this.log.trace(msgWithPrefix,t);
  251.             }
  252.             else {
  253.                 this.log.trace(msgWithPrefix);
  254.             }
  255.         }
  256.     }
  257. }