位置:org.springframework.util
�实现接口:无
继承类:java.io.Write
作用:日志Log与字符流Writer的适配器
一、效果
传入指定Log对象,为提供的Log对象创建一个新的CommonsLogWriter,便于使用字符流记录日志语句。
二、API
// 传入Log实现的构造器
public CommonsLogWriter(Log logger) {
Assert.notNull(logger, "Logger must not be null");
this.logger = logger;
}
// 将给定字符添加到成员StringBuilder的实例中,如果是StringBuilder实例长度大于0且
给定字符是换行符,则调用Log成员的debug方法。
public void write(char ch) {
if (ch == '\n' && this.buffer.length() > 0) {
this.logger.debug(this.buffer.toString());
this.buffer.setLength(0);
}
else {
this.buffer.append(ch);
}
}
// 给定要记录日志的字符数组,并提供要写入日志的起始位置与截止长度。用于记录日志
public void write(char[] buffer, int offset, int length) {
for (int i = 0; i < length; i++) {
char ch = buffer[offset + i];
if (ch == '\n' && this.buffer.length() > 0) {
this.logger.debug(this.buffer.toString());
this.buffer.setLength(0);
}
else {
this.buffer.append(ch);
}
}
}
三、总结
四、补充
无
参考资料: CommonsLogWriter