<commit_before>/**
 * Orignal work: Copyright 2015 www.seleniumtests.com
 * Modified work: Copyright 2016 www.infotel.com
 * 				Copyright 2017-2019 B.Hecquet
 *
 * 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.seleniumtests.util.logging;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.seleniumtests.util.helper.WaitHelper;

public class SeleniumRobotLogger {
	
	private static final String LOG_PATTERN = " %-5p %d [%t] %C{1}: %m%n";

	private static final String FILE_APPENDER_NAME = "FileLogger";
	private static final Pattern LOG_FILE_PATTERN = Pattern.compile(".*?\\d \\[(.*?)\\](.*)");
	private static Map<String, String> testLogs = Collections.synchronizedMap(new HashMap<>());
	private static String outputDirectory;
	private static String defaultOutputDirectory;
	
	public static final String START_TEST_PATTERN = "Start method ";
	public static final String END_TEST_PATTERN = "Finish method ";
	public static final String METHOD_NAME = "methodName";				// name of the test method (or the cucumber scenario)
	public static final String UNIQUE_METHOD_NAME = "uniqueMethodName"; // unique name of the test (in case several tests have the same name)
	public static final String LOG_FILE_NAME = "seleniumRobot.log";
	private static final String INTERNAL_DEV_MODE = "internalDevMode";
	
	private SeleniumRobotLogger() {
		// As a utility class, it is not meant to be instantiated.
	}

	public static Logger getLogger(final Class<?> cls) {
	    boolean rootIsConfigured = Logger.getRootLogger().getAllAppenders().hasMoreElements();
	    if (!rootIsConfigured) {
	    	
	        BasicConfigurator.configure();
	        Logger rootLogger = Logger.getRootLogger();
	
	        Appender appender = (Appender) rootLogger.getAllAppenders().nextElement();
	        appender.setLayout(new PatternLayout(SeleniumRobotLogger.LOG_PATTERN));
	    }
	    
        // use System property instead of SeleniumTestsContext class as SeleniumrobotLogger class is used for grid extension package and 
        // we do not want to depend on "SeleniumTestsContext" class here
	    Logger rootLogger = Logger.getRootLogger();
        if (System.getProperty(INTERNAL_DEV_MODE) != null && "true".equals(System.getProperty(INTERNAL_DEV_MODE))) {
        	rootLogger.setLevel(Level.DEBUG);
        } else {
        	rootLogger.setLevel(Level.INFO);
        }
	
	    return Logger.getLogger(cls);
	}


	/**
	 * Update root logger so that logs are made available in a log file
	 * This code is delayed so that SeleniumTestsContext is initialized
	 * This is also not called for unit and integration tests
	 * 
	 */
	public static void updateLogger(String outputDir, String defaultOutputDir) {
		updateLogger(outputDir, defaultOutputDir, SeleniumRobotLogger.LOG_FILE_NAME);
	}
	
	public static void updateLogger(String outputDir, String defaultOutputDir, String logFileName) {
		outputDirectory = outputDir;
		defaultOutputDirectory = defaultOutputDir;
		Appender fileLoggerAppender = Logger.getRootLogger().getAppender(FILE_APPENDER_NAME);
		if (fileLoggerAppender == null) {
			Logger rootLogger = Logger.getRootLogger();
			FileAppender fileAppender = new FileAppender();
			
			// clean output dir
			cleanResults();
			
			for (int i=0; i < 4; i++) {
				try {
					if (!new File(outputDir).exists()) {
						new File(outputDir).mkdirs();
					}
			        
			        
			        fileAppender.setName(FILE_APPENDER_NAME);
			        fileAppender.setFile(outputDir + "/" + logFileName);
			        fileAppender.setLayout(new PatternLayout(LOG_PATTERN));
			        
			        if (System.getProperty("internalDevMode") != null && "true".equals(System.getProperty("internalDevMode"))) {
			        	fileAppender.setThreshold(Level.DEBUG);
			        } else {
			        	fileAppender.setThreshold(Level.INFO);
			        }
			        
			        fileAppender.activateOptions();
			        break;
				} catch (Exception e) {
				}<commit_msg>related to grid issue #38<commit_after>/**
 * Orignal work: Copyright 2015 www.seleniumtests.com
 * Modified work: Copyright 2016 www.infotel.com
 * 				Copyright 2017-2019 B.Hecquet
 *
 * 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.seleniumtests.util.logging;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.seleniumtests.util.helper.WaitHelper;

public class SeleniumRobotLogger {
	
	private static final String LOG_PATTERN = " %-5p %d [%t] %C{1}: %m%n";

	private static final String FILE_APPENDER_NAME = "FileLogger";
	private static final Pattern LOG_FILE_PATTERN = Pattern.compile(".*?\\d \\[(.*?)\\](.*)");
	private static Map<String, String> testLogs = Collections.synchronizedMap(new HashMap<>());
	private static String outputDirectory;
	private static String defaultOutputDirectory;
	
	public static final String START_TEST_PATTERN = "Start method ";
	public static final String END_TEST_PATTERN = "Finish method ";
	public static final String METHOD_NAME = "methodName";				// name of the test method (or the cucumber scenario)
	public static final String UNIQUE_METHOD_NAME = "uniqueMethodName"; // unique name of the test (in case several tests have the same name)
	public static final String LOG_FILE_NAME = "seleniumRobot.log";
	private static final String INTERNAL_DEV_MODE = "internalDevMode";
	
	private SeleniumRobotLogger() {
		// As a utility class, it is not meant to be instantiated.
	}

	public static Logger getLogger(final Class<?> cls) {
	    boolean rootIsConfigured = Logger.getRootLogger().getAllAppenders().hasMoreElements();
	    if (!rootIsConfigured) {
	    	
	        BasicConfigurator.configure();
	        Logger rootLogger = Logger.getRootLogger();
	
	        Appender appender = (Appender) rootLogger.getAllAppenders().nextElement();
	        appender.setLayout(new PatternLayout(SeleniumRobotLogger.LOG_PATTERN));
	    }
	    
        // use System property instead of SeleniumTestsContext class as SeleniumrobotLogger class is used for grid extension package and 
        // we do not want to depend on "SeleniumTestsContext" class here
	    Logger rootLogger = Logger.getRootLogger();
        if (System.getProperty(INTERNAL_DEV_MODE) != null && "true".equals(System.getProperty(INTERNAL_DEV_MODE))) {
        	rootLogger.setLevel(Level.DEBUG);
        } else {
        	rootLogger.setLevel(Level.INFO);
        }
	
	    return Logger.getLogger(cls);
	}


	/**
	 * Update root logger so that logs are made available in a log file
	 * This code is delayed so that SeleniumTestsContext is initialized
	 * This is also not called for unit and integration tests
	 * 
	 */
	public static void updateLogger(String outputDir, String defaultOutputDir) {
		updateLogger(outputDir, defaultOutputDir, SeleniumRobotLogger.LOG_FILE_NAME);
	}
	
	public static void updateLogger(String outputDir, String defaultOutputDir, String logFileName) {
		updateLogger(outputDir, defaultOutputDir, logFileName, true);
	}
	
	public static void updateLogger(String outputDir, String defaultOutputDir, String logFileName, boolean doCleanResults) {
		outputDirectory = outputDir;
		defaultOutputDirectory = defaultOutputDir;
		Appender fileLoggerAppender = Logger.getRootLogger().getAppender(FILE_APPENDER_NAME);
		if (fileLoggerAppender == null) {
			Logger rootLogger = Logger.getRootLogger();
			FileAppender fileAppender = new FileAppender();
			
			// clean output dir
			if (doCleanResults) {
				cleanResults();
			}
			
			for (int i=0; i < 4; i++) {
				try {
					if (!new File(outputDir).exists()) {
						new File(outputDir).mkdirs();
					}
			        
			        
			        fileAppender.setName(FILE_APPENDER_NAME);
			        fileAppender.setFile(outputDir + "/" + logFileName);
			        fileAppender.setLayout(new PatternLayout(LOG_PATTERN));
			        
			        if (System.getProperty("internalDevMode") != null && "true".equals(System.getProperty("internalDevMode"))) {
			        	fileAppender.setThreshold(Level.DEBUG);
			        } else {
			        	fileAppender.setThreshold(Level.INFO);
			        }
			        
			        fileAppender.activateOptions();
			        break;
				} catch (Exception e) {
				}
{'fraction_non_alphanumeric': 0.09543088490456912, 'fraction_numerical': 0.005976479660690187, 'mean_word_length': 3.735280693747147, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, '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': '260764', 'n_tokens_mistral': 3226, 'n_tokens_neox': 2901, 'n_words': 845}