【问题标题】:how to suppress MyFaces console html warnings?如何抑制 MyFaces 控制台 html 警告?
【发布时间】:2013-03-16 21:38:47
【问题描述】:

我们正准备尽快将我们的 JSF 2(MyFaces with Facelets 页面)应用程序迁移到生产环境。目前,我们的控制台日志(WebSphere v8 上的 SystemOut.log)充满了大量此类消息:

[3/26/13 16:42:33:744 CDT] 00000031 HtmlImageRend W   Component UIGraphic Form:errorIconSave has no attribute alt or attribute resolves to null. Path to component {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /view/groupagreement/products/volumebased/VolumeBasedProducts.xhtml][Class: javax.faces.component.html.HtmlBody,Id: commonLayoutId][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id1364021679_785179b][Class: javax.faces.component.html.HtmlForm,Id: Form][Class: javax.faces.component.html.HtmlBody,Id: j_id363369746_1d362e8b][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id363369746_1d362e61][Class: org.richfaces.component.UIRegion,Id: j_id363369746_1d362e4a][Class: org.richfaces.component.UIPopupPanel,Id: confirmationPopUpForWayFinder][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id363369746_1d362ffd][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id363369746_1d362fbc][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id363369746_1d362f9a][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id363369746_1d362f70][Class: javax.faces.component.html.HtmlGraphicImage,Id: errorIconSave]}
[3/26/13 16:42:33:746 CDT] 00000031 HtmlResponseW W   HTML nesting warning on closing div: element td rendered by component : {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /view/groupagreement/products/volumebased/VolumeBasedProducts.xhtml][Class: javax.faces.component.html.HtmlBody,Id: commonLayoutId][Class: javax.faces.component.html.HtmlPanelGrid,Id: j_id1364021679_785179b]} not explicitly closed

我们认为这种过多的日志记录可能会损害性能。虽然我们知道我们应该将我们的应用程序正确地编码为 html 规范,但它是由离岸供应商提供的,我们对代码质量没有我们想要的那样多的控制。此时我们可能没有时间修复所有的 xhtml 文件(将 alt 属性添加到图像等)。

有什么方法可以禁用此日志记录吗?例如 web.xml 上下文参数?我在 MyFaces 文档中找不到任何内容。

【问题讨论】:

  • 我应该注意,我们使用 slf4j 和 log4j 实现。要控制 MyFaces 日志记录,我们是否需要根据 this myfaces question 中的一些答案将 java.util.logging 重定向到 slf4j?
  • 经过进一步调查,SLF4J 的 jul-to-slf4j 桥似乎不是我们想要的,因为即使关闭日志语句也会影响性能。 (见performance note here)

标签: jsf-2 facelets myfaces


【解决方案1】:

我自己解决了这个问题。

我查看了生成这些警告的 MyFaces 组件的源代码 (e.g. grepcode source link for HtmlResponseWriterImpl)。从源代码很明显,在打印这些警告之前没有检查其他配置参数。但当然,正在检查 java.util.logging 级别 。所以解决方案就是简单地配置 java.util.logging(又名 jul 或 jdk 日志)来抑制来自这些组件的警告。

我本可以尝试在我们的 WebSphere 实例中配置 jdk 日志记录配置文件,但在我们的生产环境(共享基础架构、锁定服务器)中部署和管理这是一个更加困难的更改。所以我最终使用了一个 java 解决方案——一个我注册为 Spring bean 的类,它的 init 方法改变了它提供的记录器名称的日志记录级别:

<bean id="setJdkLoggingToSevere" class="ca.mycompany.myapp.util.JdkLoggingLevelConfigurer" init-method="init">
    <property name="level" value="SEVERE" />
    <property name="loggerNames">
        <list>
            <value>org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl</value>
            <value>org.apache.myfaces.renderkit.html.HtmlImageRenderer</value>
            <value>org.apache.myfaces.renderkit.html.ext.HtmlImageRenderer</value>
            <value>org.apache.myfaces.shared.renderkit.html.HtmlImageRendererBase</value>
            <value>org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlImageRendererBase</value>
            <value>org.apache.myfaces.renderkit.html.HtmlLabelRenderer</value>
            <value>org.apache.myfaces.renderkit.html.HtmlGridRenderer</value>
            <value>org.apache.myfaces.renderkit.html.ext.HtmlGridRenderer</value>
            <value>org.apache.myfaces.shared.renderkit.html.HtmlGridRendererBase</value>
            <value>org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlGridRendererBase</value>
            <value>org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils</value>
            <value>org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils</value>
        </list>
    </property>
</bean>

这是我的JdkLoggingLevelConfigurer 类的相关方法(注意myLogger 是一个 slf4j 记录器,因为 slf4j 是我的应用程序的日志框架):

public void init() {
    if (this.getLoggerNames() != null) {

        Level level = Level.parse(this.getLevel());

        for (String loggerName : loggerNames) {
            Logger logger = Logger.getLogger(loggerName);
            if (logger != null) {
                myLogger.info("setting jdk logging for {} to {}", loggerName, level);
                logger.setLevel(level);
                this.loggers.add(logger);
            }
            else {
                myLogger.warn("unable to set jdk logging for {} to {} because logger was null",
                        loggerName, this.getLevel());
            }
        }
    }
}

以上内容到位后,我们将不再看到警告消息。如果我错过了任何会发出警告的 myfaces 组件(我最初确实错过了一些),可以轻松地将它们添加到 spring 配置中。

【讨论】:

    【解决方案2】:

    在 HtmlImageRendererBase 中使用了 JUL 记录器,因此只需将文件 logging.properties 添加到您的类路径中,包括以下行:

    org.apache.myfaces.shared.renderkit.html.HtmlImageRendererBase.level = SEVERE
    

    【讨论】:

    • 在共享服务器上“将文件添加到类路径”并不一定像听起来那么简单。将文件包含在我的 WAR 的 WEB-INF/classes 目录中会起作用吗?如果是这样,那将不会与服务器上的其他应用程序冲突,并且将是一个很好的解决方案。 (当然,我需要在 logging.properties 中添加几行——根据我下面的解决方案,许多 Renderer 类。)
    • 好的,明白了:在我的环境中,将此条目添加到 WEB-INF/classes 中的 logging.properties 就足够了 - 但我不知道这是否适用于共享服务器。跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 2017-07-26
    • 2014-08-30
    • 1970-01-01
    • 2019-02-25
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多