Logging in Java
In the following tutorial, we will learn about logging in Java. Application activity is stored in log files and we can obtain those log files using Java. Logging means keeping a record of all inputs, processes, and outputs which can be done at levels such as monitoring all activity or logging only when an error occurs.
Logging is essential and we can specify the amount or the level of the information we require from the log files. Logging can provide general information as well as highly detailed information, whichever the application demands.
Levels of Logging
The levels of logging allow us to organize logs according to their level of detail and severity. The library java.util.logging provides us with several levels of logging that are:
- SEVERE (HIGHEST)
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST (LOWEST)
Log Manager
The Log Manager provides several inbuilt methods for logging application activity. The Log Manager maintains the logs and the getLogger() method is used to obtain its instantiation. GLOBAL_LOGGER_NAME is the identifier used for the logger.
import java.util.logging.*; class logger{ Logger log = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); public void generatelog() { log.log(Level.INFO,"INFO Provides General Logging Information"); } } public class LoggerProg { public static void main(String[] args) { logger x = new logger(); x.generatelog(); LogManager manager = LogManager.getLogManager(); Logger y = manager.getLogger(Logger.GLOBAL_LOGGER_NAME); y.log(Level.INFO, "CODESPEEDY TECHNOLOGY"); } }
Output:
Apr 27, 2020 3:09:01 AM loggerprog.logger generatelog INFO: INFO Provides General Logging Information Apr 27, 2020 3:09:01 AM loggerprog.LoggerProg main INFO: CODESPEEDY TECHNOLOGY
Also read: Solve Coin Change problem in Java
Leave a Reply