简体   繁体   English

找出Spring Logging

[英]Figuring out Spring Logging

I've inherited a, well, messy, Spring MVC application. 我继承了一个混乱的Spring MVC应用程序。 Some things don't work and I want to enable DEBUG or even TRACE logging to get more information and found that even that is not that simple. 有些事情不起作用,我想启用DEBUG甚至TRACE日志记录以获取更多信息,发现即使那样也不是那么简单。

When starting the application (by running a Tomcat 8 server from Eclipse), it seems to output two kinds of log messages. 启动应用程序时(通过从Eclipse运行Tomcat 8服务器),似乎输出两种日志消息。 Here is an example of the first kind: 这是第一种示例:

Jun 20, 2017 1:47:07 PM org.hibernate.Version logVersion INFO:
HHH000412: Hibernate Core {4.3.8.Final} Jun 20, 2017 1:47:07 PM
org.hibernate.cfg.Environment <clinit> INFO: HHH000206:
hibernate.properties not found Jun 20, 2017 1:47:07 PM
org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021:
Bytecode provider name : javassist

These messages are all in red. 这些消息均为红色。 The second kind looks like this: 第二种看起来像这样:

13:47:16.949 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet:489 - FrameworkServlet 'spring': initialization started
13:47:16.952 [localhost-startStop-1] INFO  org.springframework.web.context.support.XmlWebApplicationContext:583 - Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Tue Jun 20 13:47:16 IDT 2017]; parent: Root WebApplicationContext
13:47:16.952 [localhost-startStop-1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader:317 - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]

These are all in black. 这些都是黑色的。

Obviously there are at least two active logging formats. 显然,至少有两种活动的日志记录格式。 This is were it gets weird: 这是它变得奇怪:

I found numerous log4j2.xml as well as log4j.properties files. 我发现了许多log4j2.xml以及log4j.properties文件。 All of them seem to be ignored. 他们似乎都被忽略了。 Changing them makes no difference. 更改它们没有区别。 I just deleted them all, and it did not affect the logging output. 我只是将它们全部删除,并且它没有影响日志记录的输出。 In addition, log4j 1.2, log4j 2.0.2 and slf4j are all Maven dependencies. 此外,log4j 1.2,log4j 2.0.2和slf4j都是Maven依赖项。 pom.xml references version 1.2, other Maven dependencies probably need the other versions. pom.xml引用了1.2版,其他Maven依赖项可能需要其他版本。

My question is - how do I configure the logging myself? 我的问题是-我如何配置日志记录? Placing a log4j.xml file under WEB-INF (were all other property files are located) doesn't make any difference. 将log4j.xml文件放在WEB-INF下(找到所有其他属性文件)没有任何区别。 Neither that placing a log4j.properties file there. 也没有在其中放置log4j.properties文件。 I need to somehow turn debug printouts of log4j itself, so I can figure out where it's reading it's configuration from. 我需要以某种方式打开log4j本身的调试打印输出,因此我可以弄清楚它是从哪里读取其配置的。

I tried adding -Dlog4j.debug as a VM argument to the Tomcat run configuarion. 我尝试将-Dlog4j.debug作为VM参数添加到Tomcat运行配置。 Other than printing -Dlog4j.debug command line argument or something similar, nothing changed. 除了打印-Dlog4j.debug command line argument或类似内容外,没有任何变化。

You can use following code snippet that I have used for my project. 您可以使用我在项目中使用过的以下代码段。 Over here class name is FolderReader.class and placed lo4j.properties file in folder "conf".Passing argument "../conf/log4j.properties" as args[2] argument when running the application. 此处的类名是FolderReader.class,并将lo4j.properties文件放置在文件夹“ conf”中。在运行应用程序时将参数“ ../conf/log4j.properties”作为args [2]参数传递。

final FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(args[0]);
    Logger logger=Logger.getLogger(FolderReader.class);
    PropertyConfigurator.configure(args[2]);
    logger.info("Intializing beans..");
    ServiceContext serviceContext = (ServiceContext) BeanFactoryLocator.getBean("serviceContext");

PropertyConfigurator.configure(args[2]);loads the log4j.properties file from location. PropertyConfigurator.configure(args [2]);从位置加载log4j.properties文件。 Configuration is taken from this property file and formatting is done accordingly. 从该属性文件进行配置,并相应地进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM