gpt4 book ai didi

java - 日志记录不显示

转载 作者:行者123 更新时间:2023-11-30 02:28:34 25 4
gpt4 key购买 nike

我在我的应用程序中使用 JUL。通常,Netbeans 会打开一个输出选项卡,其中显示“Tomcat”并显示我生成的日志。一切正常。但突然,我意识到我的日志根本没有显示,只有 System.out 被打印。甚至连最高级的 LOG.log(Level.SEVERE, ".....

} catch(Exception e) {
System.out.println("This gets printed in Netbeans tab");
LOG.log(Level.SEVERE, "This doesnt");
}

我怀疑它可能是我包含的一个库,它扰乱了我的日志。这可能吗?库可以改变我的日志的显示方式吗?由于我有点迷失,我该如何调查这个问题?

最佳答案

I suspect it can be a library I included, which is messing with my logs. Is that possible at all?

是的。 JUL to SLF4J Bridge可以从 JUL 根记录器中删除控制台处理程序。某些库调用 LogManager.reset 可以删除并关闭所有处理程序。

Can a library change the way my logs are shown?

是的。一旦安装了日志桥,JUL 记录就不再由 JUL 格式化程序格式化。

How can I investigate this, since I am a bit lost?

修改记录器树需要权限,以便您可以安装 SecurityManager具有所有权限,但然后使用 -Djava.security.debug="access,stack" 打开调试跟踪确定正在修改记录器树的调用者。

如果这不起作用,您可以使用良好的 ole' System.out 在加载库之前和之后打印记录器树和附加的处理程序。然后开始添加删除库,直到看到记录器发生变化。

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class DebugLogging {

private static final Logger log = Logger.getLogger("test");

public static void main(String[] a) {
log.log(Level.FINEST, "Finest");
log.log(Level.FINER, "FINER");
log.log(Level.FINE, "FINE");
log.log(Level.CONFIG, "CONFIG");
log.log(Level.INFO, "INFO");
log.log(Level.WARNING, "WARNING");
log.log(Level.SEVERE, "SEVERE");
log.finest("Finest Log");
log.finer("Finer Log");
log.fine("Fine Log");
log.config("Config Log");
log.info("Info Log");
log.warning("Warning Log");
log.severe("Severe Log");
printConfig(System.err);
}

private static void printConfig(PrintStream ps) {
String cname = System.getProperty("java.util.logging.config.class");
if (cname != null) {
try {
ClassLoader sys = ClassLoader.getSystemClassLoader();
Class<?> c = Class.forName(cname, false, sys);
ps.println(sys.getClass().getName() +" found log configuration class " + c.getName());
} catch (LinkageError | ClassNotFoundException | RuntimeException cnfe) {
ps.println("Unable to load " + cname);
cnfe.printStackTrace(ps);
}
} else {
ps.println("java.util.logging.config.class was null");
}

String file = System.getProperty("java.util.logging.config.file");
if (file != null) {
ps.println("java.util.logging.config.file=" + file);
try {
ps.println("CanonicalPath=" + new File(file).getCanonicalPath());
} catch (RuntimeException | IOException ioe) {
ps.println("Unable to resolve path for " + file);
ioe.printStackTrace(ps);
}

try {
Path p = Paths.get(file);
if (Files.isReadable(p)) {
ps.println(file + " is readable and has size " + Files.size(p));
} else {
if (Files.exists(p)) {
ps.println(file + " exists for " + System.getProperty("user.name") + " but is not readable.");
} else {
ps.println(file + " doesn't exist for " + System.getProperty("user.name"));
}
}
} catch (RuntimeException | IOException ioe) {
ps.println("Unable to read " + file);
ioe.printStackTrace(ps);
}
} else {
ps.println("java.util.logging.config.file was null");
}

LogManager lm = LogManager.getLogManager();
ps.append("LogManager=").println(lm.getClass().getName());
synchronized (lm) {
Enumeration<String> e = lm.getLoggerNames();
while (e.hasMoreElements()) {
Logger l = lm.getLogger(e.nextElement());
if (l != null) {
print(l, ps);
}
}
}
}

private static void print(Logger l, PrintStream ps) {
String scn = l.getClass().getSimpleName();
ps.append("scn=").append(scn).append(", n=").append(l.getName())
.append(", uph=").append(String.valueOf(l.getUseParentHandlers()))
.append(", l=").append(String.valueOf(l.getLevel()))
.append(", fl=").println(l.getFilter());
for (Handler h : l.getHandlers()) {
ps.append("\t").append(l.getName()).append("->")
.append(h.getClass().getName()).append(", h=")
.append(String.valueOf(h.getLevel())).append(", fl=")
.append(String.valueOf(h.getFilter())).println();
}
}
}

关于java - 日志记录不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44882648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com