0

I have one service AccessLogsApplication in which i have one controller HelloWorld. I want to print every incoming request in separate log file called access_log.log in spring boot.

@RestController
public class HelloWorld {

private static final Logger log = LoggerFactory.getLogger(HelloWorld.class);

@RequestMapping(value = "/hello/1", method = RequestMethod.GET)
public String getHello() {
    log.info("In method 1");
    return "helloWorld";
}

@RequestMapping(value = "/hello/2", method = RequestMethod.GET)
public String getHelloFromUser() {
    log.info("In method 2");
    return "helloWorld From Sudhanshu Saini";
 }
}

Here is my application.properties

spring.application.name=HelloWorld
server.port=8080

server.tomcat.accesslog.buffered=true
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.directory=logs
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.pattern="%h %l %u %t %r %s %b %D"


logging.level.root=warn
logging.level.com.saini.access_logs=debug

logging.path=logs
logging.file=${logging.path}/hello_service.log

I want to log every in coming request for this service. like this (This is manually written by me not access logs)-

  [03/May/2019:16:37:52 +0530] "GET /hello/1 HTTP/1.0" 200 2
  [03/May/2019:16:37:52 +0530] "GET /hello/2 HTTP/1.0" 200 2

Access logs are not working in spring boot. Its not creating a file access_log.log

Robert Lacok
  • 4,176
  • 2
  • 26
  • 38
Sudhanshu Saini
  • 127
  • 1
  • 2
  • 9
  • The answer for your issue could be here: https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with-exceptions-in-single-pl – szachMati May 03 '19 at 11:15
  • @szachMati i dont want to log request and response, i want to log only http endpoints which are coming to service – Sudhanshu Saini May 03 '19 at 11:18

4 Answers4

2

There is also SLF4J library instead of using standard logging from java.util.Three materials with logging events are here:

Maybe it will help :)

szachMati
  • 196
  • 2
  • 12
0

Please configure server.tomcat.basedir=directory path

Ex : server.tomcat.basedir=tomcat

RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26
0

Add these to application.properties to enable tomcat access logs

 server.tomcat.basedir=/var/tomcat
 server.tomcat.accesslog.buffered=true
 server.tomcat.accesslog.directory=/access/log
 server.tomcat.accesslog.enabled=true
 server.tomcat.accesslog.pattern=%h %{X-Forwarded-For}i %l %u %t "%r" %s %b
 server.tomcat.accesslog.prefix=tomcat_access_log
Tan mally
  • 572
  • 1
  • 5
  • 11
-2

You are going to need to pass the appropriate string to the environment. Headers have being offered using the title of theirs when you do not wish to get all headers in a single line generally. Should you need more you are able to most likely log them with your app also. check here