About the Example Application

This Short Cut is going to be filled with examples; lots of examples. As noted earlier, this Short Cut also comes with an Example Application (available at the code example repository), and every example in this Short Cut (except where explicitly noted) comes straight from it. That way you can go right from here to the example, see the full code and configuration, and run it. You can match up the example number from here to the build target to execute. The configuration files (log4j.properties, for example) will be copied to the PROJECT_ROOT/target/classes directory, where PROJECT_ROOT is the full path to the directory where you installed the sample code. In all examples - unless otherwise noted - the PROJECT_ROOT folder is C:homeworkspacelog4j-example on Windows and ~/workspace/log4j-example on Linux.

I have kept the Example Application simple so that you don’t spend time learning how the application works, and can focus your efforts on learning how it uses log4j (Figure 1 illustrates the Example application architecture). The application is a simple, standalone Supplier/Consumer application. One or more Supplier threads do “work” (via the WorkUnit class), place an item into a Queue, where one or more Consumer threads similarly do “work.”

Example application architecture: Through JMX a web browser is used to manage and monitor the application as it runs.

Figure 1. Example application architecture: Through JMX a web browser is used to manage and monitor the application as it runs.

Each class is instrumented as a JMX MBean so you can see what is going on inside the application and control it through a web browser. You can change log levels, start worker threads, suspend threads, and so on. Below is a diagram showing the architecture of the example application. You can interact with the example application as it runs by pointing your browser at http://localhost:8090 (make sure that port is free on your machine). When I start the example application and point my browser to that URL, I see the following screen:

Managing the Example Application with Firefox through JMX

Figure 2. Managing the Example Application with Firefox through JMX

To interact with one of the components (again, which are JMX MBeans), click on the link (e.g., click on name=Controller to interact with the Controller MBean). The interface is fairly intuitive so I won’t go into it here.

As we move through the sections that follow, I recommended that you run the examples as they are presented so you can see the output that is produced from the various configurations shown. Log output can scroll for many pages, so the examples showing log output in this Short Cut (where space is at a premium) are elided for clarity just to give you a sense of what you will see. Always refer to the example application—and example number—when studying the examples that follow.

As I mentioned, this Short Cut is mainly to demonstrate proper log4j configuration. To that end, all of the examples have configuration files (located in PROJECT_ROOT/conf) with the same names as the examples. For example, to see the configuration for Example 3, see PROJECT_ROOT/conf/example1-3.properties (and/or example1-3.xml).

To run an example, navigate to the PROJECT_ROOT folder and run the run-example.sh or run-example.bat script, making sure to pass it the example you want to run. For example, to run Example 2 on Windows you would run the following command:

C:homelog4j-example> run-example.bat example1-2

And on Linux:

~/log4j-example$ run-example.sh example1-2

For each example (where possible), a properties-style configuration, and a XML configuration file is provided. I prefer properties-style configuration, and the examples prefer to use their respective properties configuration as a result. However, if you want to use the XML configuration with an example (so you can play around with it, change values, etc), add “-xml” to the end of the target name, and the XML configuration will be picked up instead of the properties configuration. So to run Example 2, but use the XML configuration:

C:homelog4j-example> run-example.bat example1-2-xml

And on Linux:

~/log4j-example$ run-example.sh example1-2-xml

Note

For all examples, properties-style configuration is not possible. For these examples, only an XML configuration file is provided, so you do not need to qualify the target name with “-xml” in those cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.145.158