diff --git a/build.gradle b/build.gradle index b457434a..c3238c84 100644 --- a/build.gradle +++ b/build.gradle @@ -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} ")) diff --git a/test/ConfigureLogging.java b/test/ConfigureLogging.java new file mode 100644 index 00000000..fbf0958d --- /dev/null +++ b/test/ConfigureLogging.java @@ -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 +*/ diff --git a/test/CustomHandler.java b/test/CustomHandler.java new file mode 100644 index 00000000..94cea7f3 --- /dev/null +++ b/test/CustomHandler.java @@ -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 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:, , +, INFO:, CustomHandler:, main:, , +] +___[ 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 +*/ diff --git a/test/InfoLogging.java b/test/InfoLogging.java new file mode 100644 index 00000000..52258727 --- /dev/null +++ b/test/InfoLogging.java @@ -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 +*/ diff --git a/test/InfoLogging2.java b/test/InfoLogging2.java new file mode 100644 index 00000000..1579d796 --- /dev/null +++ b/test/InfoLogging2.java @@ -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 +*/ diff --git a/test/LogToFile.java b/test/LogToFile.java new file mode 100644 index 00000000..3b070998 --- /dev/null +++ b/test/LogToFile.java @@ -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 +*/ diff --git a/test/LogToFile2.java b/test/LogToFile2.java new file mode 100644 index 00000000..f53bf61e --- /dev/null +++ b/test/LogToFile2.java @@ -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 +*/ diff --git a/test/LoggingLevelManipulation.java b/test/LoggingLevelManipulation.java new file mode 100644 index 00000000..ad4f1a32 --- /dev/null +++ b/test/LoggingLevelManipulation.java @@ -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 +*/ diff --git a/test/LoggingLevels.java b/test/LoggingLevels.java new file mode 100644 index 00000000..308aeaee --- /dev/null +++ b/test/LoggingLevels.java @@ -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 +*/ diff --git a/test/MultipleHandlers.java b/test/MultipleHandlers.java new file mode 100644 index 00000000..ee71bb22 --- /dev/null +++ b/test/MultipleHandlers.java @@ -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 +*/ diff --git a/test/MultipleHandlers2.java b/test/MultipleHandlers2.java new file mode 100644 index 00000000..2e714d4e --- /dev/null +++ b/test/MultipleHandlers2.java @@ -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 +*/ diff --git a/test/PrintableLogRecord.java b/test/PrintableLogRecord.java new file mode 100644 index 00000000..a88bb7a4 --- /dev/null +++ b/test/PrintableLogRecord.java @@ -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 += "\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 +LoggerName +Message +CurrentMillis<1469638244376> +Params +ResourceBundle +ResourceBundleName +SequenceNumber<0> +SourceClassName +SourceMethodName +Thread Id<1> +Thrown +*/ diff --git a/test/SimpleFilter.java b/test/SimpleFilter.java new file mode 100644 index 00000000..7c34ce19 --- /dev/null +++ b/test/SimpleFilter.java @@ -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! +*/ diff --git a/test/SimpleFormatterExample.java b/test/SimpleFormatterExample.java new file mode 100644 index 00000000..81f7b83b --- /dev/null +++ b/test/SimpleFormatterExample.java @@ -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 +*/ diff --git a/test/log.prop b/test/log.prop new file mode 100644 index 00000000..c9c33b1b --- /dev/null +++ b/test/log.prop @@ -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