-
Notifications
You must be signed in to change notification settings - Fork 0
netbeas SLF4J 代码模版
Overview Logging is a very useful mechanism for tracing problems that occur after you've deployed an application. The code for including a logger is standard boiler plate and can be quickly added using NetBeans Code Templates. SLF4J The use of logging facades is quite common in Java. Facades allow the developer to abstract away the specific logger implementation while developing and then plug-in the most appropriate implementation at runtime. (Such as Log4j, JDK Logging, etc.). For example, many Apache projects use commons-logging. Unfortunatly, commons-logging can give developers headaches; slf4j was introduced in order to avoid some of the issues with commons-logging. From http://www.slf4j.org: The Simple Logging Facade for Java or (SLF4J) is intended to serve as a simple facade for various logging APIs allowing to the end-user to plug in the desired implementation at deployment time. SLF4J also allows for a gradual migration path away from Jakarta Commons Logging (JCL). Adding the Code Template To add a logging code template in NetBeans do the following: Open the Options'" Dialog (Mac: Netbeans->Preferences, Other OS's: Tools->Options) Select the Editor button at the top of the dialog Select the Code Templates tab Click the New button Enter slog in the New Code Template Dialog box In the Expanded Text area add the following:
private final static ${loggerType type="org.slf4j.Logger" default="Logger" editable="false"}
log = ${loggerTypeFactory type="org.slf4j.LoggerFactory"
default="LoggerFactory" editable="false"}.getLogger(
${classVar editable="false" currClassName default="getClass()"}.class);
Using the Code Template To use the code template while editing a Java class you simply type the following: slog followed by tab. This will insert the following code into your class: import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...
private final static Logger log = LoggerFactory.getLogger(CurrentClass.class );