![JAR search and dependency download from the Maven repository](/logo.png)
com.zaradai.gluon.transform.LoggingTransformer Maven / Gradle / Ivy
Show all versions of gluon-examples Show documentation
/**
* Copyright 2017 Zaradai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaradai.gluon.transform;
import com.zaradai.gluon.MicroService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class LoggingTransformer extends SimpleTransformer {
private static final Logger LOG = LoggerFactory.getLogger(LoggingTransformer.class);
public static LoggingTransformer create() {
return new LoggingTransformer<>();
}
@Override
public CompletableFuture apply(Req req, MicroService service) {
long started = System.nanoTime();
return service.apply(req).whenCompleteAsync((rep, error) -> {
if (rep != null) {
LOG.info("Returned: {} given input: {} after {} ms", rep, req, getElapsed(started));
}
if (error != null) {
LOG.error("Threw: {} given input: {} after {} ms", error, req, getElapsed(started));
}
});
}
private static long getElapsed(long started) {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started);
}
}