LoggerBuffer.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;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.PrintStream;

  23. import org.slf4j.Logger;

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

  32.     private StringBuilder sbError;
  33.     private StringBuilder sbDebug;
  34.     private boolean logErrorInDebug = true;
  35.     private boolean addSeverityPrefix = false;
  36.     private Logger logDebug;
  37.     private Logger logError;
  38.    
  39.     public Logger getLogDebug() {
  40.         return this.logDebug;
  41.     }
  42.     public void setLogDebug(Logger logDebug) {
  43.         this.logDebug = logDebug;
  44.     }
  45.     public Logger getLogError() {
  46.         return this.logError;
  47.     }
  48.     public void setLogError(Logger logError) {
  49.         this.logError = logError;
  50.     }
  51.     public StringBuilder getSbError() {
  52.         return this.sbError;
  53.     }
  54.     public void setSbError(StringBuilder sbError) {
  55.         this.sbError = sbError;
  56.     }
  57.     public StringBuilder getSbDebug() {
  58.         return this.sbDebug;
  59.     }
  60.     public void setSbDebug(StringBuilder sbDebug) {
  61.         this.sbDebug = sbDebug;
  62.     }
  63.     public boolean isLogErrorInDebug() {
  64.         return this.logErrorInDebug;
  65.     }
  66.     public void setLogErrorInDebug(boolean logErrorInDebug) {
  67.         this.logErrorInDebug = logErrorInDebug;
  68.     }
  69.     public boolean isAddSeverityPrefix() {
  70.         return this.addSeverityPrefix;
  71.     }
  72.     public void setAddSeverityPrefix(boolean addSeverityPrefix) {
  73.         this.addSeverityPrefix = addSeverityPrefix;
  74.     }
  75.     public void addSeverityPrefix(boolean addSeverityPrefix) {
  76.         this.addSeverityPrefix = addSeverityPrefix;
  77.     }
  78.    
  79.     public void debug(String msgError) {
  80.         this.debug(msgError, null);
  81.     }
  82.     public void debug(String msgError, Throwable e) {
  83.        
  84.         if(this.logDebug!=null) {
  85.             this.logDebug.debug(msgError,e);
  86.         }
  87.        
  88.         if(this.sbDebug!=null) {
  89.             if(this.sbDebug.length()>0) {
  90.                 this.sbDebug.append("\n");
  91.             }
  92.             if(this.addSeverityPrefix) {
  93.                 this.sbDebug.append("DEBUG ");
  94.             }
  95.             this.sbDebug.append(msgError);
  96.             try( ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(out);){
  97.                 e.printStackTrace(ps);
  98.                 ps.flush();
  99.                 out.flush();
  100.                 if(this.sbDebug.length()>0) {
  101.                     this.sbDebug.append("\n");
  102.                 }
  103.                 if(this.addSeverityPrefix) {
  104.                     this.sbDebug.append("DEBUG ");
  105.                 }
  106.                 this.sbDebug.append(out.toString());
  107.             }catch(Throwable t) {
  108.                 // ignore
  109.             }
  110.         }
  111.     }
  112.    
  113.     public void error(String msgError) {
  114.         this.error(msgError, null);
  115.     }
  116.     public void error(String msgError, Throwable e) {
  117.        
  118.         if(this.logError!=null) {
  119.             this.logError.error(msgError,e);
  120.         }
  121.         if(this.logDebug!=null && this.logErrorInDebug) {
  122.             if(this.logError==null || this.logError.getName()==null || !this.logError.getName().equals(this.logDebug.getName()) ) {
  123.                 this.logDebug.error(msgError,e);
  124.             }
  125.         }
  126.        
  127.         if(this.sbError!=null || this.sbDebug!=null) {
  128.             if(this.sbError!=null) {
  129.                 if(this.sbError.length()>0) {
  130.                     this.sbError.append("\n");
  131.                 }
  132.                 if(this.addSeverityPrefix) {
  133.                     this.sbError.append("ERROR ");
  134.                 }
  135.                 this.sbError.append(msgError);
  136.             }
  137.             if(this.sbDebug!=null && this.logErrorInDebug) {
  138.                 if(this.sbDebug.length()>0) {
  139.                     this.sbDebug.append("\n");
  140.                 }
  141.                 if(this.addSeverityPrefix) {
  142.                     this.sbDebug.append("ERROR ");
  143.                 }
  144.                 this.sbDebug.append(msgError);
  145.             }
  146.             try( ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(out);){
  147.                 e.printStackTrace(ps);
  148.                 ps.flush();
  149.                 out.flush();
  150.                 if(this.sbError!=null) {
  151.                     if(this.sbError.length()>0) {
  152.                         this.sbError.append("\n");
  153.                     }
  154.                     if(this.addSeverityPrefix) {
  155.                         this.sbError.append("ERROR ");
  156.                     }
  157.                     this.sbError.append(out.toString());
  158.                 }
  159.                 if(this.sbDebug!=null && this.logErrorInDebug) {
  160.                     if(this.sbDebug.length()>0) {
  161.                         this.sbDebug.append("\n");
  162.                     }
  163.                     if(this.addSeverityPrefix) {
  164.                         this.sbDebug.append("ERROR ");
  165.                     }
  166.                     this.sbDebug.append(out.toString());
  167.                 }
  168.             }catch(Throwable t) {
  169.                 // ignore
  170.             }
  171.         }
  172.     }
  173. }