在eclipse中设置log4j以使用quartz工作

时间:2013-04-07 06:46:33

标签: java-ee log4j quartz-scheduler

这是log4j.properties目录中的WEB-INF/classes文件:

#SET LEVEL of ROOT-LOGGER, you will like to have Debug in local, but in prod you may just want WARN and ABOVE. This setting is done here!
log4j.rootLogger=all, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number. (basically, format of log)
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# THIS IS WHERE YOU WILL HAVE ALL THE LOG WRITTEN
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/Users/miranda_zhang/Documents/workspace/cloud_demo_3/log/speedtest.log

# Maximum size of log file, usually we keep 10MB
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file, usually we keep 10
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

在java类中我有类似的东西:

import org.quartz.Job;
import org.apache.log4j.Logger;

public class collectSpeedtestDataJob implements Job {

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

    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        try {

        } catch (Exception e) {
            logger.error(e);
        }

    }
}

但文件/Users/miranda_zhang/Documents/workspace/cloud_demo_3/log/speedtest.log

中没有记录任何内容

以下是控制台

中显示的消息
Apr 7, 2013 4:24:47 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: .:/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Apr 7, 2013 4:24:47 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:demo_3' did not find a matching property.
Apr 7, 2013 4:24:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Apr 7, 2013 4:24:47 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Apr 7, 2013 4:24:47 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1993 ms
Apr 7, 2013 4:24:47 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 7, 2013 4:24:47 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.19
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:02 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Apr 7, 2013 4:25:03 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  rest.handler
Apr 7, 2013 4:25:03 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
Apr 7, 2013 4:25:03 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Apr 7, 2013 4:25:03 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM'
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:06 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: urn:www.jenkov.com/projects/prizetags is already defined
Apr 7, 2013 4:25:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Apr 7, 2013 4:25:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Apr 7, 2013 4:25:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 19119 ms

我在Eclipse中添加了-Dlog4j.debug=true作为VM参数。

我已尝试以下方法进行调试:

public void execute(JobExecutionContext arg0) throws JobExecutionException {
    logger.info("Log - collectSpeedtestDataJob");
    System.out.println("info logged");

    try {

        DB.executeUpdate(speedtest());

    } catch (Exception e) {
        logger.error(e);
    }

}

控制台中,我可以看到正在打印,但日志文件中没有任何内容:

log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
info logged

1 个答案:

答案 0 :(得分:1)

好的,我已经添加了以下代码来解决问题:

PropertyConfigurator.configure("absolute/path/to/your/WebContent/WEB-INF/log4j.properties");

但仍想知道是否有办法自动检测文件。或者自动获取文件的路径。

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource("/WebContent/WEB-INF/lib/log4j.properties");

只给我null