Announcement Announcement Module
Collapse
No announcement yet.
Logging errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Logging errors

    I'm attempting to do a very basic config that allows a customizable logger to be included in the configuration of the app. I built a skeleton of my daemon and a skeleton of the logging bean that will consume the custom logger (at the moment Log4j). At present, there is no reference to the actual logging component EXCEPT within the classpath, simply structural support framework exists. However, when I run the code, I get a logging warning:

    log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
    log4j:WARN Please initialize the log4j system properly.
    When I remove log4j-1.2.13.jar from the classpath, I don't get the warning and it starts ok. However, as soon as I start trying to reference anything in the Log4j package (NetBeans sees the Log4j JAR and can compile the Java files) I get the following error (because it's no longer in the run-time classpath):

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: Error registering bean with name 'loggingBean' defined in file [/<sompath>/./conf/logging.xml]: Class that bean class [foo.bar.LoggingBean] depends on not found; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    The following is my code:

    Main.java
    Note: this is trimmed down and doesn't include the daemonizing code
    Code:
    package foo.bar;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.aop.ThrowsAdvice;
    
    public class Main implements ThrowsAdvice {
      
        static LoggingBean logBean;
        
        public Main() {
        }
    
        public static void main(String[] args) {
        
            BeanFactory factory = new XmlBeanFactory(new FileSystemResource("./conf/logging.xml"));
            logBean = (LoggingBean)factory.getBean("loggingBean");
           
        }
    
        public void afterThrowing(Exception ex) throws Throwable { }
        
        public void afterThrowing(Method method, Object[] args, Object target, IllegalArgumentException ex) throws Throwable{ }  
    }
    LoggingBean.java
    Note: I'm not referencing any logging components yet, this is just framework
    Code:
    package foo.bar;
    
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.InitializingBean;
    
    public class LoggingBean implements BeanNameAware, InitializingBean {
        
        private String beanName = null;
        private String logConfigFile;
    
        public void setBeanName(String beanName)
        {
            this.beanName = beanName;
        }
    
        public void afterPropertiesSet() throws Exception
        {
            if( !"".equalsIgnoreCase(logConfigFile) && null != logConfigFile ){
                // Do some work
            }
            else{
                throw new IllegalArgumentException("You must specify the logConfigFile property of " + LoggingBean.class);
            }
        }
    
        public void setLogConfigFile(String logConfigFile) {
            this.logConfigFile = logConfigFile;
        }
    }
    logging.xml
    Note: ./conf/log4j.xml exists, but again, no component has been included in LoggingBean.java to actually parse and implement the configuration it contains.
    Code:
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
        
        <bean id="loggingBean" class="foo.bar.LoggingBean">
            <property name="logConfigFile">
                <value>./conf/log4j.xml</value>
            </property>
        </bean>
        
    </beans>
    Log4j.xml (adding this just because I don't want to miss anything)
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "dtd/log4j.dtd">
    
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
            
      <appender name="R" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="daemon.log" />
        <param name="MaxFileSize" value="100KB" />
        <param name="MaxBackupIndex" value="1" />
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%-4r [%t] (%F:%L) %-5p %c %x - %m%n" />
        </layout>
      </appender>
    
      <root>
        <level value ="debug" />
        <appender-ref ref="R"/>
      </root>
    
    </log4j:configuration>
    My execution is:
    Note: I've excluded the daemonizing bits:
    Code:
    java -jar ./foobar.jar -cp ./lib/spring.jar:./lib/log4j-1.2.13.jar:./conf
    Any ideas how I can allow Log4j (and, therefore, other logging components) to be injected into my app without getting this warning? Maybe I just have the setup wrong. Any guidance would be greatly appreciated. Thanks!

  • #2
    Log4J in Netbeans (log4j.xml)

    From what I can tell, when Netbeans builds a project, it does not copy over XML files from the source directories to the build dir. If you were to use log4j.properties, it would probably work. Because the log4j.xml is not copied, it is essentially ignored by the application at runtime and will result in this error.

    I just noticed this today, so I do not have a workaround yet.

    The best temporary solution would probably be to edit the build_impl.xml template that goes with projects to make sure it includes XML files, and not just property files. However, this is something that may not stick through netbeans upgrades, etc.

    I believe this affects all types of projects in Netbeans, anything that uses Log4j and log4j.xml.

    Comment


    • #3
      Re: log4J in Netbeans (log4j.xml)

      After moving my source directory order around, and looking at many options, cleaning the project and taking guesses it seems to have fixed itself. I am using netbeans 5.5b. I could see no reason why log4j.xml would be ignored, since the excludes list for building (for the jar file anyway) only excludes .java and .form files.

      Trial and error - just make sure log4j.xml is in your classpath.

      Originally posted by jalex
      From what I can tell, when Netbeans builds a project, it does not copy over XML files from the source directories to the build dir. If you were to use log4j.properties, it would probably work. Because the log4j.xml is not copied, it is essentially ignored by the application at runtime and will result in this error.

      I just noticed this today, so I do not have a workaround yet.

      The best temporary solution would probably be to edit the build_impl.xml template that goes with projects to make sure it includes XML files, and not just property files. However, this is something that may not stick through netbeans upgrades, etc.

      I believe this affects all types of projects in Netbeans, anything that uses Log4j and log4j.xml.

      Comment

      Working...
      X