incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ran...@apache.org
Subject cvs commit: incubator-ftpserver/src/java/org/apache/ftpserver/logger AbstractLogger.java CompositeLogger.java ConsoleLogger.java FileLogger.java NullLogger.java
Date Wed, 07 Sep 2005 05:03:18 GMT
rana_b      2005/09/06 22:03:18

  Added:       src/java/org/apache/ftpserver/logger AbstractLogger.java
                        CompositeLogger.java ConsoleLogger.java
                        FileLogger.java NullLogger.java
  Log:
  Refactored - removed avalon entirely
  
  Revision  Changes    Path
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/logger/AbstractLogger.java
  
  Index: AbstractLogger.java
  ===================================================================
  // $Id: AbstractLogger.java,v 1.1 2005/09/07 05:03:18 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * 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.apache.ftpserver.logger;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.ftplet.Logger;
  
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Locale;
  
  
  /**
   * Abstract logger class.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  abstract class AbstractLogger implements Logger {
  
      protected final static String LINE_SEP = System.getProperty("line.separator", "\n");
      protected final static String[] LOG_LABELS = {"DEBUG", "INFO", "WARN", "ERROR"};
      
      
      public final static int LEVEL_DEBUG = 0;
      public final static int LEVEL_INFO  = 1;
      public final static int LEVEL_WARN  = 2;
      public final static int LEVEL_ERROR = 3;
      public final static int LEVEL_NONE  = 4;
  
      private int m_level = LEVEL_INFO;
      private SimpleDateFormat m_fmt;
      
      
      /**
       * Set logger - does nothing.
       */
      public void setLogger(Logger logger) {
      }
      
      /**
       * Configure - set the level
       */
      public void configure(Configuration conf) throws FtpException {
          m_level = conf.getInt("level", LEVEL_INFO);
          m_fmt   = new SimpleDateFormat(conf.getString("date-format", "yyyy-MM-dd'T'HH:mm:ss"),
Locale.US);
      }
      
      /**
       * Destroy - does nothing
       */
      public void dispose() {
      }
      
      /**
       * Get date format.
       */
      public DateFormat getDateFormat() {
          return m_fmt;
      }
       
      /**
       * Check the log enable status
       */
      protected boolean canBeWritten(int level) {
          return level >= m_level;
      }
      
      /**
       * Write debug message
       */
      public void debug(String msg) {
          if(canBeWritten(LEVEL_DEBUG)) {
              write(LEVEL_DEBUG, msg);
          }
      }
  
      /**
       * Write debug throwable object
       */
      public void debug(String msg, Throwable th) {
          if(canBeWritten(LEVEL_DEBUG)) {
              write(LEVEL_DEBUG, msg, th);
          }
      }    
      
      /**
       * Write info message
       */
      public void info(String msg) {
          if(canBeWritten(LEVEL_INFO)) {
              write(LEVEL_INFO, msg);
          }
      }
  
      /**
       * Write info throwable object
       */
      public void info(String msg, Throwable th) {
          if(canBeWritten(LEVEL_INFO)) {
              write(LEVEL_INFO, msg, th);
          }
      }
  
      /**
       * Write warning message
       */
      public void warn(String msg) {
          if(canBeWritten(LEVEL_WARN)) {
              write(LEVEL_WARN, msg);
          }
      }
  
      /**
       * Write warning throwable object
       */
      public void warn(String msg, Throwable th) {
          if(canBeWritten(LEVEL_WARN)) {
              write(LEVEL_WARN, msg, th);
          }
      }
  
      /**
       * Write error message
       */
      public void error(String msg) {
          if(canBeWritten(LEVEL_ERROR)) {
              write(LEVEL_ERROR, msg);
          }
      }
  
      /**
       * Write warning throwable object
       */
      public void error(String msg, Throwable th) {
          if(canBeWritten(LEVEL_ERROR)) {
              write(LEVEL_ERROR, msg, th);
          }
      }    
       
      /**
       * Write log message.
       */
      protected abstract void write(int level, String msg);
      
      /**
       * Write log message and exception.
       */
      protected abstract void write(int level, String msg, Throwable th);
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/logger/CompositeLogger.java
  
  Index: CompositeLogger.java
  ===================================================================
  // $Id: CompositeLogger.java,v 1.1 2005/09/07 05:03:18 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * 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.apache.ftpserver.logger;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.ftplet.Logger;
  
  import java.util.ArrayList;
  
  /**
   * This composite logger writes into multiple logger.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class CompositeLogger implements Logger {
  
      private ArrayList m_loggers = new ArrayList();
      
      /**
       * Add logger.
       */
      public void addLogger(Logger logger) {
          m_loggers.add(logger);
      }
      
      /**
       * Remove logger.
       */
      public void removeLogger(Logger logger) {
          m_loggers.remove(logger);
      }
      
      /**
       * Write debug message.
       */
      public void debug(String msg) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.debug(msg);
          }
      }
  
      /**
       * Write debug message. 
       */
      public void debug(String msg, Throwable th) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.debug(msg, th);
          }
      }
  
      /**
       * Write info message 
       */
      public void info(String msg) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.info(msg);
          }
      }
  
      /**
       * Write infor message. 
       */
      public void info(String msg, Throwable th) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.info(msg, th);
          }
      }
  
      /**
       * Write warnning message 
       */
      public void warn(String msg) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.warn(msg);
          }
      }
  
      /**
       * Write warnning message. 
       */
      public void warn(String msg, Throwable th) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.warn(msg, th);
          }
      }
  
      /**
       * Write warning message. 
       */
      public void error(String msg) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.error(msg);
          }
      }
  
      /**
       * Write warnning message 
       */
      public void error(String msg, Throwable th) {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.error(msg, th);
          }
      }
  
      /**
       * It does nothing 
       */
      public void setLogger(Logger logger) {
      }
  
      /**
       * It does nothing. 
       */
      public void configure(Configuration config) throws FtpException {
      }
  
      /**
       * Dispose all. 
       */
      public void dispose() {
          for(int i=m_loggers.size(); --i>=0; ) {
              Logger logger = (Logger)m_loggers.get(i);
              logger.dispose();
          }
          m_loggers.clear();
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/logger/ConsoleLogger.java
  
  Index: ConsoleLogger.java
  ===================================================================
  // $Id: ConsoleLogger.java,v 1.1 2005/09/07 05:03:18 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * 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.apache.ftpserver.logger;
  
  import java.util.Date;
  
  /**
   * Redirect all the log messages to console (System.out).
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class ConsoleLogger extends AbstractLogger {
  
      /**
       * Print log message.
       */
      public void write(int level, String msg) {
          String logStr = "[" + getDateFormat().format(new Date()) + "] " +
                          "(" + LOG_LABELS[level] + ") " + 
                          msg;
          System.out.println(logStr);
      }
      
      /**
       * Print log message and exception.
       */
      public void write(int level, String msg, Throwable th) {
          write(level, msg);
          th.printStackTrace();
      } 
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/logger/FileLogger.java
  
  Index: FileLogger.java
  ===================================================================
  // $Id: FileLogger.java,v 1.1 2005/09/07 05:03:18 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * 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.apache.ftpserver.logger;
  
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.RandomAccessFile;
  import java.io.Writer;
  import java.util.Date;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.FtpException;
  import org.apache.ftpserver.util.DateUtils;
  import org.apache.ftpserver.util.IoUtils;
  
  /**
   * Log class to write log data. It uses <code>RandomAccessFile</code>.
   * If the log file size exceeds the limit, a new log file will be created.
   *
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class FileLogger extends AbstractLogger {
  
      private final static long NO_LIMIT = 0;
      
      private Writer m_writer;
      private long m_currentSize;
      
      private File m_logDir;
      private File m_file;
      private long m_maxSize = NO_LIMIT;
      private boolean m_autoFlush;
      
  
      /**
       * Configure the logger
       */
      public void configure(Configuration conf) throws FtpException {
          super.configure(conf);
          
          m_logDir = new File(conf.getString("dir", "./res/log"));
          m_file = new File(m_logDir, "log.gen");
          m_maxSize = conf.getLong("max-size", NO_LIMIT);
          m_autoFlush = conf.getBoolean("auto-flush", true);
          
          open();
      }
  
      /**
       * Open log file.
       */
      private synchronized void open() throws FtpException {
          FileWriter fw = null;
          try {
              
              // create directory if does not exist
              File logDir = m_file.getParentFile();
              if( (!logDir.exists()) && (!logDir.mkdirs()) ) {
                  String dirName = logDir.getAbsolutePath();
                  throw new FtpException("Cannot create directory : " + dirName);
              }
              
              RandomAccessFile raf = new RandomAccessFile(m_file, "rw");
              raf.seek(raf.length());
              fw = new FileWriter(raf.getFD()); 
              m_writer = new BufferedWriter(fw);
          } 
          catch (IOException ex) {
              IoUtils.close(fw);
              m_writer = null;
              throw new FtpException("FileLogger.open()", ex);
          }
      }
  
      /**
       * Write log message
       */
      protected void write(int level, String msg) {
          String logStr = "[" + getDateFormat().format(new Date()) + "] " +
                          "(" + LOG_LABELS[level] + ") " + 
                          msg + LINE_SEP;
          write(logStr);
      }
  
      /**
       * Write log message and exception
       */
      protected void write(int level, String msg, Throwable th) {
          String logStr = "[" + getDateFormat().format(new Date()) + "] " +
                          "(" + LOG_LABELS[level] + ") " + 
                          msg + LINE_SEP + IoUtils.getStackTrace(th);
          write(logStr);
      }
      
      /**
       * Write string.
       */
      protected void write(String msg) {
          if (m_writer != null) {
  
              // save file if needed
              long strLen = msg.length();
              if ( (m_maxSize > NO_LIMIT) && ((strLen + m_currentSize) > m_maxSize)
) {
                  save();
              }
              
              // now write it
              try {
                  m_writer.write(msg);
                  if (m_autoFlush) {
                      m_writer.flush();
                  }
                  m_currentSize += strLen;
              } 
              catch (Exception ex) {
                  IoUtils.close(m_writer);
                  m_writer = null;
              }
          }
      }
      
      /**
       * Set the log file and open a new log file.
       * Returns the name of the saved log file.
       */
      private synchronized void save() {
          try {
              dispose();
              
              String toFileName = DateUtils.getISO8601Date(System.currentTimeMillis()) + "-log.gen";
              File toFile = new File(m_logDir, toFileName);
              toFile = IoUtils.getUniqueFile(toFile);
              m_file.renameTo(toFile);
              open();
          } 
          catch (Exception ex) {
              m_writer = null;
          }
      } 
  
  
      /**
       * Dispose - close stream
       */
      public void dispose() {
          if (m_writer != null){
              IoUtils.close(m_writer);
              m_writer = null;
          }
          super.dispose();
      }
  }
  
  
  
  1.1                  incubator-ftpserver/src/java/org/apache/ftpserver/logger/NullLogger.java
  
  Index: NullLogger.java
  ===================================================================
  // $Id: NullLogger.java,v 1.1 2005/09/07 05:03:18 rana_b Exp $
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * 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.apache.ftpserver.logger;
  
  import org.apache.ftpserver.ftplet.Configuration;
  import org.apache.ftpserver.ftplet.Logger;
  
  /**
   * Null logger implementation.
   * 
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public 
  class NullLogger implements Logger {
  
      /**
       * Set logger - does nothing
       */
      public void setLogger(Logger logger) {
      }
      
      /**
       * Does nothing
       */
      public void configure(Configuration config) {
      }
      
      /**
       * Does nothing
       */
      public void dispose() {
      }
  
      /**
       * Does nothing
       */
      public void debug(String msg) {
      }
      
      /**
       * Does nothing
       */
      public void debug(String msg, Throwable th) {
      }
  
      /**
       * Does nothing
       */
      public void info(String msg) {
      }
  
      /**
       * Does nothing
       */
      public void info(String msg, Throwable th) {
      }
  
      /**
       * Does nothing
       */
      public void warn(String msg) {
      }
  
      /**
       * Does nothing
       */
      public void warn(String msg, Throwable th) {
      }
  
      /**
       * Does nothing
       */
      public void error(String msg) {
      }
      
      /**
       * Does nothing
       */
      public void error(String msg, Throwable th) {
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message