Skip to content

Commit e0e7ced

Browse files
author
Bruce Eckel
committed
Temporarily adding "test" directory
1 parent 7927995 commit e0e7ced

15 files changed

+708
-2
lines changed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class Tags {
5959
private def hasTag(String marker) {
6060
return block.contains("// {" + marker + "}")
6161
}
62-
6362
def extractOutputLine() {
6463
def matcher = (block =~ /(?m)^(\/\* Output:.*)$/)
6564
if (matcher) {
@@ -68,7 +67,6 @@ class Tags {
6867
return null
6968
}
7069
}
71-
7270
private def extract(String marker) {
7371
// Assume some whitespace is after marker
7472
if(!block.contains("// {${marker} "))

test/ConfigureLogging.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// test/ConfigureLogging.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {java ConfigureLogging
6+
// -Djava.util.logging.config.file=log.prop}
7+
// {ErrorOutputExpected}
8+
import java.util.logging.*;
9+
10+
public class ConfigureLogging {
11+
static Logger
12+
lgr = Logger.getLogger("com"),
13+
lgr2 = Logger.getLogger("com.mindviewinc"),
14+
util= Logger.getLogger("onjava"),
15+
test= Logger.getLogger("com.mindviewinc.test"),
16+
rand = Logger.getLogger("random");
17+
public ConfigureLogging() {
18+
/*
19+
Set Additional formatters, Filters and
20+
Handlers for the loggers here. You cannot
21+
specify the Handlers for loggers except
22+
the root logger from the configuration
23+
file.
24+
*/
25+
}
26+
public static void main(String[] args) {
27+
sendLogMessages(lgr);
28+
sendLogMessages(lgr2);
29+
sendLogMessages(util);
30+
sendLogMessages(test);
31+
sendLogMessages(rand);
32+
}
33+
private static void
34+
sendLogMessages(Logger logger) {
35+
System.out.println(" Logger Name : "
36+
+ logger.getName() + " Level: "
37+
+ logger.getLevel());
38+
logger.finest("Finest");
39+
logger.finer("Finer");
40+
logger.fine("Fine");
41+
logger.config("Config");
42+
logger.info("Info");
43+
logger.warning("Warning");
44+
logger.severe("Severe");
45+
}
46+
}
47+
/* Output:
48+
Logger Name : com Level: null
49+
Logger Name : com.mindviewinc Level: FINEST
50+
Logger Name : onjava Level: INFO
51+
Logger Name : com.mindviewinc.test Level: FINER
52+
Logger Name : random Level: SEVERE
53+
___[ Error Output ]___
54+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
55+
FINEST: Finest
56+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
57+
FINER: Finer
58+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
59+
FINE: Fine
60+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
61+
CONFIG: Config
62+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
63+
INFO: Info
64+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
65+
WARNING: Warning
66+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
67+
SEVERE: Severe
68+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
69+
FINEST: Finest
70+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
71+
FINER: Finer
72+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
73+
FINE: Fine
74+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
75+
CONFIG: Config
76+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
77+
INFO: Info
78+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
79+
WARNING: Warning
80+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
81+
SEVERE: Severe
82+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
83+
INFO: Info
84+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
85+
WARNING: Warning
86+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
87+
SEVERE: Severe
88+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
89+
FINER: Finer
90+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
91+
FINE: Fine
92+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
93+
CONFIG: Config
94+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
95+
INFO: Info
96+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
97+
WARNING: Warning
98+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
99+
SEVERE: Severe
100+
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
101+
SEVERE: Severe
102+
*/

test/CustomHandler.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// test/CustomHandler.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// How to write custom handler
6+
// {ErrorOutputExpected}
7+
import java.util.logging.*;
8+
import java.util.*;
9+
10+
public class CustomHandler {
11+
private static Logger logger =
12+
Logger.getLogger("CustomHandler");
13+
private static List<String> trace =
14+
new ArrayList<>();
15+
public static void main(String[] args) {
16+
logger.addHandler(new Handler() {
17+
@Override
18+
public void publish(LogRecord logRecord) {
19+
trace.add(logRecord.getLevel() + ":");
20+
trace.add(logRecord.getSourceClassName()
21+
+ ":");
22+
trace.add(
23+
logRecord.getSourceMethodName() +":");
24+
trace.add("<" + logRecord.getMessage()
25+
+ ">");
26+
trace.add("\n");
27+
}
28+
@Override
29+
public void flush() {}
30+
@Override
31+
public void close() {}
32+
});
33+
logger.warning("Logging Warning");
34+
logger.info("Logging Info");
35+
System.out.print(trace);
36+
}
37+
}
38+
/* Output:
39+
[WARNING:, CustomHandler:, main:, <Logging Warning>,
40+
, INFO:, CustomHandler:, main:, <Logging Info>,
41+
]
42+
___[ Error Output ]___
43+
Jul 27, 2016 10:50:39 AM CustomHandler main
44+
WARNING: Logging Warning
45+
Jul 27, 2016 10:50:39 AM CustomHandler main
46+
INFO: Logging Info
47+
*/

test/InfoLogging.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// test/InfoLogging.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {ErrorOutputExpected}
6+
import java.util.logging.*;
7+
8+
public class InfoLogging {
9+
private static Logger logger =
10+
Logger.getLogger("InfoLogging");
11+
public static void main(String[] args) {
12+
logger.info("Logging: INFO-level message");
13+
}
14+
}
15+
/* Output:
16+
___[ Error Output ]___
17+
Jul 27, 2016 10:50:39 AM InfoLogging main
18+
INFO: Logging: INFO-level message
19+
*/

test/InfoLogging2.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// test/InfoLogging2.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// Guaranteeing proper class and method names
6+
// {ErrorOutputExpected}
7+
import java.util.logging.*;
8+
9+
public class InfoLogging2 {
10+
private static Logger logger =
11+
Logger.getLogger("InfoLogging2");
12+
public static void main(String[] args) {
13+
logger.logp(Level.INFO, "InfoLogging2",
14+
"main", "Logging an INFO-level message");
15+
}
16+
}
17+
/* Output:
18+
___[ Error Output ]___
19+
Jul 27, 2016 10:50:40 AM InfoLogging2 main
20+
INFO: Logging an INFO-level message
21+
*/

test/LogToFile.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// test/LogToFile.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {ErrorOutputExpected}
6+
import java.util.logging.*;
7+
8+
public class LogToFile {
9+
private static Logger logger =
10+
Logger.getLogger("LogToFile");
11+
public static void
12+
main(String[] args) throws Exception {
13+
logger.addHandler(
14+
new FileHandler("LogToFile.xml"));
15+
logger.info("A message logged to the file");
16+
}
17+
}
18+
/* Output:
19+
___[ Error Output ]___
20+
Jul 27, 2016 10:50:41 AM LogToFile main
21+
INFO: A message logged to the file
22+
*/

test/LogToFile2.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// test/LogToFile2.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
// {ErrorOutputExpected}
6+
import java.util.logging.*;
7+
8+
public class LogToFile2 {
9+
private static Logger logger =
10+
Logger.getLogger("LogToFile2");
11+
public static void
12+
main(String[] args) throws Exception {
13+
FileHandler logFile =
14+
new FileHandler("LogToFile2.txt");
15+
logFile.setFormatter(new SimpleFormatter());
16+
logger.addHandler(logFile);
17+
logger.info("A message logged to the file");
18+
}
19+
}
20+
/* Output:
21+
___[ Error Output ]___
22+
Jul 27, 2016 10:50:41 AM LogToFile2 main
23+
INFO: A message logged to the file
24+
*/

0 commit comments

Comments
 (0)