By default slf4jcan be used for logging.
Changes required -
1. Define Logger object in the class
2. Use the Logger with appropriate level (info, debug, etc.)
3. Set the file name and path in application.properties to write to a specific file.
application.properties
logging.file.name=logs/restapp.log
all the logs will be writted to restapp.log file under logs folder, of the application.
To change/restrict the log level to be printed in the log files, set the below property in application.properties file
application.properties
logging.level.root=error
or
logging.level.root=info
logging.level.com.raj.*=error (To enable for a custom package)
or
logging.level.root=info
logging.level.org.springframework.*=error (To enable for a framework)
Controller class
package com.raj.springweb.controllers;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.raj.springweb.model.Product;
import com.raj.springweb.repos.ProductJpaRepository;
@RestController
public class RestProductController {
@Autowired
ProductJpaRepository productRepo;
private static final Logger LOGGER = LoggerFactory.getLogger(RestProductController.class);
@RequestMapping (value="/productapi/allproducts", method=RequestMethod.GET)
public List findAllProducts () {
LOGGER.info("Find All Products");
return productRepo.findAll();
}
}
Changes required -
1. Define Logger object in the class
2. Use the Logger with appropriate level (info, debug, etc.)
3. Set the file name and path in application.properties to write to a specific file.
application.properties
logging.file.name=logs/restapp.log
all the logs will be writted to restapp.log file under logs folder, of the application.
To change/restrict the log level to be printed in the log files, set the below property in application.properties file
application.properties
logging.level.root=error
or
logging.level.root=info
logging.level.com.raj.*=error (To enable for a custom package)
or
logging.level.root=info
logging.level.org.springframework.*=error (To enable for a framework)
Controller class
package com.raj.springweb.controllers;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.raj.springweb.model.Product;
import com.raj.springweb.repos.ProductJpaRepository;
@RestController
public class RestProductController {
@Autowired
ProductJpaRepository productRepo;
private static final Logger LOGGER = LoggerFactory.getLogger(RestProductController.class);
@RequestMapping (value="/productapi/allproducts", method=RequestMethod.GET)
public List
LOGGER.info("Find All Products");
return productRepo.findAll();
}
}
No comments:
Post a Comment