- Loggerfactory class in java
- Field Summary
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Field Detail
- MQTT_CLIENT_MSG_CAT
- Constructor Detail
- LoggerFactory
- Method Detail
- getLogger
- getLoggingProperty
- setLogger
- Spring Boot Logging with LoggerFactory
- Adding Logging to Your Java Class
- Example of Java class that uses Spring Boot Logging
- Enable DEBUG level logging
- Output logs to a File
- Related Posts:
- Loggerfactory class in java
- Method Summary
- Methods inherited from class java.lang.Object
- Method Detail
- getLogger
- getLogger
- getILoggerFactory
Loggerfactory class in java
A factory that returns a logger for use by the MQTT client. The default log and trace facility uses Java’s build in log facility:- java.util.logging. For systems where this is not available or where an alternative logging framework is required the logging facility can be replaced using setLogger(String) which takes an implementation of the Logger interface.
Field Summary
Constructor Summary
Method Summary
Modifier and Type | Method and Description |
---|---|
static Logger | getLogger (java.lang.String messageCatalogName, java.lang.String loggerID) |
Set the class name of the logger that the LoggerFactory will load If not set getLogger will attempt to create a logger appropriate for the platform.
Methods inherited from class java.lang.Object
Field Detail
MQTT_CLIENT_MSG_CAT
public static final java.lang.String MQTT_CLIENT_MSG_CAT
Constructor Detail
LoggerFactory
Method Detail
getLogger
public static Logger getLogger(java.lang.String messageCatalogName, java.lang.String loggerID)
Find or create a logger for a named package/class. If a logger has already been created with the given name it is returned. Otherwise a new logger is created. By default a logger that uses java.util.logging will be returned.
getLoggingProperty
public static java.lang.String getLoggingProperty(java.lang.String name)
When run in JSR47, this allows access to the properties in the logging.properties file. If not run in JSR47, or the property isn’t set, returns null.
setLogger
public static void setLogger(java.lang.String loggerClassName)
Set the class name of the logger that the LoggerFactory will load If not set getLogger will attempt to create a logger appropriate for the platform.
Spring Boot Logging with LoggerFactory
Adding logging support to your RESTful Web Service application with Spring Boot is extremely simple.
To use Spring Boot logging, you do not need to add any additional dependencies or libraries to your web app project other than the spring-boot-starter-web dependency. If you are working on a Spring Boot Web or a Web Services project, you likely already have the below dependency in your pom.xml file. Otherwise, you need to add it.
org.springframework.boot spring-boot-starter-web
Adding Logging to Your Java Class
Let’s add logging to our RestController class, for example. Below is a simple example of a RestController class that logs a message on the DEBUG level.
- Import org.slf4j.Logger and org.slf4j.LoggerFactory to my class,
- Get an instance of org.slf4j.Logger by using the LoggerFactory and its static method getLogger().
Logger log = LoggerFactory.getLogger(this.getClass());
Example of Java class that uses Spring Boot Logging
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.appsdeveloperblog.examples.jwt.model.request.CreateUserRequestBody; @RestController @RequestMapping("/users") public class UsersController < private final Logger log = LoggerFactory.getLogger(this.getClass()); @GetMapping public String checkStatus() < log.debug("Inside of checkStatus() method "); return "working"; >@PostMapping public String createUser(@RequestBody CreateUserRequestBody createUserRequestBody) < return "user created"; >>
Enable DEBUG level logging
To enable DEBUG level logging for all classes in a Java package, you can update application.properties. An example of how to update the application.properties file is below.
logging.level.com.appsdeveloperblog.examples=DEBUG
Output logs to a File
Log messages can be stored in a log file. To configure your Spring Boot application to output log messages to a specific file, add the following property to the application.properties file.
logging.file = /complete-path-to-file-here/log-file-name.log
There are many more configuration options for advanced users of Logging, but to quickly add very simple logging to your Spring Boot Web App, this is all you need to do.
I hope this short blog post was helpful. Please check the video courses below to learn more about Spring Boot and how to build RESTful Web Services with the Spring framework.
Related Posts:
Loggerfactory class in java
The LoggerFactory is a utility class producing Loggers for various logging APIs, most notably for log4j, logback and JDK 1.4 logging. Other implementations such as NOPLogger and SimpleLogger are also supported. LoggerFactory is essentially a wrapper around an ILoggerFactory instance bound with LoggerFactory at compile time. Please note that all methods in LoggerFactory are static.
Method Summary
Return a logger named corresponding to the class passed as parameter, using the statically bound ILoggerFactory instance.
Return a logger named according to the name parameter using the statically bound ILoggerFactory instance.
Methods inherited from class java.lang.Object
Method Detail
getLogger
Return a logger named according to the name parameter using the statically bound ILoggerFactory instance.
getLogger
Return a logger named corresponding to the class passed as parameter, using the statically bound ILoggerFactory instance. In case the clazz parameter differs from the name of the caller as computed internally by SLF4J, a logger name mismatch warning will be printed but only if the slf4j.detectLoggerNameMismatch system property is set to true. By default, this property is not set and no warnings will be printed even in case of a logger name mismatch.
getILoggerFactory
Return the ILoggerFactory instance in use. ILoggerFactory instance is bound with this class at compile time.