【问题标题】:My logging.properties custom formatter is not working我的 logging.properties 自定义格式化程序不起作用
【发布时间】:2019-11-06 05:48:15
【问题描述】:

我实际上是在我的项目中添加 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!不知道为什么!

【问题讨论】:

    标签: java logging weblogic formatter


    【解决方案1】:

    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

    【讨论】:

    • 我有一个问题,不知道你是否可以帮助我,如果我从代码中添加格式化程序,当我重新部署我的应用程序时,就像处理程序没有关闭一样,它会生成多个日志。 (它是一个 API REST,我不知道在每次调用时打开和关闭处理程序是否是一个好方法)
    • 你知道如何将我的自定义格式化程序移动到系统类加载器吗?
    • 我解决了更改 log4j2 的 java 日志记录的问题。无论如何,我选择了您的答案,因为它对帮助我了解 LogManager 如何与 weblogic 一起工作非常有用:)
    【解决方案2】:

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

    "%d{HH:mm:ss,SSS} %-5p %m (%F:%L) in %t%n" in 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, 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      相关资源
      最近更新 更多