2013-04-07 112 views
0

這是我log4j.properties文件WEB-INF/classes目錄:設置log4j的石英就業工作

#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 
+0

你看到日誌在控制檯?這個班是否初始化?您正在這裏打印catch塊中的日誌。代碼是否到達那裏?或者,您可以嘗試在try塊本身中打印它,並查看它是否出現在控制檯中。你的設置似乎很好,但。 – 2013-04-07 07:23:39

回答

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