I have a EJB Module using Netbeans and glassfish 3.1.2. I need to implement log but I don't know how to configure log4j in a EJB. I'm having this problem when deploying:
SEVERE: log4j:ERROR Ignoring configuration file [file:/C:/Users/Luis Carlos/AppData/Roaming/NetBeans/7.2/config/GF3/domain1/config/log4j.properties].
SEVERE: log4j:WARN No appenders could be found for logger (beans.RutasBean).
SEVERE: log4j:WARN Please initialize the log4j system properly.
SEVERE: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
It is shown in the glassfish server console.
This is the log4j.properties file:
log4j.rootLogger=DEBUG, FILE
log4j.logger.myapp=DEBUG
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=/space/gfv3/v3setup/GlassFish3/GlassFish/domains/domain1/logs/log4j.log
log4j.appender.FILE.MaxFileSize=100KB
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{DATE} %-5p %c{1} : %m%n
I tried
this
and it says that I have to configure the properties file like this in the server admin of glassfish
-Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/config/log4j.properties
but didn't work
I have a Singleton Session Bean and in the startup method I configure the log, so according to this I have in the bean:
private Logger log;
private static final String LOG4J_PROPERTIES = "/log4j.properties";
@PostConstruct
public void startup(){
URL url = this.getClass().getResource(LOG4J_PROPERTIES);
if(url == null || url.getFile().length() == 0) {
throw new RuntimeException("Log4j config file "+ LOG4J_PROPERTIES +" not found");
}
PropertyConfigurator.configure(url);
log = Logger.getLogger(this.getClass());
log.info("Starting application");
em = Persistence.createEntityManagerFactory("BuscaBus-ejbPU");
rutaController = new RutaJpaController(ut, em);
actualizarCoordenadasRutas();
...
}
but still appears the same error
How can I configure log4j in a EJB enviroment? Thanks
Do the following:
-Dlog4j.configuration=
from your server.log4j.properties
that you might have placed into C:/Users/Luis Carlos/AppData/Roaming/NetBeans/7.2/config/GF3/domain1/config
Remove the following code:
URL url = this.getClass().getResource(LOG4J_PROPERTIES);
if(url == null || url.getFile().length() == 0) {
throw new RuntimeException("Log4j config file "+ LOG4J_PROPERTIES +" not found");
Remove this: PropertyConfigurator.configure(url);
Once you're done with the aforementioned steps, your code is in the position to have Log4J automatically load its log4j.properties
file from the classpath. From my experience, that's the most flexible way to go about configuring Log4J. All that's left for you to do is to ensure that log4j.properties
indeed exists somewhere in the classpath.
You have the following options:
log4j.properties
file inside the default package of your EJB module. That way, the log4j.properties
file will be packaged along with your application. There are pros and cons to this, but that's a different story (which belongs, most likely, in a different Q&A thread).log4j.properties
in some directory on your file system, and add that directory to your server's classpath. Follow the GlassFish documentation about how to do that.If you insist to have the log4j.properties
file referred-to by a URL, then reinstate the -Dlog4j.configuration=
directive. Make sure that the URL you provide, specifying the log4j.properties
file, actually exists. Also, specify -Dlog4j.debug=true
; that will provide you with debugging information from Log4J itself, telling you exactly what it is doing.