简体   繁体   中英

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.

So, I need to implement logging for a Web Service based on Spring. Maven is used for dependencies, with the spring-boot-starter-ws dependency, and not the whole 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. 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.

In the Spring boot documentation, the application.properties file is mentioned. So I created this in WEB-INF, and put the logging.level.org.springframework and the logging.path entries. 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. And I also don't have a main method, only an Endpoint for the web service, if any of these might be relevant. 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. 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!). 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.

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. 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

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).

dont know if logging.level.org.springframework and the logging.path works with spring boot. You can try to move your logback.xml file and application.properties directly to resources dir + paste this to 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.

//Update:

to manually set where to write logs you can do something like ( i basically reduced whats inside of base.xml which is imported):

<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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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