Books in black and white
 Main menu Share a book About us Home
 Books Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

# More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Previous << 1 .. 16 17 18 19 20 21 < 22 > 23 24 25 26 27 28 .. 166 >> Next

A first attempt at using this log package, experimenting with levels of granularity, can be shown in Listing 5.1.
01 package org.javapitfalls.item5;
02
03 import java.io.*;
04 import java.util.logging.*;
05
06 public class BadLoggerExample1
07 {
08 private Logger m_log = null;
09
10 public BadLoggerExample1(Level l)
Listing 5.1 BadLoggerExample 1 (continued)
46 Item 5
11 {
12
13
14 //This creates the logger!
15 m_log = 2
16
17 m_log.setLevel(l);
18 }
19 /*
20 * This tests the levels of granularity!
21 */
22 public void test()
23 {
24 System.out.println("The level for the log is: "
25 + m_log.getLevel());
26 m_log.finest("This is a test for finest");
27 m_log.finer("This is a test for finer");
28 m_log.fine("This is a test for fine");
29 m_log.info("This is a test for info");
30 m_log.warning("This is a warning test");
31 m_log.severe("This is a severe test");
32 }
33
34 /*
35 * A very simple example, where we will optionally
36 * pass in the level of granularity to our application
37 */
38 public static void main(String[] args)
39 {
40 Level loglevel = Level.INFO;
41
42 if ( args.length !=0 )
43 {
44 if ( args[0].equals("ALL") )
45 {
46 loglevel = Level.ALL;
47 }
48 else if ( args[0].equals("FINE") )
49 {
50 loglevel = Level.FINE;
51 }
52 else if ( args[0].equals("FINEST") )
53 {
54 loglevel = Level.FINEST;
55 }
56 else if ( args[0].equals("WARNING") )
57 {
58 loglevel = Level.WARNING;
Listing 5.1 (continued)
Avoiding Granularity Pitfalls in java.util.logging 47
59 }
60 else if ( args[0].equals("SEVERE") )
61 {
62 loglevel = Level.SEVERE;
63 }
64
65 }
67 logex.test();
68 }
69 }
Listing 5.1 (continued)
In Listing 5.1, you can see that we create a simple logger and call a test function that tests the levels of granularity. In the main() method of this class, we pass it an argument pertaining to the level of granularity (ALL, FINE, FINEST, WARNING, SEVERE), or if there is no argument, the loglevel defaults to INFO. If you run this program without an argument, you will see the following printed to standard error, which is correct output:
The level for the log is: INFO
Feb 16, 2002 3:42:08 PM org.pitfalls.logging.BadLoggerExample1 test INFO: This is a test for info
Feb 16, 2002 3:42:08 PM org.pitfalls.logging.BadLoggerExample1 test WARNING: This is a warning test
Feb 16, 2002 3:42:08 PM org.pitfalls.logging.BadLoggerExample1 test SEVERE: This is a severe test
Additionally, if you pass SEVERE as an argument, you will see the following correct output:
The level for the log is: SEVERE
Feb 16, 2002 3:42:09 PM org.pitfalls.logging.BadLoggerExample1 test SEVERE: This is a severe test
However, if you run this program with the argument FINE, you will see the following:
The level for the log is: FINE
Feb 16, 2002 3:42:10 PM org.pitfalls.logging.BadLoggerExample1 test INFO: This is a test for info
Feb 16, 2002 3:42:10 PM org.pitfalls.logging.BadLoggerExample1 test WARNING: This is a warning test
Feb 16, 2002 3:42:10 PM org.pitfalls.logging.BadLoggerExample1 test SEVERE: This is a severe test
48 Item 5
What happened? Where are the "fine" messages? Is something wrong with the Logger? We set the level of granularity to FINE, but it still acts as if its level is INFO. We know that is wrong, because we printed out the level with the Logger's getLevel() method. Let us add a FileHandler to our example, so that it will write the log to a file, and see if we see the same output. Listing 5.2 shows the BadLoggerExample2, where we add a FileHandler to test this. On lines 20 and 21 of Listing 5.2, we create a new FileHandler to write to the log file log.xml, and we add that handler to our Logger object.
01: package org.javapitfalls.item5;
02:
03: import java.io.*;
04: import java.util.logging.*;
05:
06: public class BadLoggerExample2
07: {
08: private Logger m_log = null;
09:
10: public BadLoggerExample2(Level l)
11: {
12: FileHandler fh = null;
13:
14: //This creates the logger!
15: m_log = 2
16:
17: //Try to create a FileHandler that writes it to file!
18: try
19: {
20: fh = new FileHandler("log.xml");
22: }
23: catch ( IOException ioexc )
24: {
25: ioexc.printStackTrace();
26: }
27:
28: m_log.setLevel(l);
29: }
30: /*
31: * This tests the levels of granularity!
32: */
33: public void test()
34: {
35: System.out.println("The level for the log is: "
Listing 5.2 BadLoggerExample2.java (continued)
Avoiding Granularity Pitfalls in java.util.logging 49
36 + m_log.getLevel());
37 m_log.finest("This is a test for finest");
38 m_log.finer("This is a test for finer");
Previous << 1 .. 16 17 18 19 20 21 < 22 > 23 24 25 26 27 28 .. 166 >> Next