This section introduce you about Java logging and how to use the logger in Java program to log the error and messages into the log file. Explanation of java logging is explained in the given example program. This program will help you understand the java logging and its implementation in the java program.
Java provides logging APIs like: Logger, Level, Handler etc. for implementing logging features in your java application. It is a part of J2SE (Java 2 Standard Edition). To create java logging program you need a Logger object. The Logger object contains log messages. Logger has one or more handler that performs log records. Java logging provides a way to contain multiple types of message like: warning, info, severe etc. for an application. These information or messages can be used for many purposes but it is specially used for debugging, troubleshooting and auditing.
The first program simply create a log file and shows log message for the warning, info and servere and another checked the logging message. This program creates a log file that contains multiple information.
This is the constructor of FileHandler class. FileHandler class extends the StreamHandler. This is the class of java.uti.logging package. The above constructor constructs the FileHandler with given string as file name.
This is the constructor of Logger class. The logger class extends the Object. IThis is the class of java.util.logging package. Logger constructor used for constructing a logger. A logger object contains a log message to the system or application component. The logger object called by the getLogger method and returns a suitable maintain logger.
You can find or construct a logger with the help of above method. It takes string types value that can be used for a named subsystem.
It can be used for adding a log handler to get logging message which is maintaining in the warning, info and severe.
This method used for showing the log WARNING message. The given message shows to all output handler objects when the logger is enabled at the time of warning message level. It takes string type value for the warning message.
It works same as warning method but it shows log INFO message.
It also works same as like: warning and info method but it shows log SERVERE message.
Here is the code of "QuintLog.java":
This program check logger object. If given instruction is certify then shows finest message like: 'Display a finest message' otherwise no any message appear to here.
The above method is a Boolean type than returns true or false. It checks the given message is actually logged by this logger that means method is returned true.
The above method is used to represent the Log FINEST message. This message shows to all output handler objects when the logger is enabled at the time of finest message level . It takes string type value for message.
Here is the code of "CheckLogMessage.java":
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial