Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EssentialFilter play framework java (logging time)

Im Trying to create a time logger filter to monitor the time my requests are taking in play framework 2 using java, however the documentation on the java side of Filters are weak.

Can anyone point me in the right direction on how to achieve this?

The scala guide is found at http://www.playframework.com/documentation/2.1.3/ScalaHttpFilters

like image 521
user2870621 Avatar asked Dec 07 '25 21:12

user2870621


1 Answers

So you would start with Filter like this:

public class TimeLoggingFilter implements EssentialFilter {

    public EssentialAction apply(final EssentialAction next) {

        return new TimeLoggingAction() {

            @Override
            public EssentialAction apply() {
                return next.apply();
            }

            @Override
            public Iteratee<byte[], SimpleResult> apply(final RequestHeader rh) {
                final long startTime = System.currentTimeMillis();

                return next.apply(rh).map(new AbstractFunction1<SimpleResult, SimpleResult>() {

                    @Override
                    public SimpleResult apply(SimpleResult v1) {
                        long time = logTime(rh, startTime);
                        List<Tuple2<String, String>> list = new ArrayList<Tuple2<String, String>>();
                        Tuple2<String, String> t =
                                new Tuple2<String, String>("Request-Time",
                                        String.valueOf(time));
                        list.add(t);
                        Seq<Tuple2<String, String>> seq = Scala.toSeq(list);
                        return v1.withHeaders(seq);
                    }

                    @Override
                    public <A> Function1<SimpleResult, A> andThen(Function1<SimpleResult, A> g) {
                        return g;
                    }

                    @Override
                    public <A> Function1<A, SimpleResult> compose(Function1<A, SimpleResult> g) {
                        return g;
                    }

                }, Execution.defaultExecutionContext());
            }


            private long logTime(RequestHeader request, long startTime) {
                long endTime = System.currentTimeMillis();
                long requestTime = endTime - startTime;
                Logger.info(request.uri() + " from " + request.remoteAddress() + " took " + requestTime + " ms");

                return requestTime;
            }
        };
    }

    public abstract class TimeLoggingAction extends
            AbstractFunction1<RequestHeader, Iteratee<byte[], SimpleResult>>
            implements EssentialAction {}
}

and then hook it up in your Global.java:

public <T extends EssentialFilter> Class<T>[] filters() {
        return new Class[] { TimeLoggingFilter.class  };
    }

I was looking for a similar example today myself, and didn't find anything - but this appears to work.

like image 133
kozyr Avatar answered Dec 09 '25 19:12

kozyr



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!