/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.phoenix.log;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.phoenix.query.ConnectionQueryServices;
import org.apache.phoenix.schema.PName;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public class QueryLoggerUtil {

    public static void logInitialDetails(QueryLogger queryLogger, PName tenantId, ConnectionQueryServices queryServices,
            String query, long startTime, List<Object> bindParameters) {
        try {
            queryLogger.log(QueryLogState.STARTED,
                    getInitialDetails(tenantId, queryServices, query, startTime, bindParameters));
        } catch (Exception e) {
            // Ignore for now
        }

    }

    private static ImmutableMap<QueryLogInfo, Object> getInitialDetails(PName tenantId,
            ConnectionQueryServices queryServices, String query, long startTime, List<Object> bindParameters) {
        Builder<QueryLogInfo, Object> queryLogBuilder = ImmutableMap.builder();
        String clientIP;
        try {
            clientIP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            clientIP = "UnknownHost";
        }

        if (clientIP != null) {
            queryLogBuilder.put(QueryLogInfo.CLIENT_IP_I, clientIP);
        }
        if (query != null) {
            queryLogBuilder.put(QueryLogInfo.QUERY_I, query);
        }
        queryLogBuilder.put(QueryLogInfo.START_TIME_I, startTime);
        if (bindParameters != null) {
            queryLogBuilder.put(QueryLogInfo.BIND_PARAMETERS_I, StringUtils.join(bindParameters, ","));
        }
        if (tenantId != null) {
            queryLogBuilder.put(QueryLogInfo.TENANT_ID_I, tenantId.getString());
        }

        queryLogBuilder.put(QueryLogInfo.USER_I, queryServices.getUserName() != null ? queryServices.getUserName()
                : queryServices.getUser().getShortName());
        return queryLogBuilder.build();
    }
}

@@ -1277,16 +1277,28 @@
 enantId,
+%0A           
  Connect
@@ -1328,28 +1328,16 @@
 ervices,
-%0A           
  String 
@@ -1390,34 +1390,16 @@
 ters) %7B%0A
-        try %7B%0A    
         
@@ -1437,20 +1437,16 @@
 TARTED,%0A
-    
         
@@ -1535,80 +1535,8 @@
 s));
-%0A        %7D catch (Exception e) %7B%0A            // Ignore for now%0A        %7D
 %0A%0A  
@@ -2022,45 +2022,8 @@
   %7D%0A
-%0A        if (clientIP != null) %7B%0A    
     
@@ -2087,51 +2087,8 @@
 P);%0A
-        %7D%0A        if (query != null) %7B%0A    
     
@@ -2141,26 +2141,16 @@
 query);%0A
-        %7D%0A
         
@@ -2342,17 +2342,16 @@
 ameters,
- 
 %22,%22));%0A 
@@ -2468,24 +2468,24 @@
 tString());%0A
+
         %7D%0A%0A 
@@ -2478,25 +2478,24 @@
 ;%0A        %7D%0A
-%0A
         quer

Revert "NPE while running sql through file using psql"