For any system, a powerful logging function is very important and necessary, according to log records can be in time to grasp the health status of the system and fault location. However, There is another kind of log-access log as a Web container. Access logs typically record client access related information, including client IP, request time, request protocol, request method, request bytes, response code, session ID, processing time, and so on. Access logs can be used to count the number of users visited, the time distribution of access, and so on, and this data can help companies make decisions on their operational strategies.
How would you design an access log component if you were to design it? You should soon think of the core function of the access log is to record the information, as to where to record, in which form to record we do not care, and quickly think of interface-oriented programming to define an interface Accesslog, the method name is named Log Bar, you need to pass parameters containing the request object and the response object, as below,
Public interface Accesslog {
public void log (Request request, Response Response);
}
Defining a good interface is a good start, and the next thing to consider is what types of components are needed, where to go for the previous record, what kind of record we are most familiar with and the first to think about is the document to disk, so let's implement a file log of the access log component.
public class Fileaccesslog implements accesslog{
public void log (Request request, Response Response) {
String message=request and the values in response make up the strings you need.
try {
Charset Charset = Charset.defaultcharset ();
PrintWriter writer = new PrintWriter (new BufferedWriter (New OutputStreamWriter (
New FileOutputStream ("C:/accesslog.log", True), CharSet), 128000),
FALSE);
WRITER.PRINTLN (message);
Writer.flush ();
} catch (IOException e) {
}
}
}
It seems that this is a simple and good implementation of the file log access log component, at least for example to show that people feel simple and clear, using printwriter bufferedwriter Add a buffering effect (using the buffered How can the buffering effect be achieved, If you forget to look at the buffer that I wrote in front of the relevant chapters, read it will certainly have a deep understanding of the buffer, the reason for the buffer size is set to 128000 is a fitting value derived from experience, outputstreamwriter charset fileoutputstream true indicates that the log is appended instead of overwritten.
If you think that using the SQL language to log information to make you more handy, then write to the file does not meet the requirements, we need another implementation, through the jdbc log to the database. So you have to build another jdbcaccesslog class and re-implement the log method, using JDBC to manipulate the database everyone is the most familiar. , limited by the length of the implementation details, but one prerequisite is that you have to make an appointment with the database to create a specific table and the structure of the table should be defined according to the access information.
public class Jdbcaccesslog implements accesslog{
public void log (Request request, Response Response) {
the access information contained in request and response is composed of a SQL through JDBC Statement into the database.
}
}
You can also customize a variety of access log components to meet your needs, just implement the accesslog interface. But sometimes you might be using multiple access log components, such as writing files and persisting them into a database, so let's give him a set of adapters.
public class Accesslogadapter implements Accesslog {
Private accesslog[] logs;
Public Accesslogadapter (Accesslog log) {
logs = new accesslog[] {log};
}
public void Add (Accesslog log) {
Accesslog newarray[] = arrays.copyof (logs, logs.length + 1);
Newarray[newarray.length-1] = log;
logs = NewArray;
}
public void log (Request request, Response Response) {
for (Accesslog log:logs) {
Log.log (request, response);
}
}
}
After adapter adaptation, thelog method has become a log method that iterates through multiple access log components , and the interface provided to the external adapter is still a log method , a call to write the following test class log will record the hello Tomcat separately to the file and database .
public class test{
public static void Main (string[] args) {
Accesslog Accesslog = new Accesslogadapter (new Fileaccesslog ());
Accesslog.add (New Jdbcaccesslog ());
AccessLog.log (New Request ("Hello Tomcat"), New Response ());
}
}
After the design of a good access log component has been formed, and this is the tomcat Access log component design ideas, and Tomcat for Modular and configurable extensions, It takes the access log component as a valve in a pipe (a friend who forgets the plumbing mechanism see the previous Piping mechanism section) so that the access logging function can be configured in any container by using Tomcat 's server configuration file configuration.
Designing an Access Log component for a middleware