AbstractBasicLogger.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.logger;

  21. import org.openspcoop2.utils.UtilsException;
  22. import org.openspcoop2.utils.date.DateManager;
  23. import org.openspcoop2.utils.id.UUIDUtilsGenerator;
  24. import org.openspcoop2.utils.logger.beans.Diagnostic;
  25. import org.openspcoop2.utils.logger.config.DiagnosticConfig;
  26. import org.openspcoop2.utils.logger.constants.LowSeverity;
  27. import org.openspcoop2.utils.logger.constants.Severity;

  28. /**
  29.  * AbstractBasicLogger
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public abstract class AbstractBasicLogger extends AbstractBaseDiagnosticManagerCore implements ILogger {

  36.     protected String idTransaction;
  37.        
  38.     public AbstractBasicLogger(DiagnosticConfig diagnosticConfig) throws UtilsException{
  39.         super(diagnosticConfig);
  40.     }

  41.     @Override
  42.     public void initLogger() throws UtilsException{
  43.        
  44.         try{
  45.            
  46.             this.idTransaction = UUIDUtilsGenerator.newUUID();
  47.            
  48.             super.init(this.getContext(), this);
  49.            
  50.         }catch(Exception e){
  51.             throw new UtilsException(e.getMessage(),e);
  52.         }
  53.     }
  54.    
  55.     @Override
  56.     public void initLogger(String idTransaction) throws UtilsException{
  57.         try{
  58.            
  59.             if(idTransaction!=null && !"".equals(idTransaction)){
  60.                 this.idTransaction = idTransaction;
  61.             }
  62.             else{
  63.                 this.idTransaction = UUIDUtilsGenerator.newUUID();
  64.             }
  65.            
  66.             super.init(this.getContext(), this);
  67.            
  68.         }catch(Exception e){
  69.             throw new UtilsException(e.getMessage(),e);
  70.         }
  71.     }
  72.    
  73.     @Override
  74.     public void log(String message, LowSeverity severity) throws UtilsException{
  75.         String functionDefault = this.diagnosticManager.getDefaultFunction();
  76.         this.log(functionDefault, message, severity.toSeverity(), this.diagnosticManager.getDefaultCode(functionDefault, severity));
  77.     }
  78.    
  79.     @Override
  80.     public void log(String message, LowSeverity severity, String function) throws UtilsException{
  81.         this.log(function, message, severity.toSeverity(), this.diagnosticManager.getDefaultCode(function, severity));
  82.     }

  83.     @Override
  84.     public void log(String code) throws UtilsException{
  85.         this.log(this.diagnosticManager.getFunction(code),
  86.                 this.diagnosticManager.getMessage(code),
  87.                 this.diagnosticManager.getSeverity(code),
  88.                 this.diagnosticManager.getCode(code));
  89.     }

  90.     @Override
  91.     public void log(String code, String... params) throws UtilsException{
  92.         this.log(this.diagnosticManager.getFunction(code),
  93.                 this.diagnosticManager.getMessage(code, params),
  94.                 this.diagnosticManager.getSeverity(code),
  95.                 this.diagnosticManager.getCode(code));
  96.     }
  97.    
  98.     // serve per evitare che la chiamata con string ricata erroneamente nella firma Object invece che nella firma String ... params
  99.     @Override
  100.     public void log(String code, String param) throws UtilsException{
  101.         this.log(code,new String [] {param});
  102.     }
  103.    
  104.     @Override
  105.     public void log(String code, Object o) throws UtilsException{
  106.         this.log(this.diagnosticManager.getFunction(code),
  107.                 this.diagnosticManager.getMessage(code,o),
  108.                 this.diagnosticManager.getSeverity(code),
  109.                 this.diagnosticManager.getCode(code));
  110.     }
  111.    
  112.     private void log(String function,String message, Severity severity, String code) throws UtilsException{
  113.        
  114.         Diagnostic diagnostic = new Diagnostic();
  115.         diagnostic.setDate(DateManager.getDate());
  116.         diagnostic.setCode(code);
  117.         diagnostic.setFunction(function);
  118.         diagnostic.setIdTransaction(this.idTransaction);
  119.         diagnostic.setMessage(message);
  120.         diagnostic.setSeverity(severity);
  121.         this.log(diagnostic, this.getContext());
  122.        
  123.     }
  124.    
  125.     protected abstract void log(Diagnostic diagnostic,IContext context) throws UtilsException;
  126.    
  127.    
  128.    
  129. }