A Logger object is used to log messages for a specific system or application component.
A Logger is useful to track the sequence of actions.
Each Logger has a “Level” associated with it. This reflects a minimum Level that this logger cares about.
The log level can be configured on the Logger.setLevel method.
Refer to the API Link for different levels.
Log Levels
- SEVERE (highest value)
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST (lowest value)
- OFF
- ALL
Logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package total_qa_logger; import java.io.File; import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import java.util.logging.XMLFormatter; public class LoggerExample { public static void main(String args[]) throws SecurityException, IOException { String logFilePath = new File(System.getProperty("user.dir"))+"/Logs/logs.txt"; FileHandler handler = new FileHandler(logFilePath,false);//true append the logs , false it will override the logs Logger log = Logger.getLogger(logFilePath); log.addHandler(handler); handler.setFormatter(new XMLFormatter()); log.setLevel(Level.INFO); log.info("Browser Launched Successfully"); log.info("Validated Login Functionality"); handler.setFormatter(new SimpleFormatter()); log.setLevel(Level.SEVERE); log.severe("Browser Launched Successfully"); log.severe("Validated Login Functionality"); } } |
Conclusion:
Logging using a java.util.logging.Logger class is easy to implement in Java and Selenium automation projects.
OUTPUT: