在Jboss webservice中使用Log4j时写入重复数据

时间:2010-12-15 05:19:53

标签: java web-services logging jboss ejb

我正在使用Log4j和Jboss EJB webservice。我正在文件中记录应用程序流。

这是我用于记录的代码

 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());
    }

当我运行应用程序时,我在我的文件中得到重复的数据,即..,相同的数据被写入两次或三次。上面的代码在tomcat中部署的webapplication中运行良好。

所以我觉得我缺少与JBoss有关的东西。

此Web服务现在使用服务器内置的log4j属性。我可以知道如何使用应用程序自己的属性文件吗?

请帮助我,

提前致谢,

2 个答案:

答案 0 :(得分:2)

您需要创建自己的日志编写器

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;
    }
}

您可以根据需要使用此功能。

答案 1 :(得分:1)

如果您在log4j.properties中有类似的内容,则会在输出中看到重复:

log4j.rootLogger=error, myappender
log4j.logger.com.sample=warn, myappender 
log4j.logger.com.sample.deeper.package=all, myappender 

更正:

log4j.rootLogger=error, myappender
log4j.logger.com.sample=warn 
log4j.logger.com.sample.deeper.package=all

编辑:

以下内容将从com.sample。*类中的每个类,致命错误和警告级别日志以及com.sample.deeper.package。*类中的每个日志写入致命和错误级别日志到/ var / log / myappender /myappender.log文件。

在root package中保存为log4j.properties:

# This appender is not used in this example.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c - %m%n

# But this one is used.
log4j.appender.myappender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappender.DatePattern='.'yyyy-MM-dd
log4j.appender.myappender.File=/var/log/myappender/myappender.log
log4j.appender.myappender.layout=org.apache.log4j.PatternLayout
log4j.appender.myappender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c - %m%n
log4j.appender.myappender.Encoding=UTF-8

log4j.rootLogger=error, myappender
log4j.logger.com.sample=warn 
log4j.logger.com.sample.deeper.package=all

在每个课程中,您需要一个记录器:

    private static Logger logger = Logger.getLogger(ThisClassName.class);

并且您不再需要您提出的代码段。