LoggerProperties.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.core.config.rs.server.config;

  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.utils.Costanti;
  28. import org.openspcoop2.utils.LoggerWrapperFactory;
  29. import org.openspcoop2.utils.UtilsException;
  30. import org.openspcoop2.utils.logger.LoggerFactory;
  31. import org.openspcoop2.utils.logger.config.DiagnosticConfig;
  32. import org.openspcoop2.utils.logger.config.Log4jConfig;
  33. import org.openspcoop2.utils.logger.config.MultiLoggerConfig;
  34. import org.openspcoop2.utils.logger.log4j.Log4jLoggerWithApplicationContext;
  35. import org.openspcoop2.utils.properties.CollectionProperties;
  36. import org.openspcoop2.utils.properties.PropertiesUtilities;

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

  49.     public static void initialize(Logger logConsole,String rootDirectory,Properties objectProperties) throws IOException, UtilsException{

  50.         // Originale
  51.         java.util.Properties loggerProperties = new java.util.Properties();
  52.         java.io.File loggerFile = null;
  53.         if(rootDirectory!=null)
  54.             loggerFile = new java.io.File(rootDirectory+"rs-api-config.log4j2.properties");
  55.         else
  56.             loggerFile = new java.io.File("rs-api-config.log4j2.properties");
  57.         if(!loggerFile .exists() ){
  58.             loggerProperties.load(LoggerProperties.class.getResourceAsStream("/rs-api-config.log4j2.properties"));
  59.         }else{
  60.             try (FileInputStream fin = new java.io.FileInputStream(loggerFile);){
  61.                 loggerProperties.load(fin);
  62.             }
  63.         }

  64.         // File Local Implementation
  65.         CollectionProperties loggerPropertiesRidefinito =  
  66.                 PropertiesUtilities.searchLocalImplementation(Costanti.OPENSPCOOP2_LOCAL_HOME,logConsole, ConstantsEnv.OPENSPCOOP2_LOGGER_PROPERTIES, ConstantsEnv.OPENSPCOOP2_LOGGER_LOCAL_PATH,  rootDirectory);
  67.         if(loggerPropertiesRidefinito!=null && loggerPropertiesRidefinito.size()>0){
  68.             Enumeration<?> ridefinito = loggerPropertiesRidefinito.keys();
  69.             while (ridefinito.hasMoreElements()) {
  70.                 String key = (String) ridefinito.nextElement();
  71.                 String value = loggerPropertiesRidefinito.get(key);
  72.                 if(loggerProperties.containsKey(key)){
  73.                     //Object o =
  74.                     loggerProperties.remove(key);
  75.                 }
  76.                 loggerProperties.put(key, value);
  77.                 /**System.out.println("CHECK NUOVO VALORE ["+key+"]: "+loggerProperties.get(key));*/
  78.             }
  79.         }

  80.         // File Object Implementation
  81.         if(objectProperties!=null && objectProperties.size()>0){
  82.             Enumeration<?> ridefinito = objectProperties.keys();
  83.             while (ridefinito.hasMoreElements()) {
  84.                 String key = (String) ridefinito.nextElement();
  85.                 String value = (String) objectProperties.get(key);
  86.                 if(loggerProperties.containsKey(key)){
  87.                     //Object o =
  88.                     loggerProperties.remove(key);
  89.                 }
  90.                 loggerProperties.put(key, value);
  91.                 /**System.out.println("CHECK NUOVO VALORE ["+key+"]: "+loggerProperties.get(key));*/
  92.             }
  93.         }
  94.         LoggerWrapperFactory.patchLoggers(loggerProperties,
  95.                 org.openspcoop2.utils.Costanti.ENV_LOG_API_CONFIG,
  96.                 Map.of(org.openspcoop2.utils.Costanti.VAR_LOGGER_APPNAME, "govwayAPIConfig"));
  97.        
  98.         // DIAGNOSTIC CONFIGURATION
  99.         DiagnosticConfig diagnosticConfig = DiagnosticConfig.newEmptyDiagnosticConfig();
  100.         diagnosticConfig.setThrowExceptionPlaceholderFailedResolution(true);
  101.                
  102.                
  103.         // LOG4J CONFIGURATION
  104.         Log4jConfig log4jConfig = new Log4jConfig();
  105.         log4jConfig.setLog4jConfigProperties(loggerProperties);

  106.         // MULTILOGGER
  107.                
  108.         MultiLoggerConfig mConfig = new MultiLoggerConfig();
  109.        
  110.         mConfig.setDiagnosticConfig(diagnosticConfig);
  111.                
  112.         /**mConfig.setDiagnosticSeverityFilter(Severity.DEBUG_HIGH);
  113.         mConfig.setEventSeverityFilter(Severity.INFO);*/
  114.                
  115.         mConfig.setLog4jLoggerEnabled(true);
  116.         mConfig.setLog4jConfig(log4jConfig);
  117.                
  118.         mConfig.setDbLoggerEnabled(false);
  119.         /**mConfig.setDatabaseConfig(dbConfig);*/
  120.            
  121.         try {
  122.             LoggerFactory.initialize(Log4jLoggerWithApplicationContext.class.getName(),
  123.                     mConfig);
  124.            
  125.             LoggerFactory.newLogger(); // inizializza la configurazione log4j
  126.             /**LoggerWrapperFactory.setLogConfiguration(loggerProperties);*/
  127.         }catch(Exception e) {
  128.             throw new UtilsException(e.getMessage(),e);
  129.         }
  130.        
  131.     }

  132.    
  133.     public static Logger getLoggerCore(){
  134.         if(!Startup.isInitializedLog()){
  135.             Startup.initLog();
  136.         }
  137.         return LoggerWrapperFactory.getLogger("config.core");
  138.     }
  139.    
  140.     public static Logger getLoggerDAO(){
  141.         return LoggerWrapperFactory.getLogger("config.dao");
  142.     }
  143. }