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

📄 httplog.java

📁 21天学通java的示例程序源代码
💻 JAVA
字号:
// HTTPLog.java

package com.wrox.httpserver;

import java.io.*;
import java.util.*;

/**
 * This class logs both access and error messages.
 */
class HTTPLog extends HTTPConstants {

  // Convenience logger object
  public static HTTPLog logger = null;

  // Localized messages
  private HTTPLocalizedResources resources;

  // Instance variables
  private BufferedWriter logfileWriter;

  /**
   * Constructs an HTTPLog object.
   */
  protected HTTPLog(File logfile) throws IOException {
    try {
      resources = new HTTPLocalizedResources("msg.httplog");
    } catch (MissingResourceException mre) {

      // The resource is missing, throw exception with error message
      throw new IOException(resources.getResourceString(RESOURCE_ERROR));
    } 

    // Initialize instance variables, append to the logfile
    this.logfileWriter = 
      new BufferedWriter(new FileWriter(logfile.toString(), true));
  }

  /**
   * Flushes to file and closes the logger.
   */
  public void close() {
    try {
      logfileWriter.flush();
      logfileWriter.close();
    } catch (IOException e) {

      // Ignore exceptions when closing the logfile
    } 
  } 

  /**
   * Initializes the logger.
   */
  public static void initializeLogger(File logfile) throws IOException {
    logger = new HTTPLog(logfile);
  } 

  /**
   * Append a log entry.
   */
  public void log(HTTPInformation info) {
    String logMessage = info.remoteAddr + "-[" + new HTTPGMTTimestamp() 
                        + "]-" + info.requestString + "-" + info.status 
                        + "\r\n";
    synchronized (logfileWriter) {
      try {
        logfileWriter.write(logMessage);
      } catch (IOException e) {

        // The log could not be written, write out at least
        // a warning message to the console so that the
        // user is informed of the problem
        System.err
          .println(resources.getResourceString("ERROR_WRITING_LOG"));

      } 
    } 
  } 
}

⌨️ 快捷键说明

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