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

📄 logformatter.java

📁 STRUTS数据库项目开发宝典
💻 JAVA
字号:


package com.helpsoft.util.log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * A class wich specifies the format for the log messages
 * @author caoguangxin- www.relationinfo.com

 */
public final class LogFormatter extends Formatter {

   /** a variable for specifying a given data format */
   private SimpleDateFormat dateFormatter;

   /**
    * a variable the will set the number of last packages will be shown
    * example number=3 com.helpsoft.appname.util.log.LogFormatter
    * will be util.log.LogFormatter
    */
   private int showNumberOfLastPackages;

   /** a variable for specifying the separator between the Classname and the message */
   private String messageSeparator;


   /**
    * Constrcutor for making a LogFormatter
    * @param showNumberOfLastPackages the number of packages to log
    * @param datePattern the pattern of the date to log
    * @param messageSeparator the message separator
    */
   public LogFormatter(int showNumberOfLastPackages, String datePattern, String messageSeparator) {
      this.dateFormatter = new SimpleDateFormat(datePattern);
      this.messageSeparator = messageSeparator;
      this.showNumberOfLastPackages = showNumberOfLastPackages;
   }


   /**
    * Formatting a logrecord to a single line.
    * sequence of the logline: Logevel date Classname separator message exception
    * @param rec the log record to format
    * @return String the line to log
    */
   public String format(LogRecord rec) {
      StringBuffer buf = new StringBuffer(1000);
      StringBuffer packageClass = new StringBuffer(50);
      String loggerName = rec.getLoggerName();

      if (this.showNumberOfLastPackages <= 0) {
         packageClass.append(loggerName);
      }

      for (int i = 0; i < this.showNumberOfLastPackages; i++) {
         int index = loggerName.lastIndexOf(".");

         if (index > -1) {
            packageClass.insert(0, loggerName.substring(index, loggerName.length()));
            loggerName = loggerName.substring(0, index);
         }
         else {
            packageClass.insert(0, loggerName);
            break;
         }
      }
      if (packageClass.charAt(0) == '.') {
         packageClass.deleteCharAt(0);
      }
      //String level = rec.getLevel().toString();
      //level = (level.length() >= 5) ? level.substring(0, 5) : level + " ";

      buf.append(rec.getLevel());
      buf.append("     ");
      buf.delete(8, buf.length());
      buf.insert(8, dateFormatter.format(new Date(rec.getMillis())));
      buf.append(" ");
      buf.append(packageClass);
      buf.append(" ");
      buf.append(this.messageSeparator);
      buf.append(" ");
      buf.append(formatMessage(rec));
      buf.append((rec.getThrown() != null) ? rec.getThrown().toString() : "");
      buf.append('\n');
      return buf.toString();
   }
}

⌨️ 快捷键说明

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