3 Replies Latest reply on Jun 12, 2009 5:26 PM by gonorrhea

    Custom Loggin

    daniele4

      Hi,
      can i customize the logger to print some other information like session id, username ..??!
      I read a lot post but i didnt find a easy solution?!
      Any ideas?


      thanks

        • 1. Re: Custom Loggin
          gonorrhea

          We use NTLM auth.  I have some code that prints the sessionId, node, username, etc.  I'll try to locate it and post it...

          • 2. Re: Custom Loggin
            daniele4

            Oh very good!I am waiting that!!


            thanks a lot

            • 3. Re: Custom Loggin
              gonorrhea
              package com.cox.bets.session;
              import java.net.InetAddress;
              import java.net.NetworkInterface;
              import java.net.SocketException;
              import java.sql.Connection;
              import java.sql.DatabaseMetaData;
              import java.sql.SQLException;
              import java.util.Enumeration;
              import java.util.List;
              
              import javax.ejb.Remove;
              import javax.ejb.Stateful;
              import javax.faces.context.ExternalContext;
              import javax.faces.context.FacesContext;
              import javax.persistence.EntityManager;
              import javax.servlet.http.HttpSession;
              
              import org.hibernate.Session;
              import org.hibernate.connection.ConnectionProvider;
              import org.hibernate.engine.SessionFactoryImplementor;
              import org.jboss.seam.ScopeType;
              import org.jboss.seam.annotations.Destroy;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Logger;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Scope;
              import org.jboss.seam.log.Log;
              
              import com.cox.bets.entity.ApplicationMetaData;
              import com.cox.bets.utils.CoxConstants;
              import com.cox.bets.utils.CoxProperties;
              
              
              @Stateful
              @Scope(ScopeType.SESSION)
              @Name("applicationMetaData")
              public class ApplicationMetaDataAction implements ApplicationMetaDataLocal {
                   
                   @In 
                   EntityManager entityManager;
                   
                   @Logger
                   Log log;
                        
                   private String dbVersion, dbVendor, userName, url, jdbcDriver, jdbcVersion, useApplicationMetaData;
                   
                   private List<ApplicationMetaData> applicationMetaDataList;
                   
                   private ApplicationMetaData applicationMetaData;
                   
                   private InetAddress inetAddress;
                   
                   private Boolean isDebug;
                   
                   /*-----------------------------------------------------------BEGIN METHODS-------------------------------------------------------------*/
                   
                   public String getApplicationShortName()
                   {
                        useApplicationMetaData = CoxProperties.getPropertyObject().getProperty(CoxConstants.USE_APPLICATION_METADATA);
                        if (useApplicationMetaData != null && useApplicationMetaData.equalsIgnoreCase("true"))
                        {
                             String applicationName = CoxConstants.APPLICATION_NAME;
                             
                             applicationMetaDataList = entityManager.createQuery("select a "+
                                                                                              " from ApplicationMetaData a "+
                                                                                              " where a.applicationName = :applicationName ")
                                                                                              .setParameter("applicationName", applicationName)
                                                                                              .getResultList();
                                                                
                             if (applicationMetaDataList != null && applicationMetaDataList.size() > 0)
                             {
                                  applicationMetaData = applicationMetaDataList.get(0);
                                  return applicationMetaData.getApplicationShortName();
                             }               
                        }
                        return "";
                   }
              
                   public String getBuildNumber() 
                   {     
                        if (applicationMetaData  != null)
                             return applicationMetaData.getBuildNumber();
                        else          
                             return CoxProperties.getPropertyObject().getProperty(CoxConstants.BUILD_NUMBER);
                   }
                   
                   public String getTemplateVersion()
                   {
                        return CoxProperties.getPropertyObject().getProperty(CoxConstants.TEMPLATE_VERSION);
                   }
                   
                   public void getDatabaseDetails()
                   {
                        
                        Session session = (Session)entityManager.getDelegate();
                        SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor)session.getSessionFactory();
                        ConnectionProvider connectionProvider = sessionFactoryImplementor.getConnectionProvider();
                        Connection connection = null;
                                  
                        try {
                             connection = connectionProvider.getConnection();               
                             DatabaseMetaData databaseMetaData = connection.getMetaData();
                                            
                             dbVendor = databaseMetaData.getDatabaseProductName();
                             dbVersion = databaseMetaData.getDatabaseProductVersion();
                             userName = databaseMetaData.getUserName();
                             url = databaseMetaData.getURL();
                             jdbcDriver = databaseMetaData.getDriverName();
                             jdbcVersion = databaseMetaData.getDriverVersion();               
                             
                        }
                        catch (SQLException sqle)
                        {
                             log.error(sqle);
                        }
                        finally
                        {
                             try
                             {
                                  connectionProvider.closeConnection(connection);
                             }
                             catch (SQLException sqle)
                             {
                                  log.error(sqle);
                             }
                        }          
                   }
                   
                   public String getSessionId()
                   {               
                        FacesContext facesContext = FacesContext.getCurrentInstance();
                        ExternalContext externalContext = facesContext.getExternalContext();
                        HttpSession session = (HttpSession)externalContext.getSession(true); 
                        String id = session.getId();
                        
                        log.info(this.getClass().getName()+": Session Id is : " + id);
                        
                        return id;
                   }
                   
                   public String getServerName()
                   {
                        initServerInfo();
                        return inetAddress.getHostName();
                   }
                   
                   public String getServerAddress()
                   {
                        initServerInfo();
                        return inetAddress.getHostAddress();
                   }
                        
                   private void initServerInfo()
                   {
                        if (inetAddress == null)
                        {
                             Enumeration<NetworkInterface> e = null;
                             
                             try 
                             {
                                  e = NetworkInterface.getNetworkInterfaces();
                             }
                             catch (SocketException se)
                             {
                                  log.error("SocketException occurred: ", se);
                             }
                             while(e.hasMoreElements()) 
                             {
                                  NetworkInterface ni = e.nextElement();
                                  Enumeration<InetAddress> e2 = ni.getInetAddresses();
                                  while (e2.hasMoreElements())
                                  {
                                       inetAddress = e2.nextElement();
                                       log.info("inetAddress.getHostAddress(): " + inetAddress.getHostAddress());
                                       log.info("inetAddress.getHostName(): " + inetAddress.getHostName());                    
                                  }
                                  
                             }
                        }
                   }
                   
                   private String getServerInfoName(String serverInfo) {
                       int slash = serverInfo.indexOf('/');
                       if (slash == -1) return serverInfo;
                       else return serverInfo.substring(0, slash);
                   }
                   
                   private String getServerInfoVersion(String serverInfo) {
                       int slash = serverInfo.indexOf('/');
                       if (slash == -1) return null;
                       else return serverInfo.substring(slash + 1);
                   }
                   
                   public Boolean getIsDebug()
                   {
                        isDebug = org.jboss.seam.core.Init.instance().isDebug();
                        return isDebug;
                   }
                   
                   public void setDatabaseVendor(String dbVendor)
                   {
                        this.dbVendor = dbVendor;
                   }
                   public String getDatabaseVendor()
                   {
                        return dbVendor;
                   }
                   
                   public void setDatabaseVersion(String dbVersion)
                   {
                        this.dbVersion = dbVersion;
                   }
                   public String getDatabaseVersion()
                   {
                        return dbVersion;
                   }
                   
                   public void setDatasourceUsername(String userName)
                   {
                        this.userName = userName;
                   }
                   public String getDatasourceUsername()
                   {
                        return userName;
                   }
                   
                   public void setURL(String url)
                   {
                        this.url = url;
                   }
                   public String getURL()
                   {
                        return url;
                   }
                   
                   public void setJDBCDriver(String jdbcDriver)
                   {
                        this.jdbcDriver =jdbcDriver; 
                   }
                   public String getJDBCDriver()
                   {
                        return jdbcDriver;
                   }
                   
                   public void setJDBCVersion(String jdbcVersion)
                   {
                        this.jdbcVersion = jdbcVersion;
                   }
                   public String getJDBCVersion()
                   {
                        return jdbcVersion;
                   }
                   
                   @Destroy @Remove
                   public void destroy(){}
              
              }