Customization of the Access log format

Source: Internet
Author: User

After a few steps in the first section an Access log component has been formed, but in order to increase user customization we still have to do something, the most classic approach to user-defined implementations is to introduce variable representations, such as defining %a to represent remote host IP ,%A represents the native IP , and so on, and then writes the variable to the corresponding value by the corresponding logic before writing to the log. This section enables us to implement custom support for the log format.

The whole process is to customize the variable group first, then replace the variable with the corresponding value, and then write the replacement value to the file. Since many different variables need to be implemented, an interface is defined to constrain the definition of all variable addition operations, and a addelement method is defined, by the request and Response Gets the corresponding variable value after it is added to the string buf .

Public interface Accesslogelement {

public void AddElement (StringBuilder buf, request request, Response Response);

}

Then define two elements to add the response status code and the remote address, use the direct call their addelement to add the status code and the remote address to the string,

public class Statuscodelelement implements Accesslogelement {

public void AddElement (StringBuilder buf, request request, Response Response) {

Buf.append (Response.getstatus ());

}

}

public class Remoteaddrelement implements Accesslogelement {

public void AddElement (StringBuilder buf, request request, Response Response) {

Buf.append (Request.getremoteaddr ());

}

}

Now a mapper is used to parse variables into their respective accesslogelement mappings, as follows elementmapping provides a map The method parses the custom pattern into the corresponding access log element and replaces the original variable with the corresponding value.

public class Elementmapping {

Response Response;

Request Reqeust;

Public elementmapping (Request request, Response Response) {

This.reqeust=request;

This.response=response;

}

Public StringBuilder Map (String pattern) {

StringBuilder buf = new StringBuilder ();

for (int i = 0; i < pattern.length (); i++) {

char ch = pattern.charat (i);

if (ch = = '% ') {

ch = pattern.charat (++i);

AddElement (CH, buf);

} else {

Buf.append (CH);

}

}

return buf;

}

private void AddElement (char ch, StringBuilder buf) {

Switch (CH) {

Case ' a ':

New Remoteaddrelement (). AddElement (buf, request, response);

Break

Case ' s ':

New Statuscodeelement (). AddElement (buf, request, response);

Break

}

}

}

This section describes how to introduce variables to your access log component to have a custom format feature, and use a simple case description, if you want to have more powerful customization ability can be implemented on the basis of this article, for example, can be used to simplify the combination of variables to a string representation,common strings are used to represent %h%l%u%t "%r"%s%b commonly used variable combinations, of course to implement such support you have to do the corresponding processing in the mapper.

Customization of the Access log format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.