ControlStationLogger.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.web.ctrlstat.core;

  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.util.Enumeration;
  24. import java.util.Map;
  25. import java.util.Properties;

  26. import org.slf4j.Logger;
  27. import org.openspcoop2.pdd.core.CostantiPdD;
  28. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  29. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  30. import org.openspcoop2.utils.LoggerWrapperFactory;
  31. import org.openspcoop2.utils.Utilities;
  32. import org.openspcoop2.utils.UtilsException;
  33. import org.openspcoop2.utils.properties.CollectionProperties;
  34. import org.openspcoop2.utils.properties.PropertiesUtilities;
  35. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  36. import org.openspcoop2.web.ctrlstat.costanti.CostantiUtilities;
  37. import org.openspcoop2.web.ctrlstat.costanti.TipoProperties;

  38. /**
  39.  * ControlStationLogger
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class ControlStationLogger {

  47.     public static void initialize(Logger logConsole,String rootDirectory, String confPropertyName, String confLocalPathPrefix,Properties objectProperties,boolean appendActualConfiguration) throws IOException, UtilsException{

  48.         // Originale
  49.         java.util.Properties loggerProperties = new java.util.Properties();
  50.         java.io.File loggerFile = null;
  51.         if(rootDirectory!=null)
  52.             loggerFile = new java.io.File(rootDirectory+"console.log4j2.properties");
  53.         else
  54.             loggerFile = new java.io.File("console.log4j2.properties");
  55.         if(loggerFile .exists() == false ){
  56.             loggerProperties.load(OpenSPCoop2Logger.class.getResourceAsStream("/console.log4j2.properties"));
  57.         }else{
  58.             FileInputStream fin = null;
  59.             try{
  60.                 fin = new java.io.FileInputStream(loggerFile);
  61.                 loggerProperties.load(fin);
  62.             }finally{
  63.                 try{
  64.                     if(fin!=null){
  65.                         fin.close();
  66.                     }
  67.                 }catch(Exception eClose){
  68.                     // close
  69.                 }
  70.             }
  71.         }

  72.         // File Local Implementation
  73.         CollectionProperties loggerPropertiesRidefinito =  
  74.                 PropertiesUtilities.searchLocalImplementation(CostantiPdD.OPENSPCOOP2_LOCAL_HOME,logConsole,
  75.                         CostantiUtilities.get_PROPERTY_NAME(TipoProperties.LOGGER, confPropertyName),
  76.                         CostantiUtilities.get_LOCAL_PATH(TipoProperties.LOGGER, confLocalPathPrefix),
  77.                         rootDirectory);
  78.         if(loggerPropertiesRidefinito!=null && loggerPropertiesRidefinito.size()>0){
  79.             Enumeration<?> ridefinito = loggerPropertiesRidefinito.keys();
  80.             while (ridefinito.hasMoreElements()) {
  81.                 String key = (String) ridefinito.nextElement();
  82.                 String value = (String) loggerPropertiesRidefinito.get(key);
  83.                 if(loggerProperties.containsKey(key)){
  84.                     //Object o =
  85.                     loggerProperties.remove(key);
  86.                 }
  87.                 loggerProperties.put(key, value);
  88.                 //System.out.println("CHECK NUOVO VALORE: "+loggerProperties.get(key));
  89.             }
  90.         }

  91.         // File Object Implementation
  92.         if(objectProperties!=null && objectProperties.size()>0){
  93.             Enumeration<?> ridefinito = objectProperties.keys();
  94.             while (ridefinito.hasMoreElements()) {
  95.                 String key = (String) ridefinito.nextElement();
  96.                 String value = (String) objectProperties.get(key);
  97.                 if(loggerProperties.containsKey(key)){
  98.                     //Object o =
  99.                     loggerProperties.remove(key);
  100.                 }
  101.                 loggerProperties.put(key, value);
  102.                 //System.out.println("CHECK NUOVO VALORE: "+loggerProperties.get(key));
  103.             }
  104.         }

  105.         LoggerWrapperFactory.patchLoggers(loggerProperties,
  106.                 org.openspcoop2.utils.Costanti.ENV_LOG_CONSOLE,
  107.                 Map.of(org.openspcoop2.utils.Costanti.VAR_LOGGER_APPNAME, "govwayConsole"));

  108.         if(appendActualConfiguration){
  109.             System.out.println("[govwayConsole] Attendo inizializzazione GovWay prima di appender la configurazione Log4J ...");
  110.             int i=0;
  111.             int limit = 60;
  112.             while(OpenSPCoop2Startup.initialize==false && i<limit){
  113.                 Utilities.sleep(1000);
  114.                 i++;
  115.                 if(i%10==0){
  116.                     System.out.println("[govwayConsole] Attendo inizializzazione GovWay ...");
  117.                 }
  118.             }
  119.            
  120.             if(OpenSPCoop2Startup.initialize==false){
  121.                 throw new UtilsException("[govwayConsole] Inizializzazione GovWay non rilevata");
  122.             }
  123.            
  124.             System.out.println("[govwayConsole] Configurazione Log4J ...");
  125.             LoggerWrapperFactory.setLogConfiguration(loggerProperties,true);
  126.             System.out.println("[govwayConsole] Configurazione Log4J aggiunta");
  127.         }
  128.         else{
  129.             LoggerWrapperFactory.setLogConfiguration(loggerProperties);
  130.         }


  131.     }

  132.     public static Logger getPddConsoleCoreLogger(){
  133.          return LoggerWrapperFactory.getLogger("govwayConsole.core");
  134.     }
  135.    
  136.     public static Logger getSmistatoreLogger(){
  137.          return LoggerWrapperFactory.getLogger("govwayConsole.gestori.smistatore");
  138.     }
  139.    
  140.     public static Logger getGestorePddLogger(){
  141.          return LoggerWrapperFactory.getLogger("govwayConsole.gestori.config");
  142.     }
  143.    
  144.     public static Logger getGestoreGELogger(){
  145.          return LoggerWrapperFactory.getLogger("govwayConsole.gestori.gestoreEventi");
  146.     }
  147.    
  148.     public static Logger getGestoreRegistroLogger(){
  149.          return LoggerWrapperFactory.getLogger("govwayConsole.gestori.registry");
  150.     }
  151.    
  152.     public static Logger getGestoreAutorizzazioneLogger(){
  153.          return LoggerWrapperFactory.getLogger("govwayConsole.gestori.auth");
  154.     }
  155.    
  156.     public static Logger getDriverDBPddConsoleLogger(){
  157.         return LoggerWrapperFactory.getLogger(CostantiControlStation.DRIVER_DB_PDD_CONSOLE_LOGGER);
  158.     }
  159.    
  160.     public static Logger getSincronizzatoreLogger(){
  161.          return LoggerWrapperFactory.getLogger("govwayConsole.sincronizzatore");
  162.     }
  163.    
  164. }