<commit_before>/**
 * Copyright 2016, RadiantBlue Technologies, Inc.
 * 
 * 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 util;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.logger.AuditElement;
import model.logger.LoggerPayload;
import model.logger.MetricElement;
import model.logger.Severity;
import javax.annotation.PostConstruct;

/**
 * PiazzaLogger is a class to write syslog logs into Elasticsearch
 * 
 * @author Sonny.Saniev
 * @version 1.0
 */
@Component
public class PiazzaLogger {
	@Value("${logger.name:}")
	private String serviceName;
	@Value("${logger.console:}")
	private Boolean logToConsole;
	@Value("${LOGGER_INDEX}")
	private String loggerIndexName;
	@Value("${vcap.services.pz-elasticsearch.credentials.port}")
	private Integer elasticSearchPort;
	@Value("${vcap.services.pz-elasticsearch.credentials.hostname}")
	private String elasticSearchHost;
	@Value("${elasticsearch.clustername}")
	private String clustername;

	@Autowired
	private Client elasticClient;

	private final static Logger LOGGER = LoggerFactory.getLogger(PiazzaLogger.class);
	private final String LOG_SCHEMA = "LogData";

	/**
	 * Default constructor, required for bean instantiation.
	 */
	public PiazzaLogger() {
	}

	/**
	 * Creates a new Logger component. This constructor is not recommended. It's more recommended to have your project
	 * populate the pz.logger.url and pz.logger.name properties, and allow Spring to Autowire this object. However, this
	 * constructor exists for cases where that is not an option..
	 * 
	 * @param loggerServiceUrl
	 *            The URL of the PiazzaLogger service.
	 * @param serviceName
	 *            The name of the pz-component that utilizes this logger. The name of the component will be included in
	 *            the message content for every log message generated by this instance of the PiazzaLogger.
	 */
	public PiazzaLogger(String loggerServiceUrl, String serviceName) {
		this.serviceName = serviceName;
	}

	@PostConstruct
	public void init() {
		// Create elasticsearch index with mapping
		createIndexWithMapping(elasticClient, loggerIndexName, LOG_SCHEMA, null);
	}

	/**
	 * Spring bean for injecting elasticsearch client.
	 * 
	 * @return Elasticsearch client
	 * @throws UnknownHostException
	 */
	@Bean
	public Client getClient() throws UnknownHostException {
		Settings settings = Settings.settingsBuilder().put("cluster.name", clustername).build();
		TransportClient transportClient = TransportClient.builder().settings(settings).build();
		transportClient.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elasticSearchHost, 9300)));
		return transportClient;
	}

	/**
	 * Writes syslog format log message to elasticsearch
	 * 
	 * @param logMessage
	 *            the message you want to log
	 * @param severity
	 *            the severity of the log
	 */
	public void log(String logMessage, Severity severity) {
		LoggerPayload loggerPayload = getLoggerPayload();
		indexLog(loggerPayload, logMessage, severity);
	}

	/**
	 * Creates elasticsearch index with default mapping.
	 * 
	 * @param client
	 *            elasticsearch client
	 * @param indexName
	 *            index to save to
	 * @param type
	 *            schema to save to
	 * @param mapping
	 *            mapping object
	 * @return boolean
	 */
	public boolean createIndexWithMapping(Client client, String indexName, String type, String mapping) {<commit_msg>Adding new vcap env variable<commit_after>/**
 * Copyright 2016, RadiantBlue Technologies, Inc.
 * 
 * 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 util;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.logger.AuditElement;
import model.logger.LoggerPayload;
import model.logger.MetricElement;
import model.logger.Severity;
import javax.annotation.PostConstruct;

/**
 * PiazzaLogger is a class to write syslog logs into Elasticsearch
 * 
 * @author Sonny.Saniev
 * @version 1.0
 */
@Component
public class PiazzaLogger {
	@Value("${logger.name:}")
	private String serviceName;
	@Value("${logger.console:}")
	private Boolean logToConsole;
	@Value("${LOGGER_INDEX}")
	private String loggerIndexName;
	@Value("${vcap.services.pz-elasticsearch.credentials.transportClientPort}")
	private Integer elasticSearchPort;
	@Value("${vcap.services.pz-elasticsearch.credentials.hostname}")
	private String elasticSearchHost;
	@Value("${elasticsearch.clustername}")
	private String clustername;

	@Autowired
	private Client elasticClient;

	private final static Logger LOGGER = LoggerFactory.getLogger(PiazzaLogger.class);
	private final String LOG_SCHEMA = "LogData";

	/**
	 * Default constructor, required for bean instantiation.
	 */
	public PiazzaLogger() {
	}

	/**
	 * Creates a new Logger component. This constructor is not recommended. It's more recommended to have your project
	 * populate the pz.logger.url and pz.logger.name properties, and allow Spring to Autowire this object. However, this
	 * constructor exists for cases where that is not an option..
	 * 
	 * @param loggerServiceUrl
	 *            The URL of the PiazzaLogger service.
	 * @param serviceName
	 *            The name of the pz-component that utilizes this logger. The name of the component will be included in
	 *            the message content for every log message generated by this instance of the PiazzaLogger.
	 */
	public PiazzaLogger(String loggerServiceUrl, String serviceName) {
		this.serviceName = serviceName;
	}

	@PostConstruct
	public void init() {
		// Create elasticsearch index with mapping
		createIndexWithMapping(elasticClient, loggerIndexName, LOG_SCHEMA, null);
	}

	/**
	 * Spring bean for injecting elasticsearch client.
	 * 
	 * @return Elasticsearch client
	 * @throws UnknownHostException
	 */
	@Bean
	public Client getClient() throws UnknownHostException {
		Settings settings = Settings.settingsBuilder().put("cluster.name", clustername).build();
		TransportClient transportClient = TransportClient.builder().settings(settings).build();
		transportClient.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(elasticSearchHost, elasticSearchPort)));
		return transportClient;
	}

	/**
	 * Writes syslog format log message to elasticsearch
	 * 
	 * @param logMessage
	 *            the message you want to log
	 * @param severity
	 *            the severity of the log
	 */
	public void log(String logMessage, Severity severity) {
		LoggerPayload loggerPayload = getLoggerPayload();
		indexLog(loggerPayload, logMessage, severity);
	}

	/**
	 * Creates elasticsearch index with default mapping.
	 * 
	 * @param client
	 *            elasticsearch client
	 * @param indexName
	 *            index to save to
	 * @param type
	 *            schema to save to
	 * @param mapping
	 *            mapping object
	 * @return boolean
	 */
	public boolean createIndexWithMapping(Client client, String indexName, String type, String mapping) {
{'fraction_non_alphanumeric': 0.08679891249622394, 'fraction_numerical': 0.002819454234216091, 'mean_word_length': 4.4541460735859415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2539780', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2572, 'n_words': 822}