我的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起作用,因为记录器正在使用我设置的模式,限制和计数。

PD:如果我使用JUnit运行我的应用程序,则日志将与我的自定义格式化程序一起使用,但不适用于weblogic!不知道为什么!

stupid113 回答:我的logging.properties自定义格式化程序不起作用

Weblogic将具有多个类加载器。标准LogManager can only see classes loaded via the system class loader。检查Server Log中是否有与找不到自定义类有关的错误。如果是这种情况,则必须将格式程序移动到系统类加载器。否则,您必须使用代码从运行在子类加载器中的Web应用程序安装格式化程序。

还有bugs in the LogManager.readConfiguration and alternative methods to use in JDK9 and later

,

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

“%d{HH:mm:ss,SSS} %t%n 中的 %-5p %m (%F:%L)”在 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 %s\n",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
本文链接:https://www.f2er.com/3154734.html

大家都在问