<commit_before>/*
Copyright 2009-2016 <NAME>

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 org.javalite.activejdbc;

import org.javalite.activejdbc.statistics.QueryExecutionEvent;
import org.slf4j.Logger;

import java.util.regex.Pattern;

import static org.javalite.common.Util.empty;
import static org.javalite.common.Util.join;

/**
 * @author <NAME>
 */
public class LogFilter {

    private static Pattern pattern;

    static{
        String logFlag = System.getProperty("activejdbc.log");
        if (logFlag != null && logFlag.equals("")) {
            //match anything
            setLogExpression(".*");
        } else if (logFlag != null ) {//match by provided value
            setLogExpression(logFlag);
        } else {//match nothing
            setLogExpression("a{10000000}");
        }
    }
    
    private LogFilter() {
        
    }

    public static void setLogExpression(String regexp){
        pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
    }

    static void logQuery(Logger logger, String query, Object[] params, long queryStartTime){
        long time = System.currentTimeMillis() - queryStartTime;

        if (Registry.instance().getConfiguration().collectStatistics()) {
            Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time));
        }

        if (logger.isInfoEnabled()) {
            log(logger, getJson(query, params, time));
        }
    }

    private static String getJson(String query, Object[] params, long time) {
        StringBuilder paramsSB = new StringBuilder("");
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if(params[i] instanceof Number){
                    paramsSB.append(params[i]);
                }else if(params[i] instanceof byte[]){
                    paramsSB.append("\"bytes[...]\"");
                }else {
                    paramsSB.append("\"").append(params[i]).append("\"");
                }
                if(i != (params.length - 1)){
                    paramsSB.append(",");
                }
            }
        }
        return "{\"sql\":\"" + query.replace("\"", "'") + "\",\"params\":[" + paramsSB.toString() + "],\"duration_millis\":" + time + "}";
    }

    private static String getText(String query, Object[] params, long time) {
        StringBuilder log =  new StringBuilder().append("Query: \\\"").append(query.replace("\"", "'")).append("\\\"");
        if (!empty(params)) {
            log.append(", with parameters: ").append('<');
            join(log, params, ">, <");
            log.append('>');
        }
        return log.append(", took: ").append(time).append(" milliseconds").toString();
    }

    public static void log(Logger logger, String log){
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log);
        }
    }

    public static void log(Logger logger, String log, Object param) {
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log, param);
        }
    }

    public static void log(Logger logger, String log, Object param1, Object param2) {
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log, param1, param2);
        }
    }

    public static void log(Logger logger, String log, Object... params) {
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log, params);
        }<commit_msg> #576 Implement logging on JSON format - removed more junk<commit_after>/*
Copyright 2009-2016 <NAME>

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 org.javalite.activejdbc;

import org.javalite.activejdbc.statistics.QueryExecutionEvent;
import org.slf4j.Logger;

import java.util.regex.Pattern;

import static org.javalite.common.Util.empty;
import static org.javalite.common.Util.join;

/**
 * @author <NAME>
 */
public class LogFilter {

    private static Pattern pattern;

    static{
        String logFlag = System.getProperty("activejdbc.log");
        if (logFlag != null && logFlag.equals("")) {
            //match anything
            setLogExpression(".*");
        } else if (logFlag != null ) {//match by provided value
            setLogExpression(logFlag);
        } else {//match nothing
            setLogExpression("a{10000000}");
        }
    }
    
    private LogFilter() {
        
    }

    public static void setLogExpression(String regexp){
        pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
    }

    static void logQuery(Logger logger, String query, Object[] params, long queryStartTime){
        long time = System.currentTimeMillis() - queryStartTime;

        if (Registry.instance().getConfiguration().collectStatistics()) {
            Registry.instance().getStatisticsQueue().enqueue(new QueryExecutionEvent(query, time));
        }

        if (logger.isInfoEnabled()) {
            log(logger, getJson(query, params, time));
        }
    }

    private static String getJson(String query, Object[] params, long time) {
        StringBuilder paramsSB = new StringBuilder("");
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if(params[i] instanceof Number){
                    paramsSB.append(params[i]);
                }else if(params[i] instanceof byte[]){
                    paramsSB.append("\"bytes[...]\"");
                }else {
                    paramsSB.append("\"").append(params[i]).append("\"");
                }
                if(i != (params.length - 1)){
                    paramsSB.append(",");
                }
            }
        }
        return "{\"sql\":\"" + query.replace("\"", "'") + "\",\"params\":[" + paramsSB.toString() + "],\"duration_millis\":" + time + "}";
    }

    public static void log(Logger logger, String log){
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log);
        }
    }

    public static void log(Logger logger, String log, Object param) {
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log, param);
        }
    }

    public static void log(Logger logger, String log, Object param1, Object param2) {
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log, param1, param2);
        }
    }

    public static void log(Logger logger, String log, Object... params) {
        if (logger.isInfoEnabled() && pattern.matcher(log).matches()) {
           logger.info(log, params);
        }
{'fraction_non_alphanumeric': 0.13473684210526315, 'fraction_numerical': 0.0075, 'mean_word_length': 2.3842386464826357, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '14660', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2036, 'n_words': 568}