AlarmLogger.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.monitor.engine.alarm;

  21. import org.apache.commons.collections4.queue.CircularFifoQueue;
  22. import org.openspcoop2.monitor.sdk.alarm.IAlarmLogger;
  23. import org.openspcoop2.utils.date.DateManager;
  24. import org.openspcoop2.utils.date.DateUtils;
  25. import org.slf4j.Logger;

  26. /**    
  27.  * AlarmLogger
  28.  *
  29.  * @author Poli Andrea (poli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class AlarmLogger implements IAlarmLogger {

  34.     private CircularFifoQueue<String> logHistory;
  35.     private static final int NUMERO_EVENTI = 20;
  36.    
  37.     private String threadName;
  38.     public void setThreadName(String threadName) {
  39.         this.threadName = threadName;
  40.     }

  41.     private String nome;
  42.     private String id;
  43.     private Logger _log;
  44.     private Logger _logSql;
  45.    
  46.    
  47.     public AlarmLogger(String nome, String id, String threadName, Logger log, Logger logSql) {
  48.         this.logHistory = new CircularFifoQueue<String>(NUMERO_EVENTI); // mantiene gli ultimi x log
  49.         this.nome = nome;
  50.         this.id = id;
  51.         this.threadName = threadName;
  52.         this._log = log;
  53.         this._logSql = logSql;
  54.     }
  55.    

  56.     @Override
  57.     public Logger getInternalLogger() {
  58.         return this._log;
  59.     }
  60.     @Override
  61.     public Logger getInternalSqlLogger() {
  62.         return this._logSql;
  63.     }
  64.    
  65.     @Override
  66.     public void debug(String messaggio) {
  67.         _log(messaggio, true, false, false, false, null);
  68.     }
  69.     @Override
  70.     public void debug(String messaggio,  Throwable t) {
  71.         _log(messaggio, true, false, false, false, t);
  72.     }
  73.    
  74.     @Override
  75.     public void info(String messaggio) {
  76.         _log(messaggio, false, true, false, false, null);
  77.     }
  78.     @Override
  79.     public void info(String messaggio,  Throwable t) {
  80.         _log(messaggio, false, true, false, false, t);
  81.     }
  82.    
  83.     @Override
  84.     public void warn(String messaggio) {
  85.         _log(messaggio, false, false, true, false, null);
  86.     }
  87.     @Override
  88.     public void warn(String messaggio,  Throwable t) {
  89.         _log(messaggio, false, false, true, false, t);
  90.     }
  91.    
  92.     @Override
  93.     public void error(String messaggio) {
  94.         _log(messaggio, false, false, false, true, null);
  95.     }
  96.     @Override
  97.     public void error(String messaggio,  Throwable t) {
  98.         _log(messaggio, false, false, false, true, t);
  99.     }
  100.    
  101.     private void _log(String messaggio, boolean debug, boolean info, boolean warning, boolean error, Throwable e) {
  102.         String prefix = buildPrefix(this.threadName,this.nome,this.id);
  103.         String logMsg = prefix+messaggio;
  104.         if(debug) {
  105.             if(e!=null) {
  106.                 this._log.debug(logMsg,e);
  107.             }
  108.             else {
  109.                 this._log.debug(logMsg);
  110.             }
  111.         }
  112.         else if(info) {
  113.             if(e!=null) {
  114.                 this._log.info(logMsg,e);
  115.             }
  116.             else {
  117.                 this._log.info(logMsg);
  118.             }
  119.         }
  120.         else if(warning) {
  121.             if(e!=null) {
  122.                 this._log.warn(logMsg,e);
  123.             }
  124.             else {
  125.                 this._log.warn(logMsg);
  126.             }
  127.         }
  128.         else if(error) {
  129.             if(e!=null) {
  130.                 this._log.error(logMsg,e);
  131.             }
  132.             else {
  133.                 this._log.error(logMsg);
  134.             }
  135.         }
  136.         String data = " <"+DateUtils.getSimpleDateFormatMs().format(DateManager.getDate())+"> ";
  137.         this.logHistory.add("\n"+data+ messaggio);
  138.     }

  139.     public static String buildPrefix(String threadName, String aliasAllarme, String idAllarme) {
  140.         String prefix = "";
  141.         if(threadName!=null) {
  142.             prefix = "["+threadName+"] ";
  143.         }
  144.         prefix = prefix+"Allarme '"+aliasAllarme+"' (" + idAllarme+ ") ";
  145.         return prefix;
  146.     }

  147.     public String getStatoAllarme() {
  148.         StringBuilder sb = new StringBuilder();
  149.         sb.append("================================================================\n");
  150.         sb.append("Dati Identificativi\n");
  151.         sb.append("- Nome: ").append(this.nome).append("\n");
  152.         sb.append("- IdAllarme: ").append(this.id).append("\n");
  153.         sb.append("- IdThread: ").append(this.threadName).append("\n");
  154.         sb.append("\nUltimi "+NUMERO_EVENTI+" eventi:");
  155.         if(this.logHistory.isFull()) {
  156.             sb.append("\n...");
  157.         }
  158.         String s = this.logHistory.toString();
  159.         if(s.startsWith("[") && s.length()>1) {
  160.             s = s.substring(1);
  161.         }
  162.         if(s.endsWith("]") && s.length()>1) {
  163.             s = s.substring(0, s.length()-1);
  164.         }
  165.         sb.append(s);
  166.         sb.append("\n");
  167.         sb.append("================================================================\n");
  168.         return sb.toString();
  169.     }
  170. }