Search code examples
javaweb-servicesloggingjbossejb

Writing duplicate data when using Log4j in Jboss webservice


I am using Log4j with Jboss EJB webservice. I am logging the application flow in the File.

This is the code I am using for logging

 FileAppender fileappender;

    File file = new File("jws_" + getCurrentDate("dd_MMM_yyyy") + ".log");

            Logger log = null;
    System.out.println(file.getAbsolutePath() );
    try
    {
        log = Logger.getLogger(ConnMan.class);
        fileappender = new FileAppender(new PatternLayout(),"f2cjws_" + getCurrentDate("dd_MMM_yyyy") + ".log");
        log.addAppender(fileappender);


                    if (!theLogLevel.equalsIgnoreCase("error"))
                    {
                        if ("yes".equalsIgnoreCase(getProperties().getProperty("log")))
                        {
                            log.debug(getCurrentDate("dd MMM yyyy HH:mm:ss 1")+" "+theError);
                        }
                    }
                    else
                    {
                        log.debug(getCurrentDate("dd MMM yyyy HH:mm:ss 1")+" "+theError);
                    }

    }
    catch (IOException e)
    {
        System.out.println("Logger failed due to "+e.getMessage());
    }
    catch(Exception e)
    {
        System.out.println("Logger failed due to "+e.getMessage());
    }

When I run the application, I am getting duplicate data in my file, i.e.., same data is written twice or thrice. The above code worked fine in webapplication deployed in tomcat.

So I feel I am missing something in related to JBoss.

This webservice now currently uses the log4j properties built in with the server. Can I know how to make it to use application's own properties file?

Please help me,

Thanks in advance,


Solution

  • You need to create your own log writer,

    public final class LogWriter
    {
    
        private static Logger appLogger = null;
    
        private static String className = LogWriter.class.getName() + ".";
    
        static
        {
            try
            {
                appLogger = Logger.getLogger("Demologer");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    
        public static void logDebug(String message)
        {
            appLogger.log(className, Level.DEBUG, LogWriter.getMessage(message), null);
        }
    
    
        public static void logInfo(String message)
        {
            appLogger.log(className, Level.INFO, LogWriter.getMessage(message), null);
        }
    
        public static void logError(String message)
        {
            appLogger.log(className, Level.ERROR, LogWriter.getMessage(message), null);
        }
    
        private static String getMessage(String message)
        {
            String retValue;
    
            Calendar cale = Calendar.getInstance();
            Date now = cale.getTime();
    
            //retValue=now.getDate()+"/"+(now.getMonth()+1)+"/"+(now.getYear()+1900)+"  "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
            retValue = now + "\n";
    
            now = null;
            cale = null;
    
            return retValue + message;
        }
    }
    

    You can use this one as you need.