log.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 97 行

JAVA
97
字号
/*
#=========================================================================
# Copyright 2003 SRI International.  All rights reserved.
#
# The material contained in this file is confidential and proprietary to SRI
# International and may not be reproduced, published, or disclosed to others
# without authorization from SRI International.
#
# DISCLAIMER OF WARRANTIES
#
# SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
# SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
# LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
# OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
#=========================================================================
  Author : shardt
  Date: Aug 18, 2003
*/
package com.sri.oaa2.tools.oaatest;

import java.io.*;
import org.apache.log4j.*;
import org.apache.log4j.xml.*;


/** Log some messages to the console using log4j.  Wrapping log4j calls
 * in this class to avoid static initialization problems.  Sometimes
 * we're called as OaaTest.suite(), OaaTest.main(), or SelfTest.suite()
 */
class Log {
  private static final boolean useLog4j = true;

  /**
   * Property name for the OAA library log4j configuration file.
   */
  private static final String LOG_CONFIG_FILE = "oaatest.log.config.file";

  /**
   * Default property value for the OAA library log4j configuration file.
   */
  private static final String LOG_CONFIG_FILE_DEFAULT = "com/sri/oaa2/tools/oaatest/log_config.xml";

  private static Log log;
  
  // Get singleton Log object, create if necessary.
  static Log singleton() {
    if (log == null) {
      log = new Log();
    }
    return log;
  }

  private Log() {
    if (useLog4j) {
      // Set up logging from config file.
      String logconfigName = System.getProperty(LOG_CONFIG_FILE,
                                              LOG_CONFIG_FILE_DEFAULT);
      InputStream logConfig = ClassLoader.getSystemResourceAsStream(logconfigName);
      if (logConfig == null) {
        throw new RuntimeException("Could not find log configuration file " + logconfigName);
      }
      try {
        new DOMConfigurator().doConfigure(logConfig,LogManager.getLoggerRepository());
      }
      catch(Exception e) {
        RuntimeException re = new RuntimeException(e.toString());
        re.fillInStackTrace();
        throw re;
      }

      logger = Logger.getLogger(Log.class); 
    }
  }
  
  void info(String msg) {
    if (useLog4j) {
      logger.info(msg);
    }
    else {
      System.out.println(msg);
    }
  }
  
  void error(String msg) {
    if (useLog4j) {
      logger.error(msg);
    }
    else {
      System.err.println(msg);
    }
  }

  Logger logger;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?