All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.remote.server.LoggingHandler Maven / Gradle / Ivy

Go to download

Selenium automates browsers. That's it! What you do with that power is entirely up to you.

There is a newer version: 3.9.1
Show newest version
/*
Copyright 2011 Software Freedom Conservatory.

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.openqa.selenium.remote.server;

import com.google.common.collect.Lists;

import org.openqa.selenium.logging.LogEntry;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * A custom handler used to records log entries.
 *
 * This handler queues up log records as they come, up to 10000 records.
 * If it reaches this capacity it will remove the older records
 * from the queue before adding the next one.
 */
public class LoggingHandler extends Handler {

  // For now keep track of the last 10000 log records.
  private static final int MAX_RECORDS = 10000;
  private LinkedList records = Lists.newLinkedList();
  private static final LoggingHandler instance = new LoggingHandler();

  private LoggingHandler() {}

  public static LoggingHandler getInstance() {
    return instance;
  }

  /**
   *
   * @return an unmodifiable list of LogEntry.
   */
  public List getRecords() {
    return Collections.unmodifiableList(records);
  }

  @Override
  public void publish(LogRecord logRecord) {
    if (isLoggable(logRecord)) {
      if (records.size() > MAX_RECORDS) {
        records.remove();
      }
      records.add(new LogEntry(logRecord.getLevel().intValue(),
          logRecord.getMillis(),
          logRecord.getLoggerName() + " "
              + logRecord.getSourceClassName() + "." + logRecord.getSourceMethodName()
              + " " + logRecord.getMessage()));
    }
  }

  public void attachTo(Logger logger, Level level) {
    Handler[] handlers = logger.getHandlers();
    for (Handler handler : handlers) {
      if (handler == this) {
        // the handler has already been added
        return;
      }
    }
    setLevel(level);
    logger.addHandler(this);
  }

  @Override
  public void flush() {
  }

  @Override
  public void close() throws SecurityException {
    records.clear();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy