简体   繁体   English

Spring日志和application.properties

[英]Spring logging and application.properties

the following question may seem stupid to many, since it should have a simple solution, but I am a beginner in the Spring framework, and I have been searching and testing many methods with no success. 以下问题对于许多人来说似乎很愚蠢,因为它应该有一个简单的解决方案,但是我是Spring框架的初学者,并且我一直在搜索和测试许多方法,但均未成功。

So, I need to implement logging for a Web Service based on Spring. 因此,我需要为基于Spring的Web服务实现日志记录。 Maven is used for dependencies, with the spring-boot-starter-ws dependency, and not the whole starter-web starter. Maven用于依赖关系,具有spring-boot-starter-ws依赖关系,而不是整个starter-web starter。

I tried with log4j, by adding a dependency and config file, and it does produce a log file, but only for the initialization of the logger itself, and no logging about when the service is used, although log4j is in TRACE level. 我通过添加依赖项和配置文件来尝试使用log4j,它确实生成了一个日志文件,但仅用于记录器本身的初始化,尽管log4j处于TRACE级别,但是没有记录何时使用该服务。 I also tried with logback, by adding the logback.xml file, and adding the starter-logging dependency, but that also creates empty log files with nothing on them. 我还尝试了logback,方法是添加logback.xml文件,并添加starter-logging依赖项,但这也会创建空的日志文件,并且上面没有任何内容。

In the Spring boot documentation, the application.properties file is mentioned. 在Spring引导文档中,提到了application.properties文件。 So I created this in WEB-INF, and put the logging.level.org.springframework and the logging.path entries. 因此,我在WEB-INF中创建了此文件,然后将logging.level.org.springframeworklogging.path条目放入。 But still no log file created at all. 但是仍然根本没有创建日志文件。

I don't want to log my own messages, I just want to log the events generated by Spring itself. 我不想记录自己的消息,只想记录Spring本身生成的事件。 And I also don't have a main method, only an Endpoint for the web service, if any of these might be relevant. 而且我也没有主要方法,只有Web服务的端点(如果其中任何一个可能相关)。 So what I need is the simplest possible logging, possibly without adding too many dependencies or so, and retrieve the Spring messages into the log. 因此,我需要的是最简单的日志记录,可能无需添加太多依赖项,然后将Spring消息检索到日志中。 Can someone tell me what am I doing wrong? 有人可以告诉我我在做什么错吗?

logback is the default logging implementation used by Spring Boot (I assume you are using spring boot to create a jar and run that!). logback是Spring Boot使用的默认日志记录实现(我假设您正在使用Spring Boot创建一个jar并运行它!)。 Basicly you don't need to do anything logging is already handled by the starter you included. 基本上,您不需要执行任何操作,您已包含的启动程序已经处理了日志记录。

There is a default logback.xml shipped with Spring which turns on logging, for the spring framework this is on INFO. Spring附带了一个默认的logback.xml ,它可以打开日志记录,对于spring框架,它位于INFO上。

If you want to override this simply create a logback.xml file with the same content as the default one and put the logger for spring on DEBUG or TRACE. 如果要覆盖此内容,只需创建一个内容与默认内容相同的logback.xml文件,然后将spring的记录器放在DEBUG或TRACE上。 Not sure if you really want to put everything on trace unless you want a less performant application. 除非您想要性能较低的应用程序,否则不确定是否真的要跟踪一切。

As to what you are doing wrong is that you are probably doing to much and thinking to complex. 至于您做错了什么,就是您可能在做很多事情,并考虑到复杂问题。

I created this in WEB-INF 我是在WEB-INF中创建的

That's the only problem with what you already tried. 那是您已经尝试过的唯一问题。 It has to be on the classpath (*so put it in "src/main/resources" if you have a normal build configuration, and "WEB-INF/classes" if you are building a WAR by hand for some reason). 它必须在类路径上(如果具有正常的构建配置,请将其放在“ src / main / resources”中;如果出于某种原因手工构建WAR,则将其置于“ WEB-INF / classes”中)。

dont know if logging.level.org.springframework and the logging.path works with spring boot. 不知道logging.level.org.springframework and the logging.path可以在Spring Boot中使用。 You can try to move your logback.xml file and application.properties directly to resources dir + paste this to logback.xml 您可以尝试将logback.xml文件和application.properties直接移动到resources目录dir并将其粘贴到logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework" level="DEBUG" appender-ref="FILE"/>
</configuration>

this should log on console and your /tmp (if linux) dir. 这应该登录控制台和您的/ tmp(如果是Linux)目录。

//Update: //更新:

to manually set where to write logs you can do something like ( i basically reduced whats inside of base.xml which is imported): 手动设置写入日志的位置,您可以执行以下操作(我基本上减少了导入的base.xml内的内容):

<appender name="FILE_APPENDER"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex</pattern>
    </encoder>
    <file>C://yourDir</file>
</appender>

and then change 然后改变

<logger name="org.springframework" level="DEBUG" appender-ref="FILE"/>

to: 至:

<logger name="org.springframework" level="DEBUG" appender-ref="FILE_APPENDER"/>

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

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