Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log/Feedback message when using EL

Tags:

jsf

I have the problem that I don't get any feedback message within logs or page when trying to get a parameter/context value that doesn't exist.

Is there any way to enable detailed EL error logging, I already added some lines within my web.xml:

<context-param>
    <param-name>javax.faces.FACELETS_DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

But this doesn't help.

like image 500
John Rumpel Avatar asked Mar 01 '26 19:03

John Rumpel


1 Answers

I expect what you are observing is just the required behavior of EL and the VDL.

That said, you can log some limited but potentially useful information by introducing your own resolver code.

The VariableResolver resolves top-level artifacts:

package logs;

import java.util.logging.*;
import javax.faces.context.FacesContext;
import javax.faces.el.*;

@SuppressWarnings("deprecated")
public class LogVariableResolver extends VariableResolver {

  private static final Logger LOG = 
                         Logger.getLogger(LogVariableResolver.class.getName());
  private final VariableResolver decorated;

  public LogVariableResolver(VariableResolver decorated) {
    this.decorated = decorated;
  }

  @Override
  public Object resolveVariable(FacesContext context, String name)
                                          throws EvaluationException {
    Object resolved = decorated.resolveVariable(context, name);
    if (resolved == null && LOG.isLoggable(Level.WARNING)) {
      LOG.warning("Unresolved: " + name);
    } else {
      LOG.info("Resolved: " + name + " " + resolved);
    }
    return resolved;
  }
}

The PropertyResolver resolves properties on objects:

package logs;

import java.util.logging.*;
import javax.faces.el.*;

@SuppressWarnings("deprecated")
public class LogPropertyResolver extends PropertyResolver {
  private static final Logger LOG =
                          Logger.getLogger(LogPropertyResolver.class.getName());
  private final PropertyResolver decorated;

  public LogPropertyResolver(PropertyResolver pr) {
    this.decorated = pr;
  }

  @Override
  public Object getValue(Object base, Object property)
                         throws EvaluationException, PropertyNotFoundException {
    return log(decorated.getValue(base, property), base, property);
  }

  @Override
  public Object getValue(Object base, int index)
                         throws EvaluationException, PropertyNotFoundException {
    return log(decorated.getValue(base, index), base, index);
  }

  private Object log(Object result, Object base, Object prop) {
    if (result == null && LOG.isLoggable(Level.WARNING)) {
      LOG.warning("Result null for property " + prop + " on " + base);
    }
    return result;
  }

// implement remaining methods to call delegate & return values

The resolvers can be registered in a faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
      <variable-resolver>logs.LogVariableResolver</variable-resolver>
      <property-resolver>logs.LogPropertyResolver</property-resolver>
    </application>
 <!-- etc -->

These types are deprecated but it's easier to use them for this than provisioning an ELContext.

like image 60
McDowell Avatar answered Mar 04 '26 22:03

McDowell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!