0

我正在使用 Glassfish,并创建了一个名为 log4j 的 JNDI 自定义资源,它具有以下属性:

Name: log4j.appender.LOG Value: org.apache.log4j.RollingFileAppender
Name: log4j.appender.LOG.layout Value: org.apache.log4j.PatternLayout
Name: log4j.rootLogger Value: WARN, LOG
Name: log4j.appender.LOG.layout.conversionPattern Value: %d{MMM dd, yyyy HH:mm:ss} %p %m%n
Name: log4j.appender.LOG.File Value: ${com.sun.aas.instanceRoot}/logs/RestWebServices.log
Name: log4j.appender.LOG.MaxFileSize Value: 10MB
Name: log4j.appender.LOG.MaxBackupIndex Value: 10

JNDI 资源类型是 java.util.Properties 并且我的 spring 应用程序上下文包含这些行以找到它们:

<jee:jndi-lookup id="log4jJndi" jndi-name="log4j" resource-ref="true" />
<jee:jndi-lookup id="configurationsJndi" jndi-name="configurations" resource-ref="true" />

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="propertiesArray">
        <list>
            <ref bean="configurationsJndi"/>
            <ref bean="log4jJndi"/>
        </list>
    </property>
</bean>

它适用于我的配置 JNDI 资源,但 log4j 在启动 Glassfish 时给了我这个错误:

SEVERE: log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).  
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.

调试时根记录器不是我在 JNDI 资源中设置的。我尝试添加 spring Log4jConfigListener 来解决这个问题,但它没有用。我使用的 Glassfish 版本是 3.1。

有谁知道为什么 log4j 是一个问题?

编辑:现在我尝试使用具有文件位置的 JNDI 到我的 log4j 属性文件,并且我的 Spring 应用程序上下文从 JNDI 获取值。我的问题是我一直在使用 log4j 时遇到此错误:

Exception while loading the app : java.lang.IllegalStateException:  
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'log4jJndi': Invocation of init method failed; 
nested exception is javax.naming.CommunicationException: Communication exception for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is java.lang.IllegalAccessException: value cannot be null]|#]
4

1 回答 1

0

尝试以下操作,看看是否有帮助:

1-创建一个名为 log4j.properties 的文件并将其放在 /src/main/resources

2-复制该文件中的以下行:

# #Default log level
log4j.rootCategory=INFO, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d [%t] %c: %m%n

################################################
# #You can set custom log levels per-package here
################################################

# #Apache Commons tend to make a lot of noise which can clutter the log.
log4j.logger.org.apache=WARN

# #Shuts up some innocuous messages if using the JBPM transport
log4j.logger.org.hibernate.engine.StatefulPersistenceContext.ProxyWarnLog=ERROR

# #Reduce startup noise
log4j.logger.org.springframework.beans.factory=WARN

# #Your custom classes if you have one
log4j.logger.com.mycompany=DEBUG
于 2012-12-19T14:33:34.367 回答