LOG4J中提供了一个通过邮件发送日志信息的方式:SMTPAppender,
注意:需要引用到JAVA MAIL和 JavaBeans(tm) Activation Framework(JAF)
1、试用SMTPAppender:
配置文件的设定:
log4j.rootLogger=debug, MAIL
log4j.debug = true
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=DEBUG
# 最多缓存128个日志事件
log4j.appender.MAIL.BufferSize=128
log4j.appender.MAIL.SMTPHost=smtp.163.com
log4j.appender.MAIL.Subject=Log4J ErrorMessage
# 此处对应实际的帐号和密码
log4j.appender.MAIL.SMTPUsername=test
log4j.appender.MAIL.SMTPPassword=test
# 此处对应实际的收发邮箱
[email protected]
[email protected]
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.debug = true
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=DEBUG
# 最多缓存128个日志事件
log4j.appender.MAIL.BufferSize=128
log4j.appender.MAIL.SMTPHost=smtp.163.com
log4j.appender.MAIL.Subject=Log4J ErrorMessage
# 此处对应实际的帐号和密码
log4j.appender.MAIL.SMTPUsername=test
log4j.appender.MAIL.SMTPPassword=test
# 此处对应实际的收发邮箱
[email protected]
[email protected]
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage] %d - %c -%-4r [%t] %-5p %c %x - %m%n
测试类:
public
class TestLog4j {
private static Logger log = Logger.getLogger(TestLog4j. class);
public static void main(String[] args) {
PropertyConfigurator.configure( "F:\\dev_test\\log\\test\\log4j.properties");
// 下面的三个日志事件debug、debug2、error将一次性发送出去
log.debug( "debug");
log.debug( "debug2");
log.error( "error", new IOException( "ioex"));
// 下面的error2又将发送一封邮件
log.error( "error2", new IOException( "ioex2"));
}
}
private static Logger log = Logger.getLogger(TestLog4j. class);
public static void main(String[] args) {
PropertyConfigurator.configure( "F:\\dev_test\\log\\test\\log4j.properties");
// 下面的三个日志事件debug、debug2、error将一次性发送出去
log.debug( "debug");
log.debug( "debug2");
log.error( "error", new IOException( "ioex"));
// 下面的error2又将发送一封邮件
log.error( "error2", new IOException( "ioex2"));
}
}
输出:
邮箱收到两封邮件
第一封邮件的内容如下:
第二封的内容:
2、LOG4J发邮件实现了一个循环缓冲类CyclicBuffer,下面是我的一个简单实现:
public
class MyCyclicBuffer {
private int max;
/** 第一个 */
private int first;
/** 最后一个 */
private int last;
private int num;
private Object[] buf;
public MyCyclicBuffer( int maxSize) {
this.max = maxSize;
this.buf = new Object[ this.max];
}
public void add(Object obj) {
if (obj == null || this.max < 1) {
return;
}
buf[last] = obj;
last = ++last == max ? 0 : last;
if (num < max) {
num++;
} else {
first = ++first == max ? 0 : first;
}
}
/** 取得第i个对象 */
public Object getObj( int i) {
if (i < 0 || i >= max) {
return null;
}
return buf[(i + first) % max];
}
public static void main(String[] args) {
MyCyclicBuffer mcb = new MyCyclicBuffer(10);
for ( int i = 0; i <= 20; i++) {
mcb.add( "string" + i);
}
// 取得最后一个元素
System.out.println(mcb.getObj(9));
// 取得第一个元素
System.out.println(mcb.getObj(0));
}
}
private int max;
/** 第一个 */
private int first;
/** 最后一个 */
private int last;
private int num;
private Object[] buf;
public MyCyclicBuffer( int maxSize) {
this.max = maxSize;
this.buf = new Object[ this.max];
}
public void add(Object obj) {
if (obj == null || this.max < 1) {
return;
}
buf[last] = obj;
last = ++last == max ? 0 : last;
if (num < max) {
num++;
} else {
first = ++first == max ? 0 : first;
}
}
/** 取得第i个对象 */
public Object getObj( int i) {
if (i < 0 || i >= max) {
return null;
}
return buf[(i + first) % max];
}
public static void main(String[] args) {
MyCyclicBuffer mcb = new MyCyclicBuffer(10);
for ( int i = 0; i <= 20; i++) {
mcb.add( "string" + i);
}
// 取得最后一个元素
System.out.println(mcb.getObj(9));
// 取得第一个元素
System.out.println(mcb.getObj(0));
}
}
输出:
string20
string11
string11
3、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;
}
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);
}
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:1240049508484 foobar:foobar
str:goodjob db:1.234 intValue:234 bool:true date:1240049508484 foobar:null
str:goodjob db:1.234 intValue:234 bool:true date:1240049508484 foobar:null