Java Reference
In-Depth Information
3.
Using the methods included in Log4j 2, you can create log messages of various importance levels.
4.
Without creating any configuration file, there is a default logger that prints messages at the Error
level or higher to the console.
5.
By editing this configuration file, you can control which level of messages get printed and where
they will be printed.
6.
In the appenders section, you added two files. These create log files in which log messages can be
written. Files require name and filename fields. In the filename file, you should include the path to
the file. They should also be given a PatternLayout , which specifies how the messages will be for-
matted. In this exercise, they are named according to the level they will be assigned. One of the log
files is stored in the src folder of the project you are working in. The other will be stored on the
hard drive of the machine, as indicated in the filename.
7.
In the loggers section, you can add new loggers or you can attach appenders to the root logger.
This is similar to the approach with the built‐in logger, where handlers were added to the logger.
8.
You can control the level of the logger separately from the level of the appenders. Both of these are
set in the loggers section. Here, you have to set the root level to debug, meaning that all log mes-
sages will be accepted by the logger. However, the three appender levels are set to fatal, info, and
warn, so only those set to info and above will be used in the output. Only fatal messages will be
printed to the console. Info level and above will be printed to the ApacheLog_Info.log file, and
warn level and above will be printed to the ApacheLog_Warn.log ile.
You've seen two types of logging utilities, a built‐in version from Java and an open source API from
Apache. Although they function similarly, there may be reasons to choose one over the other. Many
developers are choosing Log4j over the built‐in tool, not only because of its functionality, but because
there are some criticisms of the implementation of the built‐in logger. There are also several other well‐
known loggers that you may want to investigate further. Hopefully, the examples in this section have
provided you with a foundation to begin logging your programs, as well as a basic understanding from
which you can investigate other tools and the other options within the two tools presented here.
testing your applications
Unit testing is another way you can ensure the completeness and correctness of your applications. It
allows you to develop small test scenarios to check each aspect of your program. This offers some
benefits over debugging from the main method. First, you can begin testing even when only small
parts of your application are written. In fact, some development styles start from test cases and
then write code that will satisfy the test. Compared to debugging from the main method, you can
start testing much earlier in the process and may be able to find and fix problems early and before
they impact many other areas of the development. Once your test cases are prepared, then any
changes you make to the program later can be checked quickly by running the tests again to see if
you've accidentally introduced new errors. Also, professional developers are not usually creating an
entire program; they are working on pieces that will be integrated later. In this case, a main method
doesn't really make much sense, but test units can provide confidence that the units themselves are
functioning properly before trying to fit them together in the end. JUnit is a commonly used testing
framework, which is also included in Eclipse.
 
Search WWH ::




Custom Search