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

edu.stanford.nlp.util.logging.RedirectOutputHandler Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.util.logging;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import edu.stanford.nlp.util.Generics;

/**
 *  A class to redirect the output of Redwood to another logging mechanism,
 *  e.g., java.util.logging.
 *
 *  @author Gabor Angeli
 */
public class RedirectOutputHandler extends OutputHandler {

  public final LoggerClass logger;
  public final Method loggingMethod;
  private final Map channelMapping;
  private final ChannelEquivalent defaultChannel;

  /**
   * Create a redirect handler, with a logging class, ignoring logging
   * levels.
   * @param logger The class to use for logging. For example, java.util.logging.Logger
   * @param loggingMethod A method which takes a *single* String argument
   *                         and logs that string using the |logger| class.
   */
  public RedirectOutputHandler(LoggerClass logger, Method loggingMethod) {
    this(logger, loggingMethod, null, null);
  }

  /**
   * Create a redirect handler, with a logging class, redirecting both the logging
   * message, and the channel that it came from
   * @param logger The class to use for logging. For example,
   *                 java.util.logging.Logger
   * @param loggingMethod A method which takes a *single* String argument
   *                         and logs that string using the |logger| class.
   * @param channelMapping The mapping from Redwood channels, to the native Channel equivalent.
   */
  public RedirectOutputHandler(LoggerClass logger, Method loggingMethod,
                               Map channelMapping,
                               ChannelEquivalent defaultChannel) {
    this.logger = logger;
    this.loggingMethod = loggingMethod;
    this.channelMapping = channelMapping;
    this.defaultChannel = defaultChannel;
  }

  private boolean shouldLogChannels() {
    return channelMapping != null;
  }

  @Override
  public void print(Object[] channels, String line) {
    if (line.endsWith("\n")) {
      line = line.substring(0, line.length() - 1);
    }
    if (shouldLogChannels()) {
      // -- Case: log with channel
      // (get channel to publish on)
      ChannelEquivalent channel = null;
      if (channels == null) {
        // (case: no channel provided)
        channel = defaultChannel;
      } else {
        for (Object candidate : channels) {
          if (channel == null) {
            // (case: channel found in mapping)
            channel = channelMapping.get(candidate);
          }
        }
        if (channel == null) {
          // (case: no channel found in mapping)
          channel = this.defaultChannel;
        }
      }
      // (publish message)
      try {
        this.loggingMethod.invoke(this.logger, channel, line);
      } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
      } catch (InvocationTargetException e) {
        throw new IllegalStateException(e.getCause());
      }
    } else {
      // -- Case: log without channel
      try {
        this.loggingMethod.invoke(this.logger, line);
      } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
      } catch (InvocationTargetException e) {
        throw new IllegalStateException(e.getCause());
      }
    }
  }

  /**
   * Ensure that we don't print duplicate channels when adapting to another logging framework.
   * @inheritDoc
   */
  @Override
  protected boolean formatChannel(StringBuilder b, String channelStr, Object channel){
    return !(channelMapping != null && channelMapping.containsKey(channel));
  }

  //
  // LOGGER IMPLEMENTATIONS
  //

  public static RedirectOutputHandler fromJavaUtilLogging(java.util.logging.Logger logger) {
    Map  channelMapping = Generics.newHashMap();
    channelMapping.put(Redwood.WARN, java.util.logging.Level.WARNING);
    channelMapping.put(Redwood.DBG,  java.util.logging.Level.FINE);
    channelMapping.put(Redwood.ERR,  java.util.logging.Level.SEVERE);
    try {
      return new RedirectOutputHandler<>(
              logger,
              java.util.logging.Logger.class.getMethod("log", java.util.logging.Level.class, String.class),
              channelMapping,
              java.util.logging.Level.INFO
      );
    } catch (NoSuchMethodException e) {
      throw new IllegalStateException(e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy