代码之家  ›  专栏  ›  技术社区  ›  newbie

在谷歌应用引擎上使用log4j

  •  4
  • newbie  · 技术社区  · 16 年前

    我需要在应用程序中使用log4j,但我不知道如何加载属性。deafult属性文件说我应该把log4j.properties放到/web-inf/classes/folder中,但是在Eclipse中,我看不到这个文件夹,也无法创建它,因为它已经存在了。我也不能向那个文件夹添加任何文件。

    以下是我得到的错误:

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

    那么,如何让Web应用程序加载log4j属性呢?

    5 回复  |  直到 16 年前
        1
  •  6
  •   dgeske    16 年前
        2
  •  5
  •   OhadR    12 年前

    Eclipse with the Google plugin

    <system-properties> 
      <property name="java.util.logging.config.file" value="WEB-INF/classes/log4j.properties"/> 
    </system-properties> 
    

    import org.apache.log4j.Logger; 
    ... 
    Logger logger = Logger.getLogger("com.foo"); 
    logger.debug("Yay2!"); 
    

    log4j.rootLogger=DEBUG, stdout 
    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 %t %c{1}:%M:%L - %m%n 
    

    com.google.appengine.archetypes:guestbook-archetype

    <dependency> 
    <groupId>log4j</groupId> 
    <artifactId>log4j</artifactId> 
    <version>1.2.16</version> 
    </dependency> 
    

    <%@ page import="org.apache.log4j.Logger" %> 
    ... 
    <% 
    Logger logger = Logger.getLogger("com.foo"); 
    logger.debug("Yay2!"); 
    %> 
    

    mvn clean 
    mvn verify 
    mvn appengine:devserver 
    

    http://localhost:8080/

        3
  •  1
  •   Rafael Sanches    13 年前

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener> 
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>
    

        4
  •  0
  •   Rafael Sanches    13 年前

    public class CustomXmlWebApplicationContext extends XmlWebApplicationContext {
    @Override
    protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
        super.initBeanDefinitionReader(beanDefinitionReader);
    
        try {
            Resource res = beanDefinitionReader.getResourceLoader().getResource("classpath:log4j.properties");
            Properties props = new Properties();
            props.load(res.getInputStream());
            PropertyConfigurator.configure(props);
        }
        catch(Throwable e) {
            e.printStackTrace();
        }
    }
    

        5
  •  0
  •   naXa stands with Ukraine    11 年前

    Logging in the Google AppEngine for Java (GAE/J) with Slf4j, Log4j and JUL

    \src\main\webapp\WEB-INF\appengine-web.xml

    <!-- Configure java.util.logging -->
    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/java-util-logging.properties"/>
    </system-properties>
    

    \src\main\webapp\WEB-INF\java-util-logging.properties

    .level = ALL
    

    \src\main\resources\log4j.properties

    log4j.rootLogger=ALL, stdout
    # or a lower log level such as DEBUG, INFO or WARN. 
    
    # Define the destination and format of our logging
    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=%-5p: %m  at %C.(%F:%L) on %d{ISO8601}%n
    

    Gradle

    configurations {
        all*.exclude group: "commons-logging", module: "commons-logging"
    }
    
    dependencies {
        // Logging
        compile 'org.slf4j:slf4j-api:1.7.+'
        runtime 'org.slf4j:slf4j-jdk14:1.7.+'
        runtime ('log4j:log4j:1.2.17') {
            exclude group: "com.sun.jdmk", module: "jmxtools"
            exclude group: "com.sun.jmx", module: "jmxri"
            exclude group: "javax.mail", module: "mail"
            exclude group: "javax.jms", module: "jms"
        }    
    }
    

    <!-- The definition of the Log4j Configuration -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>