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?
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.)
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