<commit_before>package org.openqa.selenium.server.log;

import org.apache.commons.logging.Log;
import org.openqa.jetty.log.LogFactory;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * Configure logging to Selenium taste.
 */
public class LoggingManager {

    private static Handler[] originalHandlers;
    private static Map<Handler, Formatter> originalFormatters;
    private static Map<Handler, Level> originalLogLevels;
    private static Map<File, FileHandler> seleniumFileHandlers = new HashMap<File, FileHandler>();
    private static ShortTermMemoryHandler shortTermMemoryHandler;
    private static PerSessionLogHandler perSessionLogHandler;

    
    public static synchronized Log configureLogging(RemoteControlConfiguration configuration, boolean debugMode) {
        final Log seleniumServerJettyLogger;
        final Logger currentLogger;

        if (configuration.dontTouchLogging()) {
            return LogFactory.getLog(SeleniumServer.class);
        }

        currentLogger = Logger.getLogger("");
        resetLoggerToOriginalState();
        overrideSimpleFormatterWithTerseOneForConsoleHandler(currentLogger, debugMode);
        addInMemoryLogger(currentLogger, configuration);
        addPerSessionLogger(currentLogger, configuration, debugMode);
        if (debugMode) {
            currentLogger.setLevel(Level.FINE);
        }

        seleniumServerJettyLogger = LogFactory.getLog(SeleniumServer.class);
        if (null != configuration.getLogOutFile()) {
            addNewSeleniumFileHandler(currentLogger, configuration);
            seleniumServerJettyLogger.info("Writing debug logs to " + configuration.getLogOutFile());
        }

        return seleniumServerJettyLogger;
    }

    public static synchronized ShortTermMemoryHandler shortTermMemoryHandler() {
        return shortTermMemoryHandler;
    }
    
    public static synchronized PerSessionLogHandler perSessionLogHandler() {
        return perSessionLogHandler;
    }
    
    private static void addInMemoryLogger(Logger logger, RemoteControlConfiguration configuration) {
        shortTermMemoryHandler = new ShortTermMemoryHandler(
                configuration.shortTermMemoryLoggerCapacity(), Level.INFO, new TerseFormatter(true));
        logger.addHandler(shortTermMemoryHandler);
    }

    private static void addPerSessionLogger(Logger logger, 
        RemoteControlConfiguration configuration, boolean debugMode) {
        if (debugMode) {
            perSessionLogHandler = new PerSessionLogHandler(configuration.shortTermMemoryLoggerCapacity(),<commit_msg>SimonStewart: Removing a dependency on the server from the logging manager<commit_after>package org.openqa.selenium.server.log;

import org.apache.commons.logging.Log;
import org.openqa.jetty.log.LogFactory;
import org.openqa.selenium.server.RemoteControlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * Configure logging to Selenium taste.
 */
public class LoggingManager {

    private static Handler[] originalHandlers;
    private static Map<Handler, Formatter> originalFormatters;
    private static Map<Handler, Level> originalLogLevels;
    private static Map<File, FileHandler> seleniumFileHandlers = new HashMap<File, FileHandler>();
    private static ShortTermMemoryHandler shortTermMemoryHandler;
    private static PerSessionLogHandler perSessionLogHandler;

    
    public static synchronized Log configureLogging(RemoteControlConfiguration configuration, boolean debugMode) {
        final Log seleniumServerJettyLogger;
        final Logger currentLogger;

        if (configuration.dontTouchLogging()) {
            return LogFactory.getLog("org.openqa.selenium.server.SeleniumServer");
        }

        currentLogger = Logger.getLogger("");
        resetLoggerToOriginalState();
        overrideSimpleFormatterWithTerseOneForConsoleHandler(currentLogger, debugMode);
        addInMemoryLogger(currentLogger, configuration);
        addPerSessionLogger(currentLogger, configuration, debugMode);
        if (debugMode) {
            currentLogger.setLevel(Level.FINE);
        }

        seleniumServerJettyLogger = LogFactory.getLog("org.openqa.selenium.server.SeleniumServer");
        if (null != configuration.getLogOutFile()) {
            addNewSeleniumFileHandler(currentLogger, configuration);
            seleniumServerJettyLogger.info("Writing debug logs to " + configuration.getLogOutFile());
        }

        return seleniumServerJettyLogger;
    }

    public static synchronized ShortTermMemoryHandler shortTermMemoryHandler() {
        return shortTermMemoryHandler;
    }
    
    public static synchronized PerSessionLogHandler perSessionLogHandler() {
        return perSessionLogHandler;
    }
    
    private static void addInMemoryLogger(Logger logger, RemoteControlConfiguration configuration) {
        shortTermMemoryHandler = new ShortTermMemoryHandler(
                configuration.shortTermMemoryLoggerCapacity(), Level.INFO, new TerseFormatter(true));
        logger.addHandler(shortTermMemoryHandler);
    }

    private static void addPerSessionLogger(Logger logger, 
        RemoteControlConfiguration configuration, boolean debugMode) {
        if (debugMode) {
            perSessionLogHandler = new PerSessionLogHandler(configuration.shortTermMemoryLoggerCapacity(),
{'fraction_non_alphanumeric': 0.07543424317617867, 'fraction_numerical': 0.0, 'mean_word_length': 4.119390347163421, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7059593', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1471, 'n_words': 323}