PropertyConfigurator.configure多个log4j文件

时间:2016-06-10 09:39:58

标签: java servlets logging log4j servlet-filters

我有两个Web应用程序(Proj1和Proj2)。

log4j.properties文件位于外部位置(Say C:\ proj1_log4j.properties和C:\ proj2_log4j.properties)

上述两个用于初始化log4j的Web应用程序都有Servlet过滤器。代码是在两个过滤器的init()方法中编写的。

log4j初始化如下 for Proj1 (filename = Proj1Filter.java)

public void init(FilterConfig arg0) throws ServletException {
    final String LOG_CONFIG_FILE = "C:/proj1_log4j.properties";
    File loggingPropFile = new File(LOG_CONFIG_FILE);
    FileInputStream fis;
    try {
        fis = new FileInputStream(loggingPropFile);
        Properties properties = new Properties();
        properties.load(fis);
        PropertyConfigurator.configure(properties);
        LOGGER.info("Logger 111 initialiZed successfully...");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

log4j初始化如下 for Proj2 (filename = Proj2Filter.java)

public void init(FilterConfig arg0) throws ServletException {
    final String LOG_CONFIG_FILE = "C:/proj2_log4j.properties";
    File loggingPropFile = new File(LOG_CONFIG_FILE);
    FileInputStream fis;
    try {
        fis = new FileInputStream(loggingPropFile);
        Properties properties = new Properties();
        properties.load(fis);
        PropertyConfigurator.configure(properties);
        LOGGER.info("Logger 222 initialized successfully...");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

proj1_log4j.properties

log4j.rootLogger=INFO, stdout1, proj1

log4j.appender.stdout1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout1.Target=System.out
log4j.appender.stdout1.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.proj1=org.apache.log4j.RollingFileAppender
log4j.appender.proj1.File=proj1.log
log4j.appender.proj1.MaxFileSize=10MB
log4j.appender.proj1.MaxBackupIndex=10
log4j.appender.proj1.layout=org.apache.log4j.PatternLayout
log4j.appender.proj1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

proj2_log4j.properties

log4j.rootLogger=INFO, stdout1, proj2

log4j.appender.stdout1=org.apache.log4j.ConsoleAppender
log4j.appender.stdout1.Target=System.out
log4j.appender.stdout1.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.proj2=org.apache.log4j.RollingFileAppender
log4j.appender.proj2.File=proj2.log
log4j.appender.proj2.MaxFileSize=10MB
log4j.appender.proj2.MaxBackupIndex=10
log4j.appender.proj2.layout=org.apache.log4j.PatternLayout
log4j.appender.proj2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

现在启动tomcat时,只创建了proj1.log。 proj2.log没有被创建。 所有日志都将转到proj1.log。 甚至来自proj2的记录器语句也是在proj1.log文件中打印日志。

可能是什么原因。

我想要的是,应该创建2个日志文件,proj1.log和proj2.log。

1 个答案:

答案 0 :(得分:0)

您将两个appenders $user = new Users (); $user->setName($strUserName); $user->setAddress1($strAddress1); $user->setAddress2($strAddress2); $user->setCity($strCity); $user->setState($strState); $user->setZip($strZip); $user->setContactSales($strContactSales); $user->setContactService($strContactService); $user->setContactParts($strContactParts); $user->setWebsite($strWebsite); $em = $this->getDoctrine()->getManager(); $em->persist($user); $userId = $user->getId(); $userWorkingHours = new UserWorkingHours(); $userWorkingHours->setDay = '"' . $day . '"'; $userWorkingHours->setStartDay = '"' . $startDay . '"'; $userWorkingHours->setEndDay = '"' . $endDay . '"'; $userWorkingHours->setLunchHourStart = '"' . $lunchStart . '"'; $userWorkingHours->setLunchHourEnd = '"' . $lunchEnd . '"'; $userWorkingHours->setUserId = '"' . $userId . '"'; $userWorkingHours->setIsActive = '"' . $isActive . '", '; $userWorkingHours->setCreatedOn = '"' . time() . '"'; $userWorkingHours->setType = 1; $em->persist($userWorkingHours); $em->flush(); 保存在一个文件中以及以下更改。

路由项目特定记录

proj1 & proj2

并避免重复日志设置可加性。

log4j.logger.com.<project1_root_package> =DEBUG, proj1
log4j.logger.com.<project2_root_pacakge> =DEBUG, proj2

最后你的根记录器会变成类似

的东西
log4j.additivity.com.<project1_root_package>=false
log4j.additivity.com.<project2_root_pacakge>=false