问题
I'm working on my java Enterprise application in ejb module with Netbeans.
When I run "MyClass.java" I only view error level.
MyEnterpriseApplication-ejb:Source Packages:
package com.mycompany;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MyClass {
public static final Logger logger = LogManager.getLogger(MyClass.class);
public static void main(String[] arg) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:context.xml");
logger.info("level info");
logger.error("level error");
logger.debug("level debug");
logger.warn("level warn");
}
}
When I run this class...
Output:
------------------------------------------------------------------------
Building MyEnterpriseApplication-ejb 1.0-SNAPSHOT
------------------------------------------------------------------------
[dependency:copy]
[resources:resources]
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 1 resource
[compiler:compile]
Compiling 1 source file to Z:\NetBeansProjects\MyEnterpriseApplication\MyEnterpriseApplication-ejb\target\classes
[exec:exec]
20:26:23.885 [main] ERROR com.mycompany.MyClass - level error
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 2.328s
Finished at: Mon May 27 20:26:23 CEST 2013
Final Memory: 14M/162M
------------------------------------------------------------------------
How can I add a log4j.xml file and add to spring context file? this is the way?
回答1:
With log4j version2.0, you don't need to call DomConfigurator. (I don't think it exists any more.) The config file should be called log4j2.xml and will be picked up if it is in the classpath.
Your maven dependencies look correct to me.
Your example MyClass code also looks fine. (Although you don't need the ApplicationContext for the logging.)
回答2:
you need to tell the application to load the xml configuration you want to use:
in this sample the file must be located on the classpath and should be called log4j.xml
package javabeat.net.log4j;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class YourClass {
private static Logger logger = Logger.getLogger
(Log4jXmlTest.class);
public static void main (String args[]){
DOMConfigurator.configure("log4j.xml");
logger.info("Test Log");
logger.error("level error");
logger.debug("level debug");
logger.warn("level warn");
}
}
来源:https://stackoverflow.com/questions/16778986/log4j-only-can-view-error-level-on-my-java-enterprise-application-module-ejb-wi