Temporarily adding "test" directory
This commit is contained in:
parent
79279957dc
commit
e0e7ced3d7
@ -59,7 +59,6 @@ class Tags {
|
||||
private def hasTag(String marker) {
|
||||
return block.contains("// {" + marker + "}")
|
||||
}
|
||||
|
||||
def extractOutputLine() {
|
||||
def matcher = (block =~ /(?m)^(\/\* Output:.*)$/)
|
||||
if (matcher) {
|
||||
@ -68,7 +67,6 @@ class Tags {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private def extract(String marker) {
|
||||
// Assume some whitespace is after marker
|
||||
if(!block.contains("// {${marker} "))
|
||||
|
102
test/ConfigureLogging.java
Normal file
102
test/ConfigureLogging.java
Normal file
@ -0,0 +1,102 @@
|
||||
// test/ConfigureLogging.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {java ConfigureLogging
|
||||
// -Djava.util.logging.config.file=log.prop}
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class ConfigureLogging {
|
||||
static Logger
|
||||
lgr = Logger.getLogger("com"),
|
||||
lgr2 = Logger.getLogger("com.mindviewinc"),
|
||||
util= Logger.getLogger("onjava"),
|
||||
test= Logger.getLogger("com.mindviewinc.test"),
|
||||
rand = Logger.getLogger("random");
|
||||
public ConfigureLogging() {
|
||||
/*
|
||||
Set Additional formatters, Filters and
|
||||
Handlers for the loggers here. You cannot
|
||||
specify the Handlers for loggers except
|
||||
the root logger from the configuration
|
||||
file.
|
||||
*/
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
sendLogMessages(lgr);
|
||||
sendLogMessages(lgr2);
|
||||
sendLogMessages(util);
|
||||
sendLogMessages(test);
|
||||
sendLogMessages(rand);
|
||||
}
|
||||
private static void
|
||||
sendLogMessages(Logger logger) {
|
||||
System.out.println(" Logger Name : "
|
||||
+ logger.getName() + " Level: "
|
||||
+ logger.getLevel());
|
||||
logger.finest("Finest");
|
||||
logger.finer("Finer");
|
||||
logger.fine("Fine");
|
||||
logger.config("Config");
|
||||
logger.info("Info");
|
||||
logger.warning("Warning");
|
||||
logger.severe("Severe");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
Logger Name : com Level: null
|
||||
Logger Name : com.mindviewinc Level: FINEST
|
||||
Logger Name : onjava Level: INFO
|
||||
Logger Name : com.mindviewinc.test Level: FINER
|
||||
Logger Name : random Level: SEVERE
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINEST: Finest
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINER: Finer
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINE: Fine
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
CONFIG: Config
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
INFO: Info
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
WARNING: Warning
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
SEVERE: Severe
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINEST: Finest
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINER: Finer
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINE: Fine
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
CONFIG: Config
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
INFO: Info
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
WARNING: Warning
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
SEVERE: Severe
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
INFO: Info
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
WARNING: Warning
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
SEVERE: Severe
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINER: Finer
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
FINE: Fine
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
CONFIG: Config
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
INFO: Info
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
WARNING: Warning
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
SEVERE: Severe
|
||||
Jul 27, 2016 10:50:38 AM ConfigureLogging sendLogMessages
|
||||
SEVERE: Severe
|
||||
*/
|
47
test/CustomHandler.java
Normal file
47
test/CustomHandler.java
Normal file
@ -0,0 +1,47 @@
|
||||
// test/CustomHandler.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// How to write custom handler
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
import java.util.*;
|
||||
|
||||
public class CustomHandler {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("CustomHandler");
|
||||
private static List<String> trace =
|
||||
new ArrayList<>();
|
||||
public static void main(String[] args) {
|
||||
logger.addHandler(new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord logRecord) {
|
||||
trace.add(logRecord.getLevel() + ":");
|
||||
trace.add(logRecord.getSourceClassName()
|
||||
+ ":");
|
||||
trace.add(
|
||||
logRecord.getSourceMethodName() +":");
|
||||
trace.add("<" + logRecord.getMessage()
|
||||
+ ">");
|
||||
trace.add("\n");
|
||||
}
|
||||
@Override
|
||||
public void flush() {}
|
||||
@Override
|
||||
public void close() {}
|
||||
});
|
||||
logger.warning("Logging Warning");
|
||||
logger.info("Logging Info");
|
||||
System.out.print(trace);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
[WARNING:, CustomHandler:, main:, <Logging Warning>,
|
||||
, INFO:, CustomHandler:, main:, <Logging Info>,
|
||||
]
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:39 AM CustomHandler main
|
||||
WARNING: Logging Warning
|
||||
Jul 27, 2016 10:50:39 AM CustomHandler main
|
||||
INFO: Logging Info
|
||||
*/
|
19
test/InfoLogging.java
Normal file
19
test/InfoLogging.java
Normal file
@ -0,0 +1,19 @@
|
||||
// test/InfoLogging.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class InfoLogging {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("InfoLogging");
|
||||
public static void main(String[] args) {
|
||||
logger.info("Logging: INFO-level message");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:39 AM InfoLogging main
|
||||
INFO: Logging: INFO-level message
|
||||
*/
|
21
test/InfoLogging2.java
Normal file
21
test/InfoLogging2.java
Normal file
@ -0,0 +1,21 @@
|
||||
// test/InfoLogging2.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// Guaranteeing proper class and method names
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class InfoLogging2 {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("InfoLogging2");
|
||||
public static void main(String[] args) {
|
||||
logger.logp(Level.INFO, "InfoLogging2",
|
||||
"main", "Logging an INFO-level message");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:40 AM InfoLogging2 main
|
||||
INFO: Logging an INFO-level message
|
||||
*/
|
22
test/LogToFile.java
Normal file
22
test/LogToFile.java
Normal file
@ -0,0 +1,22 @@
|
||||
// test/LogToFile.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class LogToFile {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("LogToFile");
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
logger.addHandler(
|
||||
new FileHandler("LogToFile.xml"));
|
||||
logger.info("A message logged to the file");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:41 AM LogToFile main
|
||||
INFO: A message logged to the file
|
||||
*/
|
24
test/LogToFile2.java
Normal file
24
test/LogToFile2.java
Normal file
@ -0,0 +1,24 @@
|
||||
// test/LogToFile2.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class LogToFile2 {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("LogToFile2");
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
FileHandler logFile =
|
||||
new FileHandler("LogToFile2.txt");
|
||||
logFile.setFormatter(new SimpleFormatter());
|
||||
logger.addHandler(logFile);
|
||||
logger.info("A message logged to the file");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:41 AM LogToFile2 main
|
||||
INFO: A message logged to the file
|
||||
*/
|
184
test/LoggingLevelManipulation.java
Normal file
184
test/LoggingLevelManipulation.java
Normal file
@ -0,0 +1,184 @@
|
||||
// test/LoggingLevelManipulation.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class LoggingLevelManipulation {
|
||||
private static Logger
|
||||
lgr = Logger.getLogger("com"),
|
||||
lgr2 = Logger.getLogger("com.mindviewinc"),
|
||||
util= Logger.getLogger("onjava"),
|
||||
test= Logger.getLogger("com.mindviewinc.test"),
|
||||
rand = Logger.getLogger("random");
|
||||
static void printLogMessages(Logger logger) {
|
||||
logger.finest(logger.getName() + " Finest");
|
||||
logger.finer(logger.getName() + " Finer");
|
||||
logger.fine(logger.getName() + " Fine");
|
||||
logger.config(logger.getName() + " Config");
|
||||
logger.info(logger.getName() + " Info");
|
||||
logger.warning(logger.getName()+" Warning");
|
||||
logger.severe(logger.getName() + " Severe");
|
||||
}
|
||||
static void logMessages() {
|
||||
printLogMessages(lgr);
|
||||
printLogMessages(lgr2);
|
||||
printLogMessages(util);
|
||||
printLogMessages(test);
|
||||
printLogMessages(rand);
|
||||
}
|
||||
static void printLevels() {
|
||||
System.out.println(" -- printing levels -- "
|
||||
+ lgr.getName()
|
||||
+ " : " + lgr.getLevel()
|
||||
+ " " + lgr2.getName()
|
||||
+ " : " + lgr2.getLevel()
|
||||
+ " " + util.getName()
|
||||
+ " : " + util.getLevel()
|
||||
+ " " + test.getName()
|
||||
+ " : " + test.getLevel()
|
||||
+ " " + rand.getName()
|
||||
+ " : " + rand.getLevel());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
printLevels();
|
||||
lgr.setLevel(Level.SEVERE);
|
||||
printLevels();
|
||||
System.out.println("net level: SEVERE");
|
||||
logMessages();
|
||||
util.setLevel(Level.FINEST);
|
||||
test.setLevel(Level.FINEST);
|
||||
rand.setLevel(Level.FINEST);
|
||||
printLevels();
|
||||
System.out.println(
|
||||
"individual loggers set to FINEST");
|
||||
logMessages();
|
||||
lgr.setLevel(Level.FINEST);
|
||||
printLevels();
|
||||
System.out.println("net level: FINEST");
|
||||
logMessages();
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
-- printing levels -- com : null com.mindviewinc : null
|
||||
onjava : null com.mindviewinc.test : null random : null
|
||||
-- printing levels -- com : SEVERE com.mindviewinc : null
|
||||
onjava : null com.mindviewinc.test : null random : null
|
||||
net level: SEVERE
|
||||
-- printing levels -- com : SEVERE com.mindviewinc : null
|
||||
onjava : FINEST com.mindviewinc.test : FINEST random :
|
||||
FINEST
|
||||
individual loggers set to FINEST
|
||||
-- printing levels -- com : FINEST com.mindviewinc : null
|
||||
onjava : FINEST com.mindviewinc.test : FINEST random :
|
||||
FINEST
|
||||
net level: FINEST
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com.mindviewinc Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: onjava Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: onjava Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: onjava Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com.mindviewinc.test Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: random Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: random Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: random Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com.mindviewinc Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: onjava Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: onjava Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: onjava Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: com.mindviewinc.test Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: com.mindviewinc.test Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com.mindviewinc.test Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: random Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: random Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: random Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: com Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: com Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: com.mindviewinc Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: com.mindviewinc Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com.mindviewinc Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: onjava Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: onjava Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: onjava Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: com.mindviewinc.test Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: com.mindviewinc.test Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: com.mindviewinc.test Severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
INFO: random Info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
WARNING: random Warning
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevelManipulation
|
||||
printLogMessages
|
||||
SEVERE: random Severe
|
||||
*/
|
61
test/LoggingLevels.java
Normal file
61
test/LoggingLevels.java
Normal file
@ -0,0 +1,61 @@
|
||||
// test/LoggingLevels.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class LoggingLevels {
|
||||
private static Logger
|
||||
lgr = Logger.getLogger("com"),
|
||||
lgr2 = Logger.getLogger("com.mindviewinc"),
|
||||
util= Logger.getLogger("onjava"),
|
||||
test= Logger.getLogger("com.mindviewinc.test"),
|
||||
rand = Logger.getLogger("random");
|
||||
private static void logMessages() {
|
||||
lgr.info("com : info");
|
||||
lgr2.info("com.mindviewinc : info");
|
||||
util.info("util : info");
|
||||
test.severe("test : severe");
|
||||
rand.info("random : info");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
lgr.setLevel(Level.SEVERE);
|
||||
System.out.println("com level: SEVERE");
|
||||
logMessages();
|
||||
util.setLevel(Level.FINEST);
|
||||
test.setLevel(Level.FINEST);
|
||||
rand.setLevel(Level.FINEST);
|
||||
System.out.println(
|
||||
"individual loggers set to FINEST");
|
||||
logMessages();
|
||||
lgr.setLevel(Level.SEVERE);
|
||||
System.out.println("com level: SEVERE");
|
||||
logMessages();
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
com level: SEVERE
|
||||
individual loggers set to FINEST
|
||||
com level: SEVERE
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
INFO: util : info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
SEVERE: test : severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
INFO: random : info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
INFO: util : info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
SEVERE: test : severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
INFO: random : info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
INFO: util : info
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
SEVERE: test : severe
|
||||
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
|
||||
INFO: random : info
|
||||
*/
|
27
test/MultipleHandlers.java
Normal file
27
test/MultipleHandlers.java
Normal file
@ -0,0 +1,27 @@
|
||||
// test/MultipleHandlers.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class MultipleHandlers {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("MultipleHandlers");
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
FileHandler logFile =
|
||||
new FileHandler("MultipleHandlers.xml");
|
||||
logger.addHandler(logFile);
|
||||
logger.addHandler(new ConsoleHandler());
|
||||
logger.warning(
|
||||
"Output to multiple handlers");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:43 AM MultipleHandlers main
|
||||
WARNING: Output to multiple handlers
|
||||
Jul 27, 2016 10:50:43 AM MultipleHandlers main
|
||||
WARNING: Output to multiple handlers
|
||||
*/
|
26
test/MultipleHandlers2.java
Normal file
26
test/MultipleHandlers2.java
Normal file
@ -0,0 +1,26 @@
|
||||
// test/MultipleHandlers2.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class MultipleHandlers2 {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("MultipleHandlers2");
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
FileHandler logFile =
|
||||
new FileHandler("MultipleHandlers2.xml");
|
||||
logger.addHandler(logFile);
|
||||
logger.addHandler(new ConsoleHandler());
|
||||
logger.setUseParentHandlers(false);
|
||||
logger.warning(
|
||||
"Output to multiple handlers");
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:43 AM MultipleHandlers2 main
|
||||
WARNING: Output to multiple handlers
|
||||
*/
|
60
test/PrintableLogRecord.java
Normal file
60
test/PrintableLogRecord.java
Normal file
@ -0,0 +1,60 @@
|
||||
// test/PrintableLogRecord.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// Override LogRecord toString()
|
||||
import java.util.logging.*;
|
||||
|
||||
public class PrintableLogRecord extends LogRecord {
|
||||
public PrintableLogRecord(Level level, String str) {
|
||||
super(level, str);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
String result = "Level<" + getLevel()+ ">\n"
|
||||
+ "LoggerName<" + getLoggerName() + ">\n"
|
||||
+ "Message<" + getMessage() + ">\n"
|
||||
+ "CurrentMillis<" + getMillis() + ">\n"
|
||||
+ "Params";
|
||||
Object[] objParams = getParameters();
|
||||
if(objParams == null)
|
||||
result += "<null>\n";
|
||||
else
|
||||
for(int i = 0; i < objParams.length; i++)
|
||||
result += " Param # <" + i + " value "+
|
||||
objParams[i].toString() + ">\n";
|
||||
result += "ResourceBundle<"
|
||||
+ getResourceBundle()
|
||||
+ ">\nResourceBundleName<"
|
||||
+ getResourceBundleName()
|
||||
+ ">\nSequenceNumber<"
|
||||
+ getSequenceNumber()
|
||||
+ ">\nSourceClassName<"
|
||||
+ getSourceClassName()
|
||||
+ ">\nSourceMethodName<"
|
||||
+ getSourceMethodName()
|
||||
+ ">\nThread Id<" + getThreadID()
|
||||
+ ">\nThrown<" + getThrown() + ">";
|
||||
return result;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
PrintableLogRecord logRecord =
|
||||
new PrintableLogRecord(
|
||||
Level.FINEST, "Simple Log Record");
|
||||
System.out.println(logRecord);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
Level<FINEST>
|
||||
LoggerName<null>
|
||||
Message<Simple Log Record>
|
||||
CurrentMillis<1469638244376>
|
||||
Params<null>
|
||||
ResourceBundle<null>
|
||||
ResourceBundleName<null>
|
||||
SequenceNumber<0>
|
||||
SourceClassName<null>
|
||||
SourceMethodName<null>
|
||||
Thread Id<1>
|
||||
Thrown<null>
|
||||
*/
|
45
test/SimpleFilter.java
Normal file
45
test/SimpleFilter.java
Normal file
@ -0,0 +1,45 @@
|
||||
// test/SimpleFilter.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class SimpleFilter {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("SimpleFilter");
|
||||
static class Duck {};
|
||||
static class Wombat {};
|
||||
static void sendLogMessages() {
|
||||
logger.log(Level.WARNING,
|
||||
"A duck in the house!", new Duck());
|
||||
logger.log(Level.WARNING,
|
||||
"A Wombat at large!", new Wombat());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
sendLogMessages();
|
||||
logger.setFilter(record -> {
|
||||
Object[] params =
|
||||
record.getParameters();
|
||||
if(params == null)
|
||||
return true; // No parameters
|
||||
if(record.getParameters()[0]
|
||||
instanceof Duck)
|
||||
return true; // Only log Ducks
|
||||
return false;
|
||||
});
|
||||
logger.info("After setting filter..");
|
||||
sendLogMessages();
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
Jul 27, 2016 10:50:44 AM SimpleFilter sendLogMessages
|
||||
WARNING: A duck in the house!
|
||||
Jul 27, 2016 10:50:44 AM SimpleFilter sendLogMessages
|
||||
WARNING: A Wombat at large!
|
||||
Jul 27, 2016 10:50:44 AM SimpleFilter main
|
||||
INFO: After setting filter..
|
||||
Jul 27, 2016 10:50:44 AM SimpleFilter sendLogMessages
|
||||
WARNING: A duck in the house!
|
||||
*/
|
38
test/SimpleFormatterExample.java
Normal file
38
test/SimpleFormatterExample.java
Normal file
@ -0,0 +1,38 @@
|
||||
// test/SimpleFormatterExample.java
|
||||
// (c)2016 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||
// {ErrorOutputExpected}
|
||||
import java.util.logging.*;
|
||||
|
||||
public class SimpleFormatterExample {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("SimpleFormatterExample");
|
||||
private static void logMessages() {
|
||||
logger.info("Line One");
|
||||
logger.info("Line Two");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
logger.setUseParentHandlers(false);
|
||||
Handler conHdlr = new ConsoleHandler();
|
||||
conHdlr.setFormatter(new Formatter() {
|
||||
public String format(LogRecord record) {
|
||||
return record.getLevel() + " : "
|
||||
+ record.getSourceClassName()
|
||||
+ " -:- "
|
||||
+ record.getSourceMethodName()
|
||||
+ " -:- "
|
||||
+ record.getMessage() + "\n";
|
||||
}
|
||||
});
|
||||
logger.addHandler(conHdlr);
|
||||
logMessages();
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
___[ Error Output ]___
|
||||
INFO : SimpleFormatterExample -:- logMessages -:- Line
|
||||
One
|
||||
INFO : SimpleFormatterExample -:- logMessages -:- Line
|
||||
Two
|
||||
*/
|
32
test/log.prop
Normal file
32
test/log.prop
Normal file
@ -0,0 +1,32 @@
|
||||
// test/log.prop
|
||||
#### Configuration File ####
|
||||
# Global Params
|
||||
# Handlers installed for the root logger
|
||||
handlers= java.util.logging.ConsoleHandler java.util.logging.FileHandler
|
||||
# Level for root logger--is used by any logger
|
||||
# that does not have its level set
|
||||
.level= FINEST
|
||||
# Initialization class--the public no-arg constructor
|
||||
# of this class is called by the Logging framework
|
||||
config = ConfigureLogging
|
||||
|
||||
# Configure FileHandler
|
||||
# Logging file name - %u specifies unique
|
||||
java.util.logging.FileHandler.pattern = java%g.log
|
||||
# Write 100000 bytes before rotating this file
|
||||
java.util.logging.FileHandler.limit = 100000
|
||||
# Number of rotating files
|
||||
java.util.logging.FileHandler.count = 3
|
||||
# Formatter for this FileHandler
|
||||
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
|
||||
|
||||
# Configure ConsoleHandler
|
||||
java.util.logging.ConsoleHandler.level = FINEST
|
||||
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
|
||||
|
||||
# Set Logger Levels #
|
||||
net.level=SEVERE
|
||||
com.mindviewinc.level = FINEST
|
||||
onjava.level = INFO
|
||||
com.mindviewinc.test.level = FINER
|
||||
random.level= SEVERE
|
Loading…
x
Reference in New Issue
Block a user