AbstractLog4JLoggerWithContext.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.log4j;

  21. import java.text.SimpleDateFormat;
  22. import java.util.List;

  23. import org.openspcoop2.utils.UtilsException;
  24. import org.openspcoop2.utils.date.DateUtils;
  25. import org.openspcoop2.utils.logger.IContext;
  26. import org.openspcoop2.utils.logger.beans.Property;
  27. import org.openspcoop2.utils.logger.beans.context.core.AbstractContext;
  28. import org.openspcoop2.utils.logger.beans.context.core.AbstractContextWithClient;
  29. import org.openspcoop2.utils.logger.beans.context.core.AbstractTransactionWithClient;
  30. import org.openspcoop2.utils.logger.beans.context.core.BaseClient;
  31. import org.openspcoop2.utils.logger.beans.context.core.BaseServer;
  32. import org.openspcoop2.utils.logger.beans.context.core.ConnectionMessage;
  33. import org.openspcoop2.utils.logger.beans.context.core.HttpClient;
  34. import org.openspcoop2.utils.logger.beans.context.core.HttpServer;
  35. import org.openspcoop2.utils.logger.config.DiagnosticConfig;
  36. import org.openspcoop2.utils.logger.config.Log4jConfig;
  37. import org.openspcoop2.utils.logger.config.MultiLoggerConfig;

  38. /**
  39.  * AbstractLog4JLoggerWithContext
  40.  *
  41.  * @author Poli Andrea (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public abstract class AbstractLog4JLoggerWithContext extends AbstractLog4JLogger  {

  46.     public AbstractLog4JLoggerWithContext(MultiLoggerConfig config) throws UtilsException {
  47.         super(config.getDiagnosticConfig(),config.getLog4jConfig());
  48.         if(config.isLog4jLoggerEnabled()){
  49.             if(config.getDiagnosticSeverityFilter()!=null){
  50.                 AbstractLog4JLogger.setDiagnosticSeverity(config.getDiagnosticSeverityFilter());
  51.             }
  52.             if(config.getEventSeverityFilter()!=null){
  53.                 AbstractLog4JLogger.setEventSeverity(config.getEventSeverityFilter());
  54.             }
  55.         }
  56.     }

  57.     public AbstractLog4JLoggerWithContext(DiagnosticConfig diagnosticConfig, Log4jConfig logConfig) throws UtilsException {
  58.         super(diagnosticConfig, logConfig);
  59.     }
  60.    
  61.     protected AbstractContext context;
  62.     protected abstract AbstractContext newContext();
  63.     protected abstract BaseClient getClient();
  64.     protected abstract List<BaseServer> getServers();
  65.    
  66.     @Override
  67.     public void initLogger() throws UtilsException{
  68.         this.initLogger(this.newContext());
  69.     }
  70.     @Override
  71.     public void initLogger(String idTransazione) throws UtilsException{
  72.         this.initLogger(idTransazione, this.newContext());
  73.     }
  74.     @Override
  75.     public void initLogger(IContext contextParam) throws UtilsException{
  76.         this.initLogger(null,contextParam);
  77.     }
  78.     @Override
  79.     public void initLogger(String idTransazione, IContext contextParam) throws UtilsException{
  80.         this.context = (AbstractContext) contextParam;
  81.         super.initLogger(idTransazione);
  82.         this.context.setIdTransaction(this.idTransaction);
  83.     }
  84.    
  85.     @Override
  86.     public IContext getContext() throws UtilsException {
  87.         return this.context;
  88.     }
  89.    
  90.     @Override
  91.     protected void logContext(IContext contextParam, StringBuilder showContext){
  92.        
  93.         SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  94.        
  95.         AbstractContext context = (AbstractContext) contextParam;
  96.        
  97.         if( context.getTransaction().getProtocol()!=null){
  98.             showContext.append("Protocol="+context.getTransaction().getProtocol()+"\n");
  99.         }
  100.         if( context.getTransaction().getDomain()!=null){
  101.             showContext.append("Domain="+context.getTransaction().getDomain()+"\n");
  102.         }
  103.         if( context.getTransaction().getRole()!=null){
  104.             showContext.append("Role="+context.getTransaction().getRole().name()+"\n");
  105.         }
  106.         if( context.getTransaction().getResult()!=null){
  107.             showContext.append("Result="+context.getTransaction().getResult().name()+"\n");
  108.         }
  109.         if( context.getTransaction().getConversationId()!=null){
  110.             showContext.append("ConversationId="+context.getTransaction().getConversationId()+"\n");
  111.         }
  112.         if( context.getTransaction().getClusterId()!=null){
  113.             showContext.append("ClusterId="+context.getTransaction().getClusterId()+"\n");
  114.         }
  115.        
  116.         if( this.getClient()!=null){
  117.             showContext.append("*** Client ***\n");
  118.             if( this.getClient().getPrincipal()!=null){
  119.                 showContext.append("Principal="+this.getClient().getPrincipal()+"\n");
  120.             }
  121.             if( this.getClient().getName()!=null){
  122.                 showContext.append("Name="+this.getClient().getName()+"\n");
  123.             }
  124.             if( this.getClient().getInvocationEndpoint()!=null){
  125.                 showContext.append("InvocationEndpoint="+this.getClient().getInvocationEndpoint()+"\n");
  126.             }
  127.             if( this.getClient().getInterfaceName()!=null){
  128.                 showContext.append("InterfaceName="+this.getClient().getInterfaceName()+"\n");
  129.             }
  130.             if( this.getClient() instanceof HttpClient) {
  131.                 HttpClient httpClient = (HttpClient) this.getClient();
  132.                 if( httpClient.getResponseStatusCode()>0){
  133.                     showContext.append("ResponseStatusCode="+httpClient.getResponseStatusCode()+"\n");
  134.                 }
  135.                 if(httpClient.getTransportRequestMethod()!=null) {
  136.                     showContext.append("HttpMethod="+httpClient.getTransportRequestMethod().name()+"\n");
  137.                 }
  138.                 if(httpClient.getSocketClientAddress()!=null) {
  139.                     showContext.append("SocketClientAddress="+httpClient.getSocketClientAddress()+"\n");
  140.                 }
  141.                 if(httpClient.getTransportClientAddress()!=null) {
  142.                     showContext.append("TransportClientAddress="+httpClient.getTransportClientAddress()+"\n");
  143.                 }
  144.             }
  145.             if( this.getClient().getGenericProperties()!=null && this.getClient().getGenericProperties().size()>0 ){
  146.                 for (Property property : this.getClient().getGenericPropertiesAsList()) {
  147.                     showContext.append("Property."+property.getName()+"="+property.getValue()+"\n");        
  148.                 }
  149.             }
  150.         }
  151.                
  152.         if( context.getTransaction().getFrom()!=null){
  153.             showContext.append("*** From ***\n");
  154.             if( context.getTransaction().getFrom().getType()!=null){
  155.                 showContext.append("Type="+context.getTransaction().getFrom().getType()+"\n");
  156.             }
  157.             if( context.getTransaction().getFrom().getName()!=null){
  158.                 showContext.append("Name="+context.getTransaction().getFrom().getName()+"\n");
  159.             }
  160.             if( context.getTransaction().getFrom().getAddress()!=null){
  161.                 showContext.append("Address="+context.getTransaction().getFrom().getAddress()+"\n");
  162.             }
  163.         }
  164.        
  165.         if( context.getTransaction().getTo()!=null){
  166.             showContext.append("*** To ***\n");
  167.             if( context.getTransaction().getTo().getType()!=null){
  168.                 showContext.append("Type="+context.getTransaction().getTo().getType()+"\n");
  169.             }
  170.             if( context.getTransaction().getTo().getName()!=null){
  171.                 showContext.append("Name="+context.getTransaction().getTo().getName()+"\n");
  172.             }
  173.             if( context.getTransaction().getTo().getAddress()!=null){
  174.                 showContext.append("Address="+context.getTransaction().getTo().getAddress()+"\n");
  175.             }
  176.         }
  177.        
  178.         if( context.getTransaction().getService()!=null){
  179.             showContext.append("*** Service ***\n");
  180.             if( context.getTransaction().getService().getType()!=null){
  181.                 showContext.append("Type="+context.getTransaction().getService().getType()+"\n");
  182.             }
  183.             if( context.getTransaction().getService().getName()!=null){
  184.                 showContext.append("Name="+context.getTransaction().getService().getName()+"\n");
  185.             }
  186.             if( context.getTransaction().getService().getVersion()!=null){
  187.                 showContext.append("Version="+context.getTransaction().getService().getVersion()+"\n");
  188.             }
  189.         }
  190.        
  191.         if( context.getTransaction().getOperation()!=null){
  192.             showContext.append("*** Operation ***\n");
  193.             if( context.getTransaction().getOperation().getName()!=null){
  194.                 showContext.append("Name="+context.getTransaction().getOperation().getName()+"\n");
  195.             }
  196.             if( context.getTransaction().getOperation().getMode()!=null){
  197.                 showContext.append("FlowMode="+context.getTransaction().getOperation().getMode().name()+"\n");
  198.             }
  199.         }
  200.        
  201.         List<BaseServer> listServer = this.getServers();
  202.         if(listServer!=null && !listServer.isEmpty()) {
  203.             int indexServer = 1;
  204.             for (BaseServer baseServer : listServer) {
  205.                 if( baseServer!=null){
  206.                     if(listServer.size()<2) {
  207.                         showContext.append("*** Server ***\n");
  208.                         if( baseServer.getName()!=null){
  209.                             showContext.append("Name="+baseServer.getName()+"\n");
  210.                         }
  211.                     }
  212.                     else if(baseServer.getName()!=null) {
  213.                         showContext.append("*** Server '"+baseServer.getName()+"' ***\n");
  214.                     }
  215.                     else {
  216.                         showContext.append("*** Server-"+indexServer+" ***\n");
  217.                     }
  218.                     if( baseServer.getIdOperation()!=null){
  219.                         showContext.append("IdOperation="+baseServer.getIdOperation()+"\n");
  220.                     }
  221.                     if( baseServer.getEndpointType()!=null){
  222.                         showContext.append("EndpointType="+baseServer.getEndpointType()+"\n");
  223.                     }
  224.                     if( baseServer.getEndpoint()!=null){
  225.                         showContext.append("Endpoint="+baseServer.getEndpoint()+"\n");
  226.                     }
  227.                     if(baseServer instanceof HttpServer) {
  228.                         HttpServer httpServer = (HttpServer) baseServer;
  229.                         if( httpServer.getResponseStatusCode()>0){
  230.                             showContext.append("ResponseStatusCode="+httpServer.getResponseStatusCode()+"\n");
  231.                         }
  232.                         if(httpServer.getTransportRequestMethod()!=null) {
  233.                             showContext.append("HttpMethod="+httpServer.getTransportRequestMethod().name()+"\n");
  234.                         }
  235.                     }
  236.                     if( baseServer.getGenericProperties()!=null && baseServer.getGenericProperties().size()>0 ){
  237.                         for (Property property : baseServer.getGenericPropertiesAsList()) {
  238.                             showContext.append("Property."+property.getName()+"="+property.getValue()+"\n");        
  239.                         }
  240.                     }
  241.                     if(baseServer.getRequest()!=null) {
  242.                         this._log(true, baseServer.getRequest(), showContext, dateformat);
  243.                     }
  244.                     if(baseServer.getResponse()!=null) {
  245.                         this._log(false, baseServer.getResponse(), showContext, dateformat);
  246.                     }
  247.                
  248.                 }
  249.                 indexServer++;
  250.             }
  251.         }

  252.         if(context.getTransaction() instanceof AbstractTransactionWithClient) {
  253.            
  254.             AbstractTransactionWithClient trWithClient = (AbstractTransactionWithClient) context.getTransaction();
  255.            
  256.             if( trWithClient.getRequest()!=null){
  257.                 showContext.append("*** Request ***\n");
  258.                 if( trWithClient.getRequest().getIdentifier()!=null){
  259.                     if( trWithClient.getRequest().getIdentifier().getId()!=null){
  260.                         showContext.append("Identifier="+trWithClient.getRequest().getIdentifier().getId()+"\n");
  261.                     }
  262.                     if( trWithClient.getRequest().getIdentifier().getDate()!=null){
  263.                         showContext.append("DateIdentifier="+dateformat.format(trWithClient.getRequest().getIdentifier().getDate())+"\n");
  264.                     }
  265.                 }
  266.                 if( trWithClient.getRequest().getDate()!=null){
  267.                     showContext.append("Date="+dateformat.format(trWithClient.getRequest().getDate())+"\n");
  268.                 }
  269.                 if( trWithClient.getRequest().getSize()!=null){
  270.                     showContext.append("Size="+trWithClient.getRequest().getSize()+"\n");
  271.                 }
  272.                 if( trWithClient.getRequest().getCorrelationIdentifier()!=null){
  273.                     showContext.append("CorrelationIdentifier="+trWithClient.getRequest().getCorrelationIdentifier()+"\n");    
  274.                 }
  275.                 if( trWithClient.getRequest().getGenericProperties()!=null && trWithClient.getRequest().getGenericProperties().size()>0 ){
  276.                     for (Property property : trWithClient.getRequest().getGenericPropertiesAsList()) {
  277.                         showContext.append("Property."+property.getName()+"="+property.getValue()+"\n");        
  278.                     }
  279.                 }
  280.             }
  281.            
  282.             if( trWithClient.getResponse()!=null){
  283.                 showContext.append("*** Response ***\n");
  284.                 if( trWithClient.getResponse().getIdentifier()!=null){
  285.                     if( trWithClient.getResponse().getIdentifier().getId()!=null){
  286.                         showContext.append("Identifier="+trWithClient.getResponse().getIdentifier().getId()+"\n");
  287.                     }
  288.                     if( trWithClient.getResponse().getIdentifier().getDate()!=null){
  289.                         showContext.append("DateIdentifier="+dateformat.format(trWithClient.getResponse().getIdentifier().getDate())+"\n");
  290.                     }
  291.                 }
  292.                 if( trWithClient.getResponse().getDate()!=null){
  293.                     showContext.append("Date="+dateformat.format(trWithClient.getResponse().getDate())+"\n");
  294.                 }
  295.                 if( trWithClient.getResponse().getSize()!=null){
  296.                     showContext.append("Size="+trWithClient.getResponse().getSize()+"\n");
  297.                 }
  298.                 if( trWithClient.getResponse().getCorrelationIdentifier()!=null){
  299.                     showContext.append("CorrelationIdentifier="+trWithClient.getResponse().getCorrelationIdentifier()+"\n");        
  300.                 }
  301.                 if( trWithClient.getResponse().getGenericProperties()!=null && trWithClient.getResponse().getGenericProperties().size()>0 ){
  302.                     for (Property property : trWithClient.getResponse().getGenericPropertiesAsList()) {
  303.                         showContext.append("Property."+property.getName()+"="+property.getValue()+"\n");        
  304.                     }
  305.                 }
  306.                 if( trWithClient.getResponse().getFault()!=null){
  307.                     int max = 250 * 1024;
  308.                     String text = null;
  309.                     try{
  310.                         text = org.openspcoop2.utils.Utilities.convertToPrintableText(trWithClient.getResponse().getFault(),max);
  311.                     }catch(Exception e){
  312.                         text = e.getMessage();
  313.                     }
  314.                     showContext.append("Fault="+text+"\n");
  315.                 }
  316.             }
  317.            
  318.         }
  319.        
  320.         showContext.append("\n");
  321.        
  322.     }
  323.        
  324.     private void _log(boolean request, ConnectionMessage message ,StringBuilder showContext,SimpleDateFormat dateformat) {
  325.         if( message!=null){
  326.             String prefix = request ? "Request-" : "Response-";
  327.             if( message.getIdMessage()!=null){
  328.                 showContext.append(prefix+"IdMessage="+message.getIdMessage()+"\n");
  329.             }
  330.             if( message.getDate()!=null){
  331.                 showContext.append(prefix+"Date="+dateformat.format(message.getDate())+"\n");
  332.             }
  333.             if( message.getSize()!=null){
  334.                 showContext.append(prefix+"Size="+message.getSize()+"\n");
  335.             }
  336.         }
  337.     }
  338.    

  339.     @Override
  340.     protected String getDomain(IContext contextParam){
  341.         AbstractContext context = (AbstractContext) contextParam;
  342.         return context.getTransaction().getDomain();
  343.     }
  344.     @Override
  345.     protected String getRequestIdentifier(IContext contextParam){
  346.         AbstractContext context = (AbstractContext) contextParam;
  347.         if(context instanceof AbstractContextWithClient) {
  348.             if(((AbstractContextWithClient)context).getRequest()!=null)
  349.                 return ((AbstractContextWithClient)context).getRequest().getId();
  350.         }
  351.         return null;
  352.     }
  353.     @Override
  354.     protected String getResponseIdentifier(IContext contextParam){
  355.         AbstractContext context = (AbstractContext) contextParam;
  356.         if(context instanceof AbstractContextWithClient) {
  357.             if(((AbstractContextWithClient)context).getResponse()!=null)
  358.                 return ((AbstractContextWithClient)context).getResponse().getId();
  359.         }
  360.         return null;
  361.     }
  362.     @Override
  363.     protected String getRequestCorrelationIdentifier(IContext contextParam){
  364.         AbstractContext context = (AbstractContext) contextParam;
  365.         if(context instanceof AbstractContextWithClient) {
  366.             if(((AbstractContextWithClient)context).getRequest()!=null)
  367.                 return ((AbstractContextWithClient)context).getRequest().getCorrelationIdentifier();
  368.         }
  369.         return null;
  370.     }
  371.     @Override
  372.     protected String getResponseCorrelationIdentifier(IContext contextParam){
  373.         AbstractContext context = (AbstractContext) contextParam;
  374.         if(context instanceof AbstractContextWithClient) {
  375.             if(((AbstractContextWithClient)context).getResponse()!=null)
  376.                 return ((AbstractContextWithClient)context).getResponse().getCorrelationIdentifier();
  377.         }
  378.         return null;
  379.     }
  380.     @Override
  381.     protected String getClient(IContext contextParam){
  382.         AbstractContext context = (AbstractContext) contextParam;
  383.         if(context.getTransaction()==null) {
  384.             return null;
  385.         }
  386.         if(context.getTransaction() instanceof AbstractTransactionWithClient) {
  387.             BaseClient baseClient = ((AbstractTransactionWithClient)context.getTransaction()).getClient();
  388.             if(baseClient!=null) {
  389.                 return baseClient.getName();
  390.             }
  391.         }
  392.         return null;
  393.     }
  394.     @Override
  395.     protected String getClientPrincipal(IContext contextParam){
  396.         AbstractContext context = (AbstractContext) contextParam;
  397.         if(context.getTransaction()==null) {
  398.             return null;
  399.         }
  400.         if(context.getTransaction() instanceof AbstractTransactionWithClient) {
  401.             BaseClient baseClient = ((AbstractTransactionWithClient)context.getTransaction()).getClient();
  402.             if(baseClient!=null) {
  403.                 return baseClient.getPrincipal();
  404.             }
  405.         }
  406.         return null;
  407.     }
  408.     @Override
  409.     protected String getFrom(IContext contextParam){
  410.         AbstractContext context = (AbstractContext) contextParam;
  411.         if(context.getTransaction()==null) {
  412.             return null;
  413.         }
  414.         if(context.getTransaction().getFrom()!=null){
  415.             if(context.getTransaction().getFrom().getName()!=null){
  416.                 return context.getTransaction().getFrom().getName();
  417.             }
  418.         }
  419.         return null;
  420.     }
  421.     @Override
  422.     protected String getTo(IContext contextParam){
  423.         AbstractContext context = (AbstractContext) contextParam;
  424.         if(context.getTransaction()==null) {
  425.             return null;
  426.         }
  427.         if(context.getTransaction().getTo()!=null){
  428.             if(context.getTransaction().getTo().getName()!=null){
  429.                 return context.getTransaction().getTo().getName();
  430.             }
  431.         }
  432.         return null;
  433.     }
  434.     @Override
  435.     protected String getService(IContext contextParam){
  436.         AbstractContext context = (AbstractContext) contextParam;
  437.         if(context.getTransaction()==null) {
  438.             return null;
  439.         }
  440.         if(context.getTransaction().getService()!=null){
  441.             if(context.getTransaction().getService().getName()!=null){
  442.                 return context.getTransaction().getService().getName();
  443.             }
  444.         }
  445.         return null;
  446.     }
  447.     @Override
  448.     protected String getOperation(IContext contextParam){
  449.         AbstractContext context = (AbstractContext) contextParam;
  450.         if(context.getTransaction()==null) {
  451.             return null;
  452.         }
  453.         if(context.getTransaction().getOperation()!=null){
  454.             if(context.getTransaction().getOperation().getName()!=null){
  455.                 return context.getTransaction().getOperation().getName();
  456.             }
  457.         }
  458.         return null;
  459.     }


  460.    
  461. }