filmov
tv
Log4j2 with Selenium

Показать описание
Log4j2 is a successor to the original Log4j — a Java utility library used to output log files from a program. It writes messages to various endpoints can include all sorts of destinations like emails, webpages, databases and more. I tried a few other newer loggers like SLF4J and standard Log4j, but came back to this one because the others were so difficult to set up. Log4j2 is simple, lightweight and powerful. Here we will set up Log4j2 in a Java project in Eclipse.
Log4j2 Config File
The Logger is the actor who is doing the logging. Each logger can have any number of appenders. You can have an number of loggers configured in many different ways and callled on at different times. In this case we only have one logger called Root. There is always at least the root logger, and it’s the top of the hierarchy. We’ll just use that one since we’re trying to keep this simple.
An Appender is actually the location that the information is going to – like the console. You can specify reports, files, emails, network locations, sockets, databases, etc. Within the Appender section is where you set up what levels of output you want and to which locations, as well and other settings. The first one we have set up is for the console.
The RollingFile appender sets up our log files so that when a new one is created, the last log file is renamed with a datestamp so it’s not overwritten. Notice each appender has a PatternLayout which is self-explanatory.
Here are the Java imports which won’t work unless your maven log4j statements are set up correctly:
Here is the Setup code in the the Java program where it actually connects to it’s xml configuration file. I call the root logger using a special method called “getRootLogger” – for all other loggers you would use the method “getLogger”.
or:
public static Logger createLogger() {
verificationErrors = new StringBuffer();
return aLogger;
}
protected static void createLogFile (FirefoxProfile fp) throws Exception {
File outfile = new File(outfileName);
}
or:
@BeforeClass //run once before each testsuite
public static void setUpClass() throws Exception {
gLogger = createLogger();
}
@Test
public void testDemo() throws Exception {
shortTest();
}
Log4j Log Levels
Each level prints itself plus the levels that came before it. So if you choose WARN you also get ERROR and FATAL messages. Each level enables a finer granularity of detail. DEBUG and TRACE are intended for development only.
OFF No logging
FATAL Severe errors
ERROR non-fatal errors
WARN misc warnings
INFO runtime events
DEBUG Detailed info
TRACE Most detailed
TTCC is a message format used by log4j -an acronym for Time Thread Category Component.
See the entire project at:
See the original full article at:
Комментарии