Search in sources :

Example 31 with FileHandler

use of java.util.logging.FileHandler in project CoreNLP by stanfordnlp.

the class SieveCoreferenceSystem method initializeAndRunCoref.

/** Returns the name of the log file that this method writes. */
public static String initializeAndRunCoref(Properties props) throws Exception {
    String timeStamp = Calendar.getInstance().getTime().toString().replaceAll("\\s", "-").replaceAll(":", "-");
    //
    // initialize logger
    //
    String logFileName = props.getProperty(Constants.LOG_PROP, "log.txt");
    if (logFileName.endsWith(".txt")) {
        logFileName = logFileName.substring(0, logFileName.length() - 4) + "_" + timeStamp + ".txt";
    } else {
        logFileName = logFileName + "_" + timeStamp + ".txt";
    }
    try {
        FileHandler fh = new FileHandler(logFileName, false);
        logger.addHandler(fh);
        logger.setLevel(Level.FINE);
        fh.setFormatter(new NewlineLogFormatter());
    } catch (SecurityException | IOException e) {
        throw new RuntimeException("Cannot initialize logger!", e);
    }
    logger.fine(timeStamp);
    logger.fine(props.toString());
    Constants.printConstants(logger);
    // initialize coref system
    SieveCoreferenceSystem corefSystem = new SieveCoreferenceSystem(props);
    // MentionExtractor extracts MUC, ACE, or CoNLL documents
    MentionExtractor mentionExtractor;
    if (props.containsKey(Constants.MUC_PROP)) {
        mentionExtractor = new MUCMentionExtractor(corefSystem.dictionaries, props, corefSystem.semantics, corefSystem.singletonPredictor);
    } else if (props.containsKey(Constants.ACE2004_PROP) || props.containsKey(Constants.ACE2005_PROP)) {
        mentionExtractor = new ACEMentionExtractor(corefSystem.dictionaries, props, corefSystem.semantics, corefSystem.singletonPredictor);
    } else if (props.containsKey(Constants.CONLL2011_PROP)) {
        mentionExtractor = new CoNLLMentionExtractor(corefSystem.dictionaries, props, corefSystem.semantics, corefSystem.singletonPredictor);
    } else {
        throw new RuntimeException("No input file specified!");
    }
    if (!Constants.USE_GOLD_MENTIONS) {
        // Set mention finder
        String mentionFinderClass = props.getProperty(Constants.MENTION_FINDER_PROP);
        if (mentionFinderClass != null) {
            String mentionFinderPropFilename = props.getProperty(Constants.MENTION_FINDER_PROPFILE_PROP);
            CorefMentionFinder mentionFinder;
            if (mentionFinderPropFilename != null) {
                Properties mentionFinderProps = new Properties();
                FileInputStream fis = new FileInputStream(mentionFinderPropFilename);
                mentionFinderProps.load(fis);
                fis.close();
                mentionFinder = (CorefMentionFinder) Class.forName(mentionFinderClass).getConstructor(Properties.class).newInstance(mentionFinderProps);
            } else {
                mentionFinder = (CorefMentionFinder) Class.forName(mentionFinderClass).newInstance();
            }
            mentionExtractor.setMentionFinder(mentionFinder);
        }
        if (mentionExtractor.mentionFinder == null) {
            logger.warning("No mention finder specified, but not using gold mentions");
        }
    }
    if (corefSystem.optimizeSieves && corefSystem.sieves.length > 1) {
        corefSystem.optimizeSieveOrdering(mentionExtractor, props, timeStamp);
    }
    try {
        runAndScoreCoref(corefSystem, mentionExtractor, props, timeStamp);
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "ERROR in running coreference", ex);
    }
    logger.info("done");
    String endTimeStamp = Calendar.getInstance().getTime().toString().replaceAll("\\s", "-");
    logger.fine(endTimeStamp);
    return logFileName;
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) IOException(java.io.IOException) Properties(java.util.Properties) NewlineLogFormatter(edu.stanford.nlp.util.logging.NewlineLogFormatter) FileInputStream(java.io.FileInputStream) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileHandler(java.util.logging.FileHandler)

Example 32 with FileHandler

use of java.util.logging.FileHandler in project heron by twitter.

the class LoggingHelper method getFileHandler.

/**
   * Initialize a <tt>FileHandler</tt> to write to a set of files
   * with optional append.  When (approximately) the given limit has
   * been written to one file, another file will be opened.  The
   * output will cycle through a set of count files.
   * The pattern of file name should be: ${processId}.log.index
   * <p>
   * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
   * properties (or their default values) except that the given pattern
   * argument is used as the filename pattern, the file limit is
   * set to the limit argument, and the file count is set to the
   * given count argument, and the append mode is set to the given
   * <tt>append</tt> argument.
   * <p>
   * The count must be at least 1.
   *
   * @param limit the maximum number of bytes to write to any one file
   * @param count the number of files to use
   * @param append specifies append mode
   * @throws IOException if there are IO problems opening the files.
   * @throws SecurityException if a security manager exists and if
   * the caller does not have <tt>LoggingPermission("control")</tt>.
   * @throws IllegalArgumentException if {@code limit < 0}, or {@code count < 1}.
   * @throws IllegalArgumentException if pattern is an empty string
   */
public static FileHandler getFileHandler(String processId, String loggingDir, boolean append, int limit, int count) throws IOException, SecurityException {
    String pattern = loggingDir + "/" + processId + ".log.%g";
    FileHandler fileHandler = new FileHandler(pattern, limit, count, append);
    fileHandler.setFormatter(new SimpleFormatter());
    fileHandler.setEncoding("UTF-8");
    return fileHandler;
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) FileHandler(java.util.logging.FileHandler)

Example 33 with FileHandler

use of java.util.logging.FileHandler in project OpenAM by OpenRock.

the class ToolLogWriter method init.

/**
     * Initializes the logger with environment parameters.
     */
public static void init() throws IOException {
    String logName = getLogName();
    logger = Logger.getLogger(ToolLogWriter.class.getName());
    fh = new FileHandler(logName, true);
    fh.setFormatter(new SimpleFormatter());
    logger.addHandler(fh);
    //log only above the log level specified
    logger.setLevel(getLogLevel());
    logger.setUseParentHandlers(false);
    String status = "";
    status = SystemProperties.get(ToolConstants.PROPERTY_LOG_ENABLED, "off");
    enabled = status.equalsIgnoreCase("on") ? true : false;
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) FileHandler(java.util.logging.FileHandler)

Example 34 with FileHandler

use of java.util.logging.FileHandler in project 360-Engine-for-Android by 360.

the class LogUtils method enableLogcat.

/**
     * Enable logging.
     */
public static void enableLogcat() {
    mEnabled = true;
    /** Enable the SD Card logger **/
    sLogger = Logger.getLogger(APP_NAME_PREFIX);
    try {
        FileHandler fileHandler = new FileHandler(APP_FILE_NAME, LOG_FILE_LIMIT, LOG_FILE_COUNT);
        fileHandler.setFormatter(new Formatter() {

            @Override
            public String format(final LogRecord logRecord) {
                StringBuilder sb = new StringBuilder();
                sb.append(DATE_FORMAT.format(new Date(logRecord.getMillis())));
                sb.append(" ");
                sb.append(logRecord.getMessage());
                sb.append("\n");
                return sb.toString();
            }
        });
        sLogger.addHandler(fileHandler);
    } catch (IOException e) {
        logE("LogUtils.logToFile() IOException, data will not be logged " + "to file", e);
        sLogger = null;
    }
    if (Settings.ENABLED_PROFILE_ENGINES) {
        /** Enable the SD Card profiler **/
        sProfileLogger = Logger.getLogger("profiler");
        try {
            FileHandler fileHandler = new FileHandler("/sdcard/engineprofiler.log", LOG_FILE_LIMIT, LOG_FILE_COUNT);
            fileHandler.setFormatter(new Formatter() {

                @Override
                public String format(final LogRecord logRecord) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(DATE_FORMAT.format(new Date(logRecord.getMillis())));
                    sb.append("|");
                    sb.append(logRecord.getMessage());
                    sb.append("\n");
                    return sb.toString();
                }
            });
            sProfileLogger.addHandler(fileHandler);
        } catch (IOException e) {
            logE("LogUtils.logToFile() IOException, data will not be logged " + "to file", e);
            sProfileLogger = null;
        }
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) IOException(java.io.IOException) Date(java.util.Date) FileHandler(java.util.logging.FileHandler)

Example 35 with FileHandler

use of java.util.logging.FileHandler in project Asqatasun by Asqatasun.

the class AsqatasunCrawlJob method closeCrawlerLogFiles.

/**
     * Heritrix lets its log files opened at the end of the crawl.
     * We have to close them "manually".
     */
private void closeCrawlerLogFiles() {
    List<FileHandler> loggerHandlerList = new ArrayList<>();
    for (Handler handler : crawlJob.getJobLogger().getHandlers()) {
        if (handler instanceof FileHandler) {
            ((FileHandler) handler).close();
            loggerHandlerList.add((FileHandler) handler);
        }
    }
    for (FileHandler fileHandler : loggerHandlerList) {
        crawlJob.getJobLogger().removeHandler(fileHandler);
    }
}
Also used : FileHandler(java.util.logging.FileHandler) Handler(java.util.logging.Handler) FileHandler(java.util.logging.FileHandler)

Aggregations

FileHandler (java.util.logging.FileHandler)43 IOException (java.io.IOException)19 File (java.io.File)12 SimpleFormatter (java.util.logging.SimpleFormatter)9 Handler (java.util.logging.Handler)7 LogRecord (java.util.logging.LogRecord)5 ConsoleHandler (java.util.logging.ConsoleHandler)4 Formatter (java.util.logging.Formatter)3 Logger (java.util.logging.Logger)3 Config (edu.neu.ccs.pyramid.configuration.Config)2 Setup (org.openjdk.jmh.annotations.Setup)2 FileLogFormatter (org.opensolaris.opengrok.logger.formatter.FileLogFormatter)2 LogFormatter (com.facebook.buck.log.LogFormatter)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 NaviLogFormatter (com.google.security.zynamics.binnavi.Log.NaviLogFormatter)1 FileReadException (com.google.security.zynamics.binnavi.config.FileReadException)1 RcvrExceptionHandler (edu.cmu.cs.hcii.cogtool.ui.RcvrExceptionHandler)1 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)1 NewlineLogFormatter (edu.stanford.nlp.util.logging.NewlineLogFormatter)1 FileInputStream (java.io.FileInputStream)1