Spring Standalone Apps |记录

时间:2014-01-13 12:58:55

标签: java spring log4j

我正在尝试将log登录到spring独立应用程序。这是我的代码

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;


@Component
public class Main {
    static Logger log = Logger.getLogger(Main.class.getName());
    public String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public static void main(String[] args) {

       // ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        log.debug("Going to create HelloWord Obj");
        //Main p = (Main) context.getBean("hello");
        //System.out.println(p.getId());
        log.debug("Exiting the program");
        log.debug("Exiting the program1");


    }


}

我的log4j.properties是

log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=C://log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

在上面的代码日志工作正常。当我从主类删除注释时,它不工作。它在log.out文件中抛出一些额外的信息,如下所示。让我知道为什么它会像这样抛出..?

Adding [systemProperties] PropertySource with lowest search precedence
Adding [systemEnvironment] PropertySource with lowest search precedence
Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
Initializing new StandardEnvironment
Adding [systemProperties] PropertySource with lowest search precedence
Adding [systemEnvironment] PropertySource with lowest search precedence
Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
Initializing new StandardEnvironment
Adding [systemProperties] PropertySource with lowest search precedence
Adding [systemEnvironment] PropertySource with lowest search precedence
Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
Loading schema mappings from [META-INF/spring.schemas]
Loaded schema mappings: {http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd}
Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
Found XML schema [http://www.springframework.org/schema/context/spring-context-3.0.xsd] in classpath: org/springframework/context/config/spring-context-3.0.xsd
Found XML schema [http://www.springframework.org/schema/tool/spring-tool-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-tool-3.0.xsd
Loading bean definitions
Loaded NamespaceHandler mappings: {http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler, http://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler, http://www.springframework.org/schema/c=org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler, http://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler, http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler, http://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler, http://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandler, http://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler}
Initializing new StandardEnvironment
Adding [systemProperties] PropertySource with lowest search precedence
Adding [systemEnvironment] PropertySource with lowest search precedence
Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
Looking for matching resources in directory tree [E:\eclipse-workspace\composer_projects\Spring_Standalone\bin\com\controller]
Searching directory [E:\eclipse-workspace\composer_projects\Spring_Standalone\bin\com\controller] for files matching pattern [E:/eclipse-workspace/composer_projects/Spring_Standalone/bin/com/controller/**/*.class]
Resolved location pattern [classpath*:com/controller/**/*.class] to resources [file [E:\eclipse-workspace\composer_projects\Spring_Standalone\bin\com\controller\Main.class]]
Identified candidate component class: file [E:\eclipse-workspace\composer_projects\Spring_Standalone\bin\com\controller\Main.class]
Loaded 6 bean definitions from location pattern [spring.xml]
Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@27cd63: org.springframework.beans.factory.support.DefaultListableBeanFactory@1871f70: defining beans [main,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,hello]; root of factory hierarchy
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor' to allow for resolving potential circular references
Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@653c7e]
Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@1588db6]
Creating shared instance of singleton bean 'main'
Creating instance of bean 'main'
Eagerly caching bean 'main' to allow for resolving potential circular references
Finished creating instance of bean 'main'
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
Creating shared instance of singleton bean 'hello'
Creating instance of bean 'hello'
Eagerly caching bean 'hello' to allow for resolving potential circular references
Finished creating instance of bean 'hello'
Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@10ddb86]
Returning cached instance of singleton bean 'lifecycleProcessor'
Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
Going to create HelloWord Obj
Returning cached instance of singleton bean 'hello'
Exiting the program
Exiting the program1

1 个答案:

答案 0 :(得分:1)

日志仍然有效..在底部你可以看到

Exiting the program 
Exiting the program1

只有Spring框架的附加日志。初始化弹簧容器时,它将记录自己的信息,如您所见。

我实际上看不到任何问题。


如果要删除spring日志记录,可以在log4j.properties中指定特定包的日志记录级别:

log4j.logger.org.springframework=WARN

只应显示警告和错误。

我还会将您的conversionPattern更改为:%d{ABSOLUTE} %5p %c{1}:%L - %m%n

这将提供更多信息,您将能够看到哪些包正在记录什么