Logging section not failing
log4j2.xml is still not being seen, though
This commit is contained in:
parent
a2cb2486f5
commit
abf72d0710
@ -1,102 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,47 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -3,11 +3,11 @@
|
||||
// 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.*;
|
||||
import org.apache.logging.log4j.*;
|
||||
|
||||
public class InfoLogging {
|
||||
private static Logger logger =
|
||||
Logger.getLogger("InfoLogging");
|
||||
private static final Logger logger =
|
||||
LogManager.getLogger(InfoLogging.class);
|
||||
public static void main(String[] args) {
|
||||
logger.info("Logging: INFO-level message");
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,22 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,24 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,184 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -3,35 +3,36 @@
|
||||
// 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;
|
||||
import org.apache.logging.log4j.*;
|
||||
import org.apache.logging.log4j.spi.*;
|
||||
import org.apache.logging.log4j.core.config.Configurator;
|
||||
|
||||
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");
|
||||
lgr = LogManager.getLogger("com"),
|
||||
lgr2 = LogManager.getLogger("com.mindviewinc"),
|
||||
util= LogManager.getLogger("onjava"),
|
||||
test= LogManager.getLogger("com.mindviewinc.test"),
|
||||
rand = LogManager.getLogger("random");
|
||||
private static void logMessages() {
|
||||
lgr.info("com : info");
|
||||
lgr2.info("com.mindviewinc : info");
|
||||
util.info("util : info");
|
||||
test.severe("test : severe");
|
||||
test.fatal("test : fatal");
|
||||
rand.info("random : info");
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
lgr.setLevel(Level.SEVERE);
|
||||
System.out.println("com level: SEVERE");
|
||||
Configurator.setLevel("com", Level.FATAL);
|
||||
System.out.println("com level: FATAL");
|
||||
logMessages();
|
||||
util.setLevel(Level.FINEST);
|
||||
test.setLevel(Level.FINEST);
|
||||
rand.setLevel(Level.FINEST);
|
||||
Configurator.setLevel("onjava", Level.TRACE);
|
||||
Configurator.setLevel("com.mindviewinc.test", Level.TRACE);
|
||||
Configurator.setLevel("random", Level.TRACE);
|
||||
System.out.println(
|
||||
"individual loggers set to FINEST");
|
||||
"individual loggers set to TRACE");
|
||||
logMessages();
|
||||
lgr.setLevel(Level.SEVERE);
|
||||
System.out.println("com level: SEVERE");
|
||||
Configurator.setLevel("com", Level.FATAL);
|
||||
System.out.println("com level: FATAL");
|
||||
logMessages();
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,26 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,60 +0,0 @@
|
||||
// verifying/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>
|
||||
*/
|
@ -1,45 +0,0 @@
|
||||
// verifying/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!
|
||||
*/
|
@ -1,38 +0,0 @@
|
||||
// verifying/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
|
||||
*/
|
@ -1,32 +0,0 @@
|
||||
// verifying/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
|
14
verifying/log4j2.xml
Normal file
14
verifying/log4j2.xml
Normal file
@ -0,0 +1,14 @@
|
||||
// verifying/log4j2.xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console" />
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
Loading…
x
Reference in New Issue
Block a user