Disclosure of Invention
The invention aims to provide a log recording system and method based on an MVC architecture, which can complete the recording of log information only by adding some annotation logics on the basis of not changing a source program code, and the recorded information is far richer than a tangent plane.
In order to achieve the above object, the present invention provides a log recording system based on an MVC architecture, where the MVC architecture includes a control layer and a service layer, the control layer receives a user request and executes a method in the control layer according to the user request, the method in the control layer is executed by calling the method in the service layer, and an annotation is set before a code corresponding to a method that needs to be intercepted in the control layer and the service layer, and the system includes: the control layer processing module is used for calling the service logic of the annotation to extract the user information when the program is executed to the control layer and the code corresponding to the method of the annotation is set before the program is executed to the control layer in the process that the control layer processes the received user request; and the service layer processing module is used for calling the annotated business logic to record log parameters when a program is executed into the service layer and a code corresponding to a method which is provided with an annotation before is set, and storing the log parameters and the user information in the log storage module.
The service logic of the annotation set before the code corresponding to the method in the control layer can be used for indicating the user to request the field for storing the user information and storing the user information in the field.
The annotated business logic set before the code corresponding to the method in the service layer can be used for recording the parameter change caused by the execution of the method.
The annotated business logic set before the code corresponding to the method in the service layer can be used for recording the parameters operated by the execution of the method.
The business logic for recording the annotation of the parameter operated by the execution of the method can also contain an annotation, and the annotated business logic can be used for indicating the field required to be recorded and the relationship between the fields.
Wherein, the relationship between the fields may include which field is the primary key field and/or which fields are parent-child relationships.
Correspondingly, the present invention also provides a log recording method based on an MVC architecture, where the MVC architecture includes a control layer and a service layer, the control layer receives a user request and executes a method in the control layer according to the user request, the method in the control layer is executed by calling the method in the service layer, and an annotation is set before a code corresponding to a method to be intercepted in the control layer and the service layer, and the method includes: in the process that the control layer processes the received user request, when a program is executed to a code corresponding to a method in which an annotation is arranged in the control layer, calling the service logic of the annotation to extract user information; and when the program is executed to the code corresponding to the method in which the annotation is set before in the service layer, calling the annotated business logic to record the log parameters, and storing the log parameters and the user information in a log storage module.
The service logic of the annotation set before the code corresponding to the method in the control layer can be used for indicating the user to request the field for storing the user information and storing the user information in the field.
The annotated business logic set before the code corresponding to the method in the service layer can be used for recording the parameter change caused by the execution of the method.
The annotated business logic set before the code corresponding to the method in the service layer can be used for recording the parameters operated by the execution of the method.
The business logic for recording the annotation of the parameter operated by the execution of the method can also contain an annotation, and the annotated business logic can be used for indicating the field required to be recorded and the relationship between the fields.
Wherein, the relationship between the fields may include which field is the primary key field and/or which fields are parent-child relationships.
The invention intercepts the annotation through the section before the annotation mark is in the related program code (namely, before the method needing intercepting in the control layer and the service layer), calls the service logic of the annotation to record the log parameter which is to be recorded, and stores the log parameter and the user information in the log storage module together, thus completing the recording of the operation log information (who do while in while time at where). The invention can complete the recording of the log information only by adding some annotations on the basis of not changing the source program code, and the recorded information is far richer than the section. In addition, the data structures of different systems needing to record logs are different, and the system can be adapted to different systems needing to record logs only by correspondingly adjusting the annotated business logic, so that the expandability of the invention is greatly improved, and the invention is applicable to various systems needing to record logs.
Additional features and advantages of the invention will be set forth in the detailed description which follows.
Detailed Description
The following detailed description of embodiments of the invention refers to the accompanying drawings. It should be understood that the detailed description and specific examples, while indicating the present invention, are given by way of illustration and explanation only, not limitation.
Before describing the invention herein, the MVC architecture is described. MVC is a software design paradigm, organizes code using a method that separates business logic, data, and interface display, aggregates business logic into one component, and does not require rewriting of business logic while improving and personalizing the customized interface and user interaction. MVC was uniquely developed to map traditional input, processing, and output functions into a logical graphical user interface structure.
The MVC architecture generally includes: a View layer, a Controller layer (i.e., "control layer" mentioned in this application), a Service layer (i.e., "Service layer" mentioned in this application), and a DAO layer, and the logging system of the present invention mainly relates to a Controller layer and a Service layer. Wherein,
the View layer is mainly responsible for providing a user View interface, and a user only needs to operate the View interface and does not need to care how a background processes.
The Controller layer is closely combined with the View layer and is responsible for controlling the specific Service module flow, and an interface of the Service layer is called in the Controller layer to control the Service flow.
The Service layer is mainly responsible for the logical application design of the Service module, provides an interface to the controller layer of the upper layer, and uses the interface provided by the Dao layer. The Service logic of the Service layer is beneficial to the independence and the reutilization of the general Service logic, and the program is very concise.
The Dao layer is primarily responsible for communicating with the database. He is only responsible for accessing data, regardless of the other business logic, and what is done within the Dao layer is the data access logic and access to the data. Data access, most often operations on data. The Dao layer provides an interface for the Service layer of the upper layer. After the operation is completed, if the query is made, the Dao layer returns the object, and if the query is an addition or deletion, only one Boolean value is required to be returned to indicate success or failure.
In order to achieve the above object, the present invention provides a logging system 200 based on an MVC architecture 100, where the MVC architecture 100 includes a Controller layer 110 and a Service layer 120, the Controller layer 110 receives a user request and executes a method in the Controller layer 110 according to the user request, the method in the Controller layer 110 is executed by calling a method in the Service layer 120 (that is, a business operation is implemented by calling a method in the Service layer 120), where a comment is set before a code corresponding to a method to be intercepted in the Controller layer 110 and the Service layer 120, and the system includes: a control layer processing module 210, configured to, in a process that the Controller layer 110 processes the received user request, call a service logic of an annotation when a program is executed to a code corresponding to a method in the Controller layer 110, where the code is set with the annotation before, so as to extract user information; and a Service layer processing module 220, configured to call a Service logic of the annotation when the program is executed to a code corresponding to a method in the Service layer 120, where the method is previously set with the annotation, so as to record a log parameter, and store the log parameter and the user information in the log storage module. The invention defines the section based on the annotation, integrates the user information and the related service information (namely, the log parameters) obtained by analyzing the service logic marked by the annotation to form an operation log, and records and saves the operation log for analysis and statistics.
Specifically, in order to record the user information, the user information is obtained and stored in the thread variable ThreadLocal by using a related method in the cut plane intercept Controller layer. In the method of the Service layer needing to be logged, the operation of the method in the Service layer can be recorded through the section. Services on the Service layer are divided into two types: one is the AroundAdvice, i.e. to record the change before and after the operation. For methods that do not have the convenience of obtaining a pre-operational state, parameters of method operation may be recorded by the AfterAdvice. After the log details are obtained, the log information can be stored in a database through the JDBCAppler of the extended Lodback. After the log record is completed, the record in the thread variable ThreadLocal can be cleared through the After address of the Controller. The invention marks the method of the log to be recorded with the annotation in the Control layer, and extracts the user information and stores the user information in the thread variable by combining the business logic related to the annotation; marking a business method needing log recording by using an annotation in a Service layer, combining an annotation parameter and business logic related to the annotation to obtain business change caused by the execution of the method, and then integrating user information and the business change to form an operation log for recording and saving.
The annotations and their business logic in the Controller layer and the Service layer are described in detail below.
1) Note @ RequestLoggable within Controller layer
The method in the Controller layer that can be intercepted by the annotation @ requestlogable, that is, the annotation @ requestlogable can be placed before the method that needs to be intercepted, as follows:
@RequestLoggable
@RequestMapping(“publishSubscribeltem”)
@ResponseBody
public ResultMsgBean PublishSubscribeltem(@RquestParam int albumld)
when the program is executed to the code corresponding to the method, the service logic of the annotation @ RequestLoggable is called and executed to obtain the user information and store the user information in ThreadLocal. The service logic of the annotation @ requestlogable may be used to indicate a field in which user information is stored in the user request and store the user information in the field. The business logic code of the note @ requestlogable may be as follows, with fields within which user information is stored in the user request being indicated.
2) Annotation within Service layer
2.1 notes @ AroundLoggable
The annotation @ arondlogable is used for annotating a method of a Service layer requiring Around Service interception, that is, the annotation @ arondlogable can be placed before the method requiring interception, as follows:
@AroundLoggable(type=OperationTypeEnum.INSERT,targetClass=ZtdiySubject.class)
@Override
public void addZtdiySubject(ZtdiySubject ztdiySubject,int copyFromld,Boolean sync)throws SQLException
when the program is executed to the code corresponding to the method, the business logic of the note @ AroundLoggable is called and executed. The business logic of the note @ aroundlogable may be used to record parameter changes resulting from the execution of the method. The business logic code of the note @ aroundlogable may be as follows, which requires two parameters: 1) log object class, i.e., targetClass; and 2) the type of operation, i.e., the enumerated value in OpereteTypeEnum.
2.2 notes @ AfterLoggable
The annotation @ AfterLoggable is used for annotating a method for inconveniently acquiring a Service layer of a pre-operation state, that is, the annotation @ AroundLoggable can be placed before a method needing to be intercepted, as follows:
@AfterLoggable(type=OperationTypeEnum.DELETE,targetClass=SubscribeRuleContent.class)
@Override
public void deleteSubscribeByAlbumld(@ModifyParam(isPK=ture,paramName=”albumld”)int albumld)
when the program is executed to the code corresponding to the method, the business logic of the note @ AfterLoggable is called and executed. The business logic of the note @ AfterLoggable may be used to record parameters upon which execution of the method operates. The business logic code of the annotation @ AfterLoggable may be as follows, which must provide log information in conjunction with the use of the annotation @ ModifyParam.
2.3 notes @ ModifyParam
The annotation @ ModifyParam is used to provide log information for the annotation @ afterloggap, and is used in conjunction with the annotation @ afterloggap, and the specific use manner is as follows:
the business logic of the comment @ ModifyParam may be used to indicate the fields of the desired record and the relationships between the fields (e.g., which field is the primary key field and/or which fields have parent-child relationships between them), and the business logic code may be as follows:
where the paramName parameter is the name of the parameter, isPk indicates whether this parameter is the primary key, pkField is used to indicate the field name of the primary key when the parameter is object.
In order to obtain log information from a log object, some annotations for the fields are also needed, as follows:
1) the note @ UnManitorField for indicating this field does not require logging
2) Note @ PKField for marking this field as a primary key field
3) Note @ PIDField for marking parent object with parent-child relationship, this field is PID
4) The annotation @ SubIDField is used for marking child objects with parent-child relationships, and the field is SubID, and the use mode of the annotation for the field is as follows:
@Entity
@Table(name=”CMS_SUBSCRIBE_CONTENT”)
public class SubscribeRuleContent implements Serializable{
private static final long serialVersionUID=‐2929087382057975094L;
@PKField
@SubIDField
private int id;
private int category;
@PIDField
private int albumld;
private String albumName;
private float currentEpisode;
private int playStatus; // update status
private Inter updateType; //1 day 2 weekly 3 every week 4 days
private float cycleEpisodes;
private String beginTime;
private String endTime;
private Timestamp ctime;
private Timestamp mtime;
private String indicateDate;
private String updateDay;
private String playtime;
private int indicate_playtime;
@UnMonitorField
private String releDesc;
@UnMonitorField
private String scope;
@UnMonitorField
private int no; // number
In summary, the use of the annotation @ aroundlogable requires the following settings:
1) annotating the corresponding method in the Controller layer by using @ RequestLoggable;
2) annotating the method in the corresponding Service layer with @ AroundLoggable; and
3) fields of related objects are annotated with @ UnManitrofield, @ PKfield, @ PIDField, @ SubIDfield.
The use of the note @ AfterLoggable requires the following settings:
1) annotating the corresponding method in the Controller layer by using @ RequestLoggable;
2) annotating the method in the corresponding Service layer with @ AfterLoggable; and
3) annotating parameters of the method within the corresponding Service layer with @ ModifyLoggable.
Fig. 2 is a flowchart of a logging method based on an MVC architecture provided in the present invention. As shown in fig. 2, correspondingly, the present invention further provides a log recording method based on an MVC architecture, where the MVC architecture includes a Controller layer and a Service layer, the Controller layer receives a user request and executes a method in the Controller layer according to the user request, the method in the Controller layer is executed by calling a method in the Service layer, where an annotation is set before a code corresponding to a method that needs to be intercepted in the Controller layer and the Service layer, and the method includes: in the process of processing the received user request by the Controller layer, when a program is executed to a code corresponding to a method in which an annotation is set in the Controller layer, calling the service logic of the annotation to extract user information; and when the program is executed to the code corresponding to the method in the Service layer, which is provided with the annotation before, calling the Service logic of the annotation to record the log parameters, and storing the log parameters and the user information in the log storage module. The details and benefits associated with this method are the same as for the logging system described above and will not be described further herein.
According to the invention, before the annotation identifier is in the related program code (namely, before the method needing to be intercepted in the control layer and the service layer), the annotation is intercepted through the section, the service logic calling the annotation records the log parameters which are required to be recorded, and the log parameters and the user information are stored in the log storage module together. The invention can complete the recording of the log information only by adding some annotations on the basis of not changing the source program code, and the recorded information is far richer than the section. In addition, the data structures of different systems needing to record logs are different, and the system can be adapted to different systems needing to record logs only by correspondingly adjusting the annotated business logic, so that the expandability of the invention is greatly improved, and the invention is applicable to various different systems needing to record logs.
The preferred embodiments of the present invention have been described in detail with reference to the accompanying drawings, however, the present invention is not limited to the specific details of the above embodiments, and various simple modifications can be made to the technical solution of the present invention within the technical idea of the present invention, and these simple modifications are within the protective scope of the present invention.
It should be noted that the various features described in the above embodiments may be combined in any suitable manner without departing from the scope of the invention. The invention is not described in detail in order to avoid unnecessary repetition.
In addition, any combination of the various embodiments of the present invention is also possible, and the same should be considered as the disclosure of the present invention as long as it does not depart from the spirit of the present invention.