Author Archive


Logging with Log4j2

Saturday, October 17th, 2015

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 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.

Maven is the build configurator. Here are the lines from the Maven pom.xml file that are necessary for Log4j2 to work. You need both of these as a minimum. Be sure the check for the latest version and update yours accordingly.

Log4j2 Config File

The configuration or preferences file is usually an xml file, although you can also use JSON or a plain text properties file. This file is critical and can have a number of different names. I chose log4j2-test.xml. It needs to reside in the Java classpath. Ours today is here in src/test/resources. This is very bare-bones setup except for the RollingFile appender which gives files datestamps.

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”.

And here’s where we actually use the logger. Just replace your normal System.out.println with yourLogger.level – as in this case it’s gLogger.info – for the info level (see levels below).

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:
https://github.com/nmschorr/git2/tree/master/SMedia

Java JNA to Dismiss Windows 7 Dialog

Friday, October 2nd, 2015

This video uses Java JNA to dismiss a Windows 7 native dialog that pops up while Firefox is launching during a Selenium test. The same technique can be used to manage any Windows 7 native dialog or window from java in many other situations.

In this case, the alert declares that Firefox has crashed, and waits to be manually dismissed. Until dismissed somehow, it blocks Firefox and the test from continuing.

This workaround addresses these logged bugs:
Firefox Bug 1167511 – When running selenium automated test, a window pops up saying “Firefox has stopped working”
Firefox Bug 1157672 – Firefox crashes on shutdown when invoked with -silent and empty profile directory
Selenium Bug #437 – Firefox 38 doesn’t work with WebDriver 2.45.1

This workaround may work in other situations where something within a Selenium script causes a Windows alert dialog window to appear.

The alert can’t be dismissed normally without manual intervention. Selenium can’t see it because it’s put up by by Windows and is not part of the Firefox..The alert needs to go away before the thread that caused it can continue.

Simple scripts run as a single program thread, each step executing after the previous step is done.
We need to start a second program thread to go around the outside of our normal set of steps to dismiss that dialog.

First create a new thread by creating a class that is an extension of the Thread class. Then we add our alert-dismiss code to the run method of this 2nd thread. When the thread starts, the dismiss code will execute.

JNA stands for Java Native Access. It’s a community developed library that supports many languages and platforms like MacOS and Linux. JNA provides a simple set of methods that access more complex lower-level operating system calls. It makes programming system calls easy.

Here’s the snippet of JNA code that closes the alert window:

Here’s the code to create the 2nd thread:

Here’s the code for starting the 2nd thread

See the entire project at:
https://git.io/vC7wp

Selenium with Java Testing a Website

Friday, October 2nd, 2015

This video shows the Eclipse IDE running a web test written with Selenium, Java, JUnit, Log4j2, Maven and other packages. It tests against my own website: schorrmedia.com which is also known as schorrmedia.com/wordpress

The tests are run with JUnit, so each test stands on its own and can be run independently of the others. Each test launches a fresh instance of FireFox with Selenium WebDriver. The tests use Maven and a Maven pom.xml file to specify dependencies. The entire suite was written in and runs with Eclipse on a Windows 7 PC.

The source code can be viewed on GitHub at: https://git.io/vC7wp

This demo was done with inverted color on a Windows 7 machine. I’m using inverted color a lot lately because it’s easier on the eyes.

The script also includes full Maven Surefire site test result reporting which I hope to share in a future video. To read more about Selenium, see: www.seleniumhq.org

Thank you Guru99.com for helping me learn Selenium. Their Selenium Course was very useful and free.