java - Spring 独立应用程序 |记录

标签 java spring log4j

我正在尝试将日志放入 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

最佳答案

日志仍在工作..在底部您可以看到

Exiting the program 
Exiting the program1

只有来自 Spring 框架的附加日志。当 spring 容器初始化时,它将记录自己的信息,如您在此处看到的。

我实际上看不出这里有什么问题。

<小时/>

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

log4j.logger.org.springframework=WARN

应该只显示警告和错误。

我也会将您的 conversionPattern 更改为类似以下内容:%d{ABSOLUTE} %5p %c{1}:%L - %m%n

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

关于java - Spring 独立应用程序 |记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21091937/

相关文章:

java - @TestPropertySource 不适用于 Spring 1.2.6 中使用 AnnotationConfigContextLoader 的 JUnit 测试

java - 如何映射具有接口(interface)和类的方法——从操作到委托(delegate)

java - @Secured 和 @PreAuthorize 在 Controller 中工作正常,但在服务级别中不起作用

java - CrudRepository 不从 API 返回数据库元素

java - Tomcat 日志显示错误的日期

java - 不需要的 JDBC 消息会忽略 log4j 配置

java - java 中的可变参数和泛型

Java 在已启动的线程上运行更多代码

java - 使用 Spring Data JPA 更新实体的正确方法是什么?

java - Websphere 所有日志都将转到 SystemOut.log