我的logging.properties自定义格式化程序不工作



我实际上是在向我的项目添加java日志记录(不能使用其他框架(。我在.war上构建了我的应用程序,并在Weblogic上部署了它,记录器正在使用我的logging.properties配置,除了格式化程序,我不知道应用程序为什么忽略它。

这是我的课,我在那里准备记录器;

public class CtgLogger {
private static final String LOAD_ERROR = "Properties could not be loaded.";
private static final Map<String, Level> LEVEL_MAP;
private static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static {
final InputStream inputStream = CtgLogger.class.getResourceAsStream("/logging.properties");
try {
LogManager.getLogManager().readConfiguration(inputStream);
} catch (Exception e) {
Logger.getAnonymousLogger().severe(LOAD_ERROR);
Logger.getAnonymousLogger().severe(e.getMessage());
}
// and I add the LEVEL_MAP to the logger...

这是我的财产。。。

handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.pattern=logsfolder/CTGLOG_%g.log
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.limit=3000
java.util.logging.FileHandler.count=6
#java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
#If I use the SimpleFormatter, apps goes well with it format. 
java.util.logging.FileHandler.formatter = com.package.my.log.JsonCustomFormatter
#If I use my custom formatter, the weblogic works with a XMLFormatter (default)

我知道.properties正在工作,因为logger正在处理我设置的模式、限制和计数。

PD:如果我用JUnit运行我的应用程序,日志可以用我的自定义格式化程序,但在weblogic上不行!不知道为什么!

Weblogic将有多个类加载器。标准LogManager只能看到通过系统类加载器加载的类。请检查服务器日志中与找不到自定义类有关的错误。如果是这种情况,您必须将格式化程序移动到系统类加载器。否则,您必须使用代码从运行在子类加载器中的web应用程序安装格式化程序。

LogManager.readConfiguration和JDK9及更高版本中使用的替代方法中也存在漏洞。

使用Eclipse和java标准记录器可能会很痛苦。我发现了一些类似Log4J:的输出

"%d{HH:mm:ss,SSS}%-5p%m(%F:%L(在%t%n〃中;在Log4J中:你可以点击参考,你就在那里日志被发布了

21:36:37,9 INFO process model event Digpro2021a/digpro.Digpro(Digpro.java:358) in processModelEvent
21:36:37,9 INFO start polling Digpro2021a/digpro.Digpro(Digpro.java:398) in processEventAutoreload
21:36:37,9 INFO reload now Digpro2021a/digpro.Digpro(Digpro.java:370) in processModelEvent

public class Digpro {
protected static final Logger L = Logger.getLogger("Digpro");
//logger conf
static {
L.setLevel(Level.FINE);
Handler handler = Logger.getLogger("").getHandlers()[0];
handler.setLevel(Level.FINE); // Default console handler
handler.setFormatter(new Formatter() {
@Override
public String format(LogRecord r) {
Date d = new Date(r.getMillis());
String srcClassLong = r.getSourceClassName();
String[] aClass = srcClassLong.split("\$")[0].split("\.");
String srcClass = aClass[aClass.length - 1];
StackTraceElement elem = (new Throwable()).getStackTrace()[7];
int line = elem.getLineNumber();
String modulName = elem.getModuleName();
return String.format("%tH:%tM:%tS,%tl %.7s %s %s/%s(%s.java:%d) in %sn", d, d, d, d, // 
r.getLevel(), r.getMessage(), // LEVEL and message
modulName, srcClassLong, srcClass, line, r.getSourceMethodName()); //ref to click on
}
});
}
...
public static class TestDigpro extends Digpro {
//TESTING: 
@Test
public void testLogFormat() {
L.info("poll info");
L.fine("got fine");
}
}
}

产品:

21:51:20,9 INFO poll info Digpro2021a/digpro.Digpro$TestDigpro(Digpro.java:723) in testLogFormat
21:51:20,9 FINE got fine Digpro2021a/digpro.Digpro$TestDigpro(Digpro.java:724) in testLogFormat

相关内容

最新更新