1、首先学习了一下log4j对日志事件也即LogEvent的定义方式:
public class LogEvent implements Serializable {
  private static final long serialVersionUID = 1L;
  private long time;
  private transient Object msg;// 不被序列化
  private ThrowableInfo throwableInfo;

  public LogEvent(Object msg, Throwable throwable) {
    time = System.currentTimeMillis();
    this.msg = msg;
    this.throwableInfo = new ThrowableInfo(throwable);
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("time:").append(new Date(time)).append("\n");
    sb.append("msg:").append(msg).append("\n");
    sb.append("throwableInfo:").append(this.throwableInfo.toString());
    return sb.toString();
  }
}

class ThrowableInfo implements Serializable {
  private static final long serialVersionUID = 1L;
  private transient Throwable throwable;
  private String[] rep;

  public ThrowableInfo(Throwable throwable) {
    this.throwable = throwable;
  }

  public String[] getRep() {
    if (rep != null) {
      return (String[]) rep.clone();
    }
    if (this.throwable == null) {
      return null;
    }
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    this.throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader lnr = new LineNumberReader(new StringReader(sw.toString()));
    List<String> lines = new ArrayList<String>();
    try {
      String line = lnr.readLine();
      while (line != null) {
        lines.add(line);
        line = lnr.readLine();
      }
    } catch (IOException e) {
      lines.add(e.toString());
    }
    rep = new String[lines.size()];
    lines.toArray(rep);
    return rep;
  }

  public String toString() {
    rep = rep == null ? getRep() : null;
    if (rep == null || rep.length == 0) {
      return "";
    }
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < rep.length; i++) {
      sb.append("line ").append(i).append(":").append(rep[i]).append("\n");
    }
    return sb.toString();
  }
}
 
测试:
  public static void main(String[] args) {
    try {
      FileReader fr = new FileReader(new File("nonon.non"));
      System.out.println(fr.read());
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("-------------------");
      LogEvent event = new LogEvent("出现了异常", e);
      System.out.println(event);
    }
  }
 
输出结果如下:
-------------------
time:Sat Apr 18 16:02:16 CST 2009
msg:出现了异常
throwableInfo:line 0:java.io.FileNotFoundException: nonon.non (系统找不到指定的文件。)
line 1:  at java.io.FileInputStream.open(Native Method)
line 2:  at java.io.FileInputStream.<init>(Unknown Source)
line 3:  at java.io.FileReader.<init>(Unknown Source)
line 4:  at test.LogEvent.main(LogEvent.java:37)

java.io.FileNotFoundException: nonon.non (系统找不到指定的文件。)
  at java.io.FileInputStream.open(Native Method)
  at java.io.FileInputStream.<init>(Unknown Source)
  at java.io.FileReader.<init>(Unknown Source)
  at test.LogEvent.main(LogEvent.java:37)
 
2、JAVA对象的序列化:
public class SerialTest implements Serializable {
  private static final long serialVersionUID = 1L;
  private String str;
  private double db;
  private int intValue;
  private boolean bool;
  private Date date;
  private transient String foobar;// foobar通过transient关键字标识为不进行序列化

  public SerialTest(boolean bool, Date date, double db, int intValue, String str,
      String foobar) {
    super();
    this.bool = bool;
    this.date = date;
    this.db = db;
    this.intValue = intValue;
    this.str = str;
    this.foobar = foobar;
  }

  public String toString() {
    return "str:" + str + " db:" + db + " intValue:" + intValue + " bool:" + bool
        + " date:" + date.getTime() + " foobar:" + foobar;
  }
}
 
测试:
  public static void main(String[] args) {
    SerialTest st = new SerialTest(true, new Date(), 1.234, 234, "goodjob", "foobar");
    System.out.println(st);

    try {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("st.txt"));
      oos.writeObject(st);
      oos.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream("st.txt"));
      st = (SerialTest) ois.readObject();
      ois.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(st);
  }
 
输出结果如下:
str:goodjob db:1.234 intValue:234 bool:true date:1240041883984 foobar:foobar
str:goodjob db:1.234 intValue:234 bool:true date:1240041883984 foobar:null