When I was working on a project a few days ago, I wrote system logs in every method.CodeIt is complicated to add. Considering that spring has the function of AOP, I am thinking about using AOP for this log function.
First, you need to input the specific operation name of the log record. We can use the Java annotation function to bring the parameter. The Code is as follows:
/**
* Class method description Annotation
* @ Author luoyu
*/
@ Target (elementtype. Method)
@ Retention (retentionpolicy. runtime)
@ Brief ented
@ Inherited
Public@InterfaceLog {
/** Operation type to be executed, such as: add operation **/
PublicString operationtype ()Default"";
/** Specific operations to be performed, such as: [add warehouse ]**/
PublicString operationname ()Default"";
}
After the annotation class is compiled, we need to consider the question of spring and my section. First, we need to create a cut point, that is, the code block to be inserted. The Code is as follows:
/*** Use Spring AOP to add system logs * @ author luoyu */public class logaspect extends baseaction {Private Static final long serialversionuid =-5063868902693772455l; private log logger = logfactory. getlog (logaspect. class); @ suppresswarnings ({"rawtypes", "unchecked"}) Public void dosystemlog (joinpoint point) throws throwable {object [] Param = point. getargs (); Method method = NULL; string methodname = poi NT. getsignature (). getname (); If (! (Methodname. startswith ("set") | methodname. startswith ("get") | methodname. startswith ("query") {class targetclass = point. gettarget (). getclass (); method = targetclass. getmethod (methodname, Param [0]. getclass (); If (method! = NULL) {Boolean hasannotation = method. isannotationpresent (COM. TLJ. pcxt. common. logaop. log. class); If (hasannotation) {COM. TLJ. pcxt. common. logaop. log annotation = method. getannotation (COM. TLJ. pcxt. common. logaop. log. class); string methoddescp = annotation. operationtype () + annotation. operationname (); If (logger. isdebugenabled () {logger. debug ("Action Method:" + method. getname () + "Description:" + Methoddescp);} user appuser = (User) This. gethttpservletrequest (). getsession (). getattribute ("user"); If (appuser! = NULL) {try {COM. TLJ. pcxt. entity. admin. log loginfo = new COM. TLJ. pcxt. entity. admin. log (); loginfo. setip (this. gethttpservletrequest (). getremoteaddr (); loginfo. setsubmituser (appuser); loginfo. setcontent (annotation. operationtype () + "," + appuser. getUserName () + "Run [" + annotation. operationname () + "] operation. The Id set that affects data is [" + GETID (Param [0]) + "]"); this. logservice. save (loginfo);} catch (exception ex) {logger. error (ex. getmessage ());}}}}}}
/*** Use Java reflection to retrieve the ID, name, and other attributes that we need to record from the input parameter object, * Here I retrieve the ID * @ author Luo Yu * @ date 2013-4-11 * @ Param OBJ * @ return string */Public String GETID (Object OBJ) {If (OBJ instanceof string) {return obj. tostring ();} propertydescriptor Pd = NULL; Method method = NULL; string v = ""; try {Pd = new propertydescriptor ("ID", obj. getclass (); method = PD. getreadmethod (); V = string. valueof (method. invoke (OBJ);} catch (exception e) {e. printstacktrace () ;}return v ;}}
After writing the cut-in code, you need to configure the cut-in rule in applicatiocontext. xml. That is to say, when executing the method, you need to cut in the code written above: the configuration is as follows:
<AOP: aspectj-autoproxy/> <bean id = "logaspect" class = "com. TLJ. pcxt. common. logaop. logaspect "/> <AOP: config> <AOP: aspect ref =" logaspect "> <AOP: pointcut id =" logpointcut "expression =" (Execution (* COM. TLJ. pcxt. service. *. * impl. add *(..))) or (Execution (* COM. TLJ. pcxt. service. *. * impl. update *(..))) or (Execution (* COM. TLJ. pcxt. service. *. * impl. delete *(..))) "/> <AOP: After pointcut-ref =" logpointcut "method =" dosystemlog "/> </AOP: aspect> </AOP: config>
Insert a code block after the method is executed during the configuration.
XML Code
- <AOP: After pointcut-ref = "logpointcut" method = "dosystemlog"/>
All methods starting with "add, update, and delete" are executed, and other methods will not be matched.
The call method is as follows,
@ Log (operationtype = "add operation:", operationname = "add warehouse room") Public void addwarehouseroom (warehouseroom) throws serviceexception {try {This. getwarehouseroomdao (). save (warehouseroom);} catch (exception e) {Throw new serviceexception (E );}}
Is to add the preceding custom @ log annotation before the method header, and input the relevant log information
In addition, in the dosystemlog method of logaspect
- Object [] Param = point. getargs ();
Is to retrieve the parameters passed in by the matching method. The relevant parameters required for logging are obtained from this object, the code below this method will check whether the matched method has annotation @ log. If not, it will jump out of this method without any processing.