清单 1
:描述由字符串连接符‘+
’所产生的字节代码操作的伪代码
|
create new String (STR_1)
duplicate the String
load the constant String "Joe" (STR_2)
call String constructor
store this String in the local variable array position 0
get the static out field from the java.io.PrintStream class (OUT)
create a new StringBuffer (STR_BUF_1)
duplicate the StringBuffer
call StringBuffer constructor
store this StringBuffer in the local variable array position 1
invoke the append method on STR_BUF_1 with STR_1 as the argument
load the constant String " is my name." (STR_3)
invoke the append method on STR_BUF_1 with STR_3 as the argument
invoke toString method on STR_BUF_1 (STR_4)
invoke the println method on OUT
|
清单
2
:使用
StringBuffer
的
append
操作符的字节代码
/
伪代码
|
create new StringBuffer (STR_BUF_1)
duplicate the StringBuffer
load the constant String "Joe" (STR_1)
call StringBuffer constructor
store this StringBuffer in the local variable array position 1
get the static out field from the java.io.PrintStream class (OUT)
load STR_BUF_1
load the constant String " is my name." (STR_2)
invoke the append method on STR_BUF_1 with STR_2 as the argument
invoke toString method on STR_BUF_1 (STR_3)
invoke the println method on OUT
|
清单3
:可以进行配置使其在发布版本中不产生代码的]
单元素的Logger
类
|
public class Logger {
//
这个类的实例
private static Logger theLogger = null;
//
调试消息(
debug messages
)的控制器,设为
true
时允许调试消息,
false
则反之
public static final boolean CAN_DEBUG = true;
/**
私有的构造函数——只允许一个实例
*/
private Logger() {}
/**
返回这个类所创建的唯一实例
*/
public static Logger getInstance() {
if(theLogger == null) { theLogger = new Logger(); }
return theLogger;
}
public void debugMsg(String msg) { System.out.println(msg); }
}
|
清单4
:提供可配置日志记录级别Logger
类
|
public class Logger {
...
//
信息消息的控制器
public static final int CAN_INFO = 1;
//
调试消息的控制器
public static final int CAN_DEBUG = 2;
//
调试级别——缺省为信息消息
public int LOG_LEVEL = 1;
...
public void setLogLevel(int level) {
if(level >= 0) { LOG_LEVEL = level; }
}
/**
如果
CAN_INFO
位被设置则返回
true */
public boolean canInfo() {
if((LOG_LEVEL & CAN_INFO) == CAN_INFO) { return true; }
return false;
}
/**
如果
CAN_DEBUG
位被设置则返回
true */
public boolean canDebug() {
if((LOG_LEVEL & CAN_DEBUG) == CAN_DEBUG) { return true; }
return false;
}
public void debugMsg(String msg) { System.out.println(msg); }
public void infoMsg(String msg) { System.out.println(msg); }
}
|
清单5
:TestLogger.java
—定义简单API
的接口的示例实现
|
/* Copyright (c) 2000 Stepping Stone Software Ltd, John Keyes */
public class TestLogger implements LogAPI {
static Logger myLogger = Logger.getInstance();
StringBuffer msg = new StringBuffer();
public static void main(String args[]) {
String strLevel = System.getProperty("app.loglevel");
if(strLevel != null) {
int level = Integer.parseInt(strLevel);
myLogger.setLogLevel(level);
}
TestLogger testLog = new TestLogger();
testLog.test();
}
TestLogger() { }
public void test() {
int age = 24;
String name = "Joe";
if(myLogger.canDebug()) {
createMsg();
appendLog(" DEBUG/n Name:");
appendLog(name);
appendLog(" Age:");
appendLog(age);
logDebugMsg();
}
if(myLogger.canInfo()) {
createMsg();
appendLog(" INFO/n Name:");
appendLog(name);
appendLog(" Age:");
appendLog(age);
logInfoMsg();
}
}
public void createMsg() { msg.setLength(0); }
public void appendLog(String str) { msg.append(str); }
public void appendLog(int i) { msg.append(i); }
public void logDebugMsg() {
myLogger.debugMsg(msg.toString());
}
public void logInfoMsg() {
myLogger.infoMsg(msg.toString());
}
}
|
标签进行设置:
于是,为了设置日志记录级别,你所要做的一切就是获取属性值并对结果调用SetLogLevel
方法:
String strLevel = System.getProperty("app.loglevel");
If(strLevel != null) {
int level = Integer.parseInt(strLevel);
Logger.getInstance().setLogLevel(level);
}
这种方法的好处在于:
l
减少对象创建,也就是说,对象被重用
l
|