⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log4jlogger.java

📁 数据仓库工具
💻 JAVA
字号:
/**
    Copyright (C) 2002-2003  Together
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111--1307  USA
 * $Id: Log4jLogger.java,v 1.3 2004/06/01 13:36:13 zoran Exp $
 */
package org.webdocwf.util.loader.logging;

import java.util.*;
import java.io.*;
import org.webdocwf.util.loader.BufferOctopusClass;

import java.util.Hashtable;
import java.io.*;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jLogger
    extends org.webdocwf.util.loader.logging.Logger{

  /**
   * Log file name.
   */
  File activeLogFile;

  /**
   * Log file writter.
   */
  PrintWriter logFileStream;

  private boolean[] enabledLogLevels = new boolean[3];
  private Hashtable messages = new Hashtable();
  private String logMode;
  private org.apache.log4j.Logger logger;

  /**
   * Construct a new logger.  Configuration is not done now, to allow
   * the logger to be created very early.
   */
  public Log4jLogger() {
    centralLogger = this;
  }

  /**
   * Configure Logger with given config file, interpreting of config file is
   * logger implementation specific.
   *
   * @param log4jConfFile is Path to configuration file.
   * @throws Exception
   */
  public void configure(String log4jConfFile) throws Exception {

    try {
      DOMConfigurator.configure(log4jConfFile);
      this.logger=Logger.getLogger("OctopusLogger");
    }
    catch (javax.xml.parsers.FactoryConfigurationError fce) {
      throw new Exception("Cannot configure Log4jLogger! FactoryConfigurationError occured!");

    }
  }

  public int getLevel(String level) {
    if (level.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE))
      return 0;
    else if (level.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL))
      return 1;
    else
      return 2;
  }

  public boolean isEnabled(int level) {
    return true;
  }

  public boolean isEnabled(String level) {
//    return isEnabled(this.getLevel(level));
  return true;
  }

  public void write(int level, String msg) {
    Level lev;
    if (this.logger == null) {
      this.logger = Logger.getLogger("OctopusLogger");
    }
    if(level==1)
      lev=Level.INFO;
    else if( level == 2 )
      lev=Level.DEBUG;
    else
      lev = Level.INFO;

    logger.log(lev, msg);
  }

  public synchronized void write(String level, String msg) {
    write(getLevel(level), msg);
  }

  public synchronized void write(int level, String msg, Throwable throwable) {
    Date date = new Date();
    StringWriter stackBuf = new StringWriter();
    throwable.printStackTrace(new PrintWriter(stackBuf));
    stackBuf.flush();

    String errMsg = msg + ":" + " " + throwable.getMessage() + '\n' + stackBuf;
    this.write(level, errMsg);

  }

  public synchronized void write(String level, String msg, Throwable throwable) {
    write(getLevel(level), msg, throwable);
  }

  public void setEnabledLogLevels(String logMode) {
  }

  public boolean[] getEnabledLogLevels() {
    return enabledLogLevels;
  }

  public String getMessage(String key) {
    if (key != null) {
      return (String)this.messages.get(key);
    } else
      return null;
  }

  public boolean setMessage(String key, String value) {
    if (value != null && key != null) {
      this.messages.put(key, value);
      return true;
    } else
      return false;
  }

  public boolean writeEcho(String strLogTxt) {
    if(!this.logMode.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE)) {
      this.write(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL,strLogTxt);
      BufferOctopusClass.getInstance().writeToBuffer(strLogTxt);
      return true;
    }
    else
    return false;
  }

  public void close(){
  //
  }
}

⌨️ 快捷键说明

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