org.hibernate.search.elasticsearch.logging.impl.ElasticsearchResponseFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-search-elasticsearch Show documentation
Show all versions of hibernate-search-elasticsearch Show documentation
Hibernate Search backend which has indexing operations forwarded to Elasticsearch
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.search.elasticsearch.logging.impl;
import org.hibernate.search.elasticsearch.client.impl.ElasticsearchRequest;
import org.hibernate.search.elasticsearch.client.impl.ElasticsearchResponse;
import org.hibernate.search.elasticsearch.util.impl.JsonLogHelper;
/**
* Used with JBoss Logging's {@link org.jboss.logging.annotations.FormatWith}
* to display {@link ElasticsearchRequest}s in log messages.
*
* @author Yoann Rodiere
*/
public class ElasticsearchResponseFormatter {
private final String stringRepresentation;
public ElasticsearchResponseFormatter(ElasticsearchResponse response) {
this.stringRepresentation = formatResponse( response );
}
public static String formatResponse(ElasticsearchResponse response) {
if ( response == null ) {
return null;
}
JsonLogHelper helper = JsonLogHelper.get();
//Wild guess for some tuning. The only certainty is that the default (16) is too small.
//Also useful to hint the builder to use larger increment steps.
StringBuilder sb = new StringBuilder( 180 );
sb.append( response.getStatusCode() )
.append( " '" )
.append( response.getStatusMessage() )
.append( "' with body " )
.append( helper.toString( response.getBody() ) );
return sb.toString();
}
@Override
public String toString() {
return stringRepresentation;
}
}