In plain Java SE 6 environment:
在普通的Java SE 6环境中:
Logger l = Logger.getLogger("nameless");
l.setLevel(Level.ALL);
l.fine("somemessage");
Nothing shows up in Eclipse console. l.info("") and above works just fine, but anything below fine just doesn't seem to work. What's could be wrong? TIA.
Eclipse控制台中没有任何内容。 l.info(“”)及以上工作正常,但是任何低于罚款的东西似乎都不起作用。什么可能是错的? TIA。
6 个解决方案
#1
54
Even though the Logger level is set to ALL, the ConsoleHandler (the default Handler on the logger) still has a default level of INFO. This comes from the default logging.properties in JAVA_HOME/jre/lib
即使Logger级别设置为ALL,ConsoleHandler(记录器上的默认处理程序)仍具有默认级别INFO。这来自JAVA_HOME / jre / lib中的默认logging.properties
#2
34
Instead of looping through all handlers and set the logging level, I prefer to set only the level of the console handler:
我宁愿只设置控制台处理程序的级别,而不是遍历所有处理程序并设置日志记录级别:
//get the top Logger
Logger topLogger = java.util.logging.Logger.getLogger("");
// Handler for console (reuse it if it already exists)
Handler consoleHandler = null;
//see if there is already a console handler
for (Handler handler : topLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
//found the console handler
consoleHandler = handler;
break;
}
}
if (consoleHandler == null) {
//there was no console handler found, create a new one
consoleHandler = new ConsoleHandler();
topLogger.addHandler(consoleHandler);
}
//set the console handler to fine:
consoleHandler.setLevel(java.util.logging.Level.FINEST);
#3
16
An indivual at my workplace found the following to work:
在我的工作场所的个人发现以下工作:
public class Foo {
private final static Logger logger = Logger.getLogger(Foo.class.getName());
public static final void main(String[] args) {
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.FINEST);
Foo.logger.addHandler(ch);
Foo.logger.setLevel(Level.FINEST);
Foo.logger.finest("test");
}
}
If you just set the root or the handler to finest (exclusively) then it didn't work. When I set both to FINEST
then it works. His explanation was:
如果你只是将root或处理程序设置为最好(独占),那么它就不起作用了。当我将两者都设置为FINEST然后它可以工作。他的解释是:
Both the logger and its handlers have Log Levels… The order of filtering is Logger then Handlers. That means it checks to see if the log message passes the loggers filter first, then sends the message on to the individual handlers for filtering.
记录器及其处理程序都具有日志级别...过滤的顺序是Logger然后是Handlers。这意味着它会检查日志消息是否先通过记录器过滤器,然后将消息发送给各个处理程序进行过滤。
He further explained it using the following examples:
他使用以下示例进一步解释了它:
-
Logger myLogger
has a level ofFINEST
and a singleConsoleHandler myHandler
which has a level ofINFO
Logger myLogger具有FINEST级别和单个ConsoleHandler myHandler,其级别为INFO
-
myLogger.fine("foo")
à message makes it past the logger’s filter, but gets stopper by the handler’s filter… Nothing output.myLogger.fine(“foo”)à消息使它超过记录器的过滤器,但是由处理程序的过滤器获取停止...没有输出。
-
myLogger.info("foo")
à passes both filters andfoo
is output.myLogger.info(“foo”)à传递两个过滤器并输出foo。
Now…
现在…
-
Logger myLogger
has a level ofINFO
and a singleConsoleHandler myHandler
which has a level ofFINEST
Logger myLogger具有一定级别的INFO和一个具有FINEST级别的ConsoleHandler myHandler
-
myLogger.fine("foo")
à message gets stopped by the logger’s filter and never makes it to the handler... Nothing output.myLogger.fine(“foo”)à消息被记录器的过滤器停止,永远不会进入处理程序......没有输出。
-
myLogger.info("foo")
à passes both filters andfoo
is output.myLogger.info(“foo”)à传递两个过滤器并输出foo。
Now…
现在…
-
Logger myLogger
has a level ofFINEST
and a singleConsoleHandler myHandler
which has a level ofFINEST
Logger myLogger具有FINEST级别和单个ConsoleHandler myHandler,其级别为FINEST
-
myLogger.fine("foo")
à passes both filters and "foo
" is output.myLogger.fine(“foo”)à传递两个过滤器并输出“foo”。
-
myLogger.info("foo")
à passes both filters andfoo
is output.myLogger.info(“foo”)à传递两个过滤器并输出foo。
#4
2
Other users have already given good answer why it happened (ConsoleHandler has a separate level variable). I reuse my application logger's level and copy it up to the parent hiearchy. Also provides easy way to refresh levels at runtime anytime I want.
其他用户已经给出了很好的答案(ConsoleHandler有一个单独的级别变量)。我重用了我的应用程序记录器的级别并将其复制到父级别。还提供了在我想要的任何时候在运行时刷新级别的简便方法。
// Set same level all loggers and handlers up to the parent level
// OFF,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST,ALL
Logger logger = Logger.getLogger(this.getClass().getPackage().getName());
//Level level = Level.parse("FINEST");
Level level = logger.getLevel();
Logger tempLogger = logger;
while(tempLogger != null) {
tempLogger.setLevel(level);
for(Handler handler : tempLogger.getHandlers())
handler.setLevel(level);
tempLogger = tempLogger.getParent();
}
#5
2
You need to set the log level on both the handlers in the logger, and the logger itself. Logging is only performed at the "coarsest" of the two levels. Here is a logging class that does the job.
您需要在记录器中的处理程序和记录器本身上设置日志级别。记录仅在两个级别的“最粗糙”中执行。这是一个完成工作的日志类。
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class Log {
private static final Logger logger = Logger.getGlobal();
private static Level logLevel = Level.INFO;
static {
// Remove all the default handlers (usually just one console handler)
Logger rootLogger = Logger.getLogger("");
Handler[] rootHandlers = rootLogger.getHandlers();
for (Handler handler : rootHandlers) {
rootLogger.removeHandler(handler);
}
// Add our own handler
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(logLevel);
handler.setFormatter(new LogFormatter());
logger.addHandler(handler);
logger.setLevel(logLevel);
}
public static class LogFormatter extends Formatter {
@Override
public String format(LogRecord record) {
String stackTrace = "";
Throwable thrown = record.getThrown();
if (thrown != null) {
StringWriter stacktraceWriter = new StringWriter();
try (PrintWriter writer = new PrintWriter(stacktraceWriter)) {
thrown.printStackTrace(writer);
}
stackTrace = stacktraceWriter.toString();
}
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(record.getMillis()), ZoneId.of("UTC")).format(DateTimeFormatter.ISO_ZONED_DATE_TIME) + "\t" + record.getLevel()
+ "\t" + record.getMessage() + "\n" + stackTrace;
}
}
private static final String classname = Log.class.getName();
private static String callerRef() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if (stackTraceElements.length < 4) {
return "";
} else {
int i = 1;
for (; i < stackTraceElements.length; i++) {
if (stackTraceElements[i].getClassName().equals(classname)) {
break;
}
}
for (; i < stackTraceElements.length; i++) {
if (!stackTraceElements[i].getClassName().equals(classname)) {
break;
}
}
if (i < stackTraceElements.length) {
return stackTraceElements[i].toString();
} else {
return "[in unknown method]";
}
}
}
public static void setLogLevel(Level newLogLevel) {
logLevel = newLogLevel;
for (Handler handler : logger.getHandlers()) {
handler.setLevel(newLogLevel);
}
Log.logger.setLevel(newLogLevel);
}
public static int getLevelNum() {
return logLevel.intValue();
}
public static int getLevelNum(Level level) {
return level.intValue();
}
public static void fine(String msg) {
logger.log(Level.FINE, msg);
}
public static void info(String msg) {
logger.log(Level.INFO, msg);
}
public static void warning(String msg) {
logger.log(Level.WARNING, msg + "\t " + callerRef());
}
public static void error(String msg) {
logger.log(Level.SEVERE, msg + "\t " + callerRef());
}
public static void exception(String msg, Throwable cause) {
logger.log(Level.SEVERE, msg + "\t " + callerRef(), cause);
}
}
#6
1
private final static Logger LOGGER = Logger.getLogger(WoTServer.class.getName());
for(Handler h : LOGGER.getParent().getHandlers()){
if(h instanceof ConsoleHandler){
h.setLevel(Level.ALL);
}
}
#1
54
Even though the Logger level is set to ALL, the ConsoleHandler (the default Handler on the logger) still has a default level of INFO. This comes from the default logging.properties in JAVA_HOME/jre/lib
即使Logger级别设置为ALL,ConsoleHandler(记录器上的默认处理程序)仍具有默认级别INFO。这来自JAVA_HOME / jre / lib中的默认logging.properties
#2
34
Instead of looping through all handlers and set the logging level, I prefer to set only the level of the console handler:
我宁愿只设置控制台处理程序的级别,而不是遍历所有处理程序并设置日志记录级别:
//get the top Logger
Logger topLogger = java.util.logging.Logger.getLogger("");
// Handler for console (reuse it if it already exists)
Handler consoleHandler = null;
//see if there is already a console handler
for (Handler handler : topLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
//found the console handler
consoleHandler = handler;
break;
}
}
if (consoleHandler == null) {
//there was no console handler found, create a new one
consoleHandler = new ConsoleHandler();
topLogger.addHandler(consoleHandler);
}
//set the console handler to fine:
consoleHandler.setLevel(java.util.logging.Level.FINEST);
#3
16
An indivual at my workplace found the following to work:
在我的工作场所的个人发现以下工作:
public class Foo {
private final static Logger logger = Logger.getLogger(Foo.class.getName());
public static final void main(String[] args) {
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.FINEST);
Foo.logger.addHandler(ch);
Foo.logger.setLevel(Level.FINEST);
Foo.logger.finest("test");
}
}
If you just set the root or the handler to finest (exclusively) then it didn't work. When I set both to FINEST
then it works. His explanation was:
如果你只是将root或处理程序设置为最好(独占),那么它就不起作用了。当我将两者都设置为FINEST然后它可以工作。他的解释是:
Both the logger and its handlers have Log Levels… The order of filtering is Logger then Handlers. That means it checks to see if the log message passes the loggers filter first, then sends the message on to the individual handlers for filtering.
记录器及其处理程序都具有日志级别...过滤的顺序是Logger然后是Handlers。这意味着它会检查日志消息是否先通过记录器过滤器,然后将消息发送给各个处理程序进行过滤。
He further explained it using the following examples:
他使用以下示例进一步解释了它:
-
Logger myLogger
has a level ofFINEST
and a singleConsoleHandler myHandler
which has a level ofINFO
Logger myLogger具有FINEST级别和单个ConsoleHandler myHandler,其级别为INFO
-
myLogger.fine("foo")
à message makes it past the logger’s filter, but gets stopper by the handler’s filter… Nothing output.myLogger.fine(“foo”)à消息使它超过记录器的过滤器,但是由处理程序的过滤器获取停止...没有输出。
-
myLogger.info("foo")
à passes both filters andfoo
is output.myLogger.info(“foo”)à传递两个过滤器并输出foo。
Now…
现在…
-
Logger myLogger
has a level ofINFO
and a singleConsoleHandler myHandler
which has a level ofFINEST
Logger myLogger具有一定级别的INFO和一个具有FINEST级别的ConsoleHandler myHandler
-
myLogger.fine("foo")
à message gets stopped by the logger’s filter and never makes it to the handler... Nothing output.myLogger.fine(“foo”)à消息被记录器的过滤器停止,永远不会进入处理程序......没有输出。
-
myLogger.info("foo")
à passes both filters andfoo
is output.myLogger.info(“foo”)à传递两个过滤器并输出foo。
Now…
现在…
-
Logger myLogger
has a level ofFINEST
and a singleConsoleHandler myHandler
which has a level ofFINEST
Logger myLogger具有FINEST级别和单个ConsoleHandler myHandler,其级别为FINEST
-
myLogger.fine("foo")
à passes both filters and "foo
" is output.myLogger.fine(“foo”)à传递两个过滤器并输出“foo”。
-
myLogger.info("foo")
à passes both filters andfoo
is output.myLogger.info(“foo”)à传递两个过滤器并输出foo。
#4
2
Other users have already given good answer why it happened (ConsoleHandler has a separate level variable). I reuse my application logger's level and copy it up to the parent hiearchy. Also provides easy way to refresh levels at runtime anytime I want.
其他用户已经给出了很好的答案(ConsoleHandler有一个单独的级别变量)。我重用了我的应用程序记录器的级别并将其复制到父级别。还提供了在我想要的任何时候在运行时刷新级别的简便方法。
// Set same level all loggers and handlers up to the parent level
// OFF,SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST,ALL
Logger logger = Logger.getLogger(this.getClass().getPackage().getName());
//Level level = Level.parse("FINEST");
Level level = logger.getLevel();
Logger tempLogger = logger;
while(tempLogger != null) {
tempLogger.setLevel(level);
for(Handler handler : tempLogger.getHandlers())
handler.setLevel(level);
tempLogger = tempLogger.getParent();
}
#5
2
You need to set the log level on both the handlers in the logger, and the logger itself. Logging is only performed at the "coarsest" of the two levels. Here is a logging class that does the job.
您需要在记录器中的处理程序和记录器本身上设置日志级别。记录仅在两个级别的“最粗糙”中执行。这是一个完成工作的日志类。
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class Log {
private static final Logger logger = Logger.getGlobal();
private static Level logLevel = Level.INFO;
static {
// Remove all the default handlers (usually just one console handler)
Logger rootLogger = Logger.getLogger("");
Handler[] rootHandlers = rootLogger.getHandlers();
for (Handler handler : rootHandlers) {
rootLogger.removeHandler(handler);
}
// Add our own handler
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(logLevel);
handler.setFormatter(new LogFormatter());
logger.addHandler(handler);
logger.setLevel(logLevel);
}
public static class LogFormatter extends Formatter {
@Override
public String format(LogRecord record) {
String stackTrace = "";
Throwable thrown = record.getThrown();
if (thrown != null) {
StringWriter stacktraceWriter = new StringWriter();
try (PrintWriter writer = new PrintWriter(stacktraceWriter)) {
thrown.printStackTrace(writer);
}
stackTrace = stacktraceWriter.toString();
}
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(record.getMillis()), ZoneId.of("UTC")).format(DateTimeFormatter.ISO_ZONED_DATE_TIME) + "\t" + record.getLevel()
+ "\t" + record.getMessage() + "\n" + stackTrace;
}
}
private static final String classname = Log.class.getName();
private static String callerRef() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if (stackTraceElements.length < 4) {
return "";
} else {
int i = 1;
for (; i < stackTraceElements.length; i++) {
if (stackTraceElements[i].getClassName().equals(classname)) {
break;
}
}
for (; i < stackTraceElements.length; i++) {
if (!stackTraceElements[i].getClassName().equals(classname)) {
break;
}
}
if (i < stackTraceElements.length) {
return stackTraceElements[i].toString();
} else {
return "[in unknown method]";
}
}
}
public static void setLogLevel(Level newLogLevel) {
logLevel = newLogLevel;
for (Handler handler : logger.getHandlers()) {
handler.setLevel(newLogLevel);
}
Log.logger.setLevel(newLogLevel);
}
public static int getLevelNum() {
return logLevel.intValue();
}
public static int getLevelNum(Level level) {
return level.intValue();
}
public static void fine(String msg) {
logger.log(Level.FINE, msg);
}
public static void info(String msg) {
logger.log(Level.INFO, msg);
}
public static void warning(String msg) {
logger.log(Level.WARNING, msg + "\t " + callerRef());
}
public static void error(String msg) {
logger.log(Level.SEVERE, msg + "\t " + callerRef());
}
public static void exception(String msg, Throwable cause) {
logger.log(Level.SEVERE, msg + "\t " + callerRef(), cause);
}
}
#6
1
private final static Logger LOGGER = Logger.getLogger(WoTServer.class.getName());
for(Handler h : LOGGER.getParent().getHandlers()){
if(h instanceof ConsoleHandler){
h.setLevel(Level.ALL);
}
}