1. Introduction
1.绪论
Spring Boot comes with an embedded Tomcat server, which is super-handy. However, we can’t see Tomcat’s logs by default.
Spring Boot带有一个嵌入式的Tomcat服务器,这是很方便的。然而,在默认情况下,我们无法看到Tomcat的日志。
In this tutorial, we’ll learn how to configure Spring Boot to show Tomcat’s internal and access logs via a toy application.
在本教程中,我们将学习如何配置Spring Boot,以通过一个玩具应用程序显示Tomcat的内部和访问日志。
2. Sample Application
2.申请书样本
First of all, let’s create a REST API. We’ll define a GreetingsController to greet the user:
首先,让我们创建一个REST API。我们将定义一个GreetingsController来迎接用户。
@GetMapping("/greetings/{username}")
public String getGreetings(@PathVariable("username") String userName) {
return "Hello " + userName + ", Good day...!!!";
}
3. Tomcat Log Types
3.Tomcat日志类型
Embedded Tomcat stores two types of logs:
嵌入式Tomcat存储两种类型的日志。
- Access logs
- Internal server logs
The access logs keep the records of all the requests processed by the application. These logs can be used to track things like page hit counts and user session activity. In contrast, internal server logs will help us to troubleshoot any issues in our running application.
访问日志保留了应用程序处理的所有请求的记录。这些日志可用于追踪诸如页面点击率和用户会话活动。相比之下,内部服务器日志将帮助我们对运行中的应用程序的任何问题进行排除。
4. Access Logs
4.访问日志
By default, the access logs aren’t enabled.
默认情况下,访问日志是不启用的。
We can easily enable them, though, by adding a property to application.properties:
不过,我们可以通过在application.properties中添加一个属性来轻松启用它们。
server.tomcat.accesslog.enabled=true
Similarly, we can use VM arguments to enable the access logs:
同样地,我们可以使用虚拟机参数来启用访问日志。
java -jar -Dserver.tomcat.basedir=tomcat -Dserver.tomcat.accesslog.enabled=true app.jar
These log files will be created in a temporary directory. For example, on Windows, the directory for access logs will look something like AppData\Local\Temp\tomcat.2142886552084850151.40123\logs
这些日志文件将在一个临时目录中创建。例如,在Windows上,访问日志的目录将看起来像AppData\Local\Temp\tomcat.2142886552084850151.40123\logs。
4.1. Format
4.1 格式
So, with this property enabled, we’d see something like the following in our running application:
因此,启用这个属性后,我们会在运行中的应用程序中看到如下内容。
0:0:0:0:0:0:0:1 - - [13/May/2019:23:14:51 +0530] "GET /greetings/Harry HTTP/1.1" 200 27
0:0:0:0:0:0:0:1 - - [13/May/2019:23:17:23 +0530] "GET /greetings/Harry HTTP/1.1" 200 27
These are the access logs, and they have the format:
这些是访问日志,它们的格式是:。
%h %l %u %t \"%r\" %>s %b
Which we can interpret as:
我们可以解释为。
%h – the client IP which has sent the request, 0:0:0:0:0:0:0:1 in this case
%l – the identity of the user
%u – the user name determined by HTTP authentication
%t – the time the request was received
%r – the request line from the client, GET /greetings/Harry HTTP/1.1 in this case
%>s – the status code sent from the server to the client, like 200 here
%b – the size of the response to the client, or 27 for these requests
%h – 发送请求的客户IP,0:0:0:0:0:0:1在这种情况下
。
%l – 用户的身份
。
%u – 由 HTTP 认证决定的用户名
%t – 收到请求的时间
%r – 来自客户端的请求行,GET /greetings/Harry HTTP/1.1在本例中
。
%>s – 从服务器发送到客户端的状态代码,比如这里的200
。
%b – 响应客户端的大小,或27这些请求的大小
Since this request didn’t have an authenticated user, %l and %u printed dashes.
由于这个请求没有认证的用户,%l和%u打印成破折号。
In fact, if any information is missing, Tomcat will print a dash for that slot.
事实上,如果缺少任何信息,Tomcat将为该槽打印一个破折号。
4.2. Customizing Access Logs
4.2.自定义访问日志
We can override the default Spring Boot configuration by adding few properties in application.properties.
我们可以通过在application.properties中添加一些属性来覆盖默认的Spring Boot配置。
Firstly, to change the default log file name:
首先,要改变默认的日志文件名。
server.tomcat.accesslog.suffix=.log
server.tomcat.accesslog.prefix=access_log
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
Also, we can change the location of the log files:
此外,我们还可以改变日志文件的位置。
server.tomcat.basedir=tomcat
server.tomcat.accesslog.directory=logs
Finally, we can override the way logs are written in the log file:
最后,我们可以覆盖日志文件中写入日志的方式。
server.tomcat.accesslog.pattern=common
There are a few more configurable properties in Spring Boot, too.
在Spring Boot中还有一些可配置的属性。
5. Internal Logs
5.内部日志
Tomcat server’s internal logs are very helpful to solve any server-side issues.
Tomcat服务器的内部日志对解决任何服务器端的问题非常有帮助。
To view these logs, we have to add below logging configuration in application.properties:
为了查看这些日志,我们必须在application.properties中添加以下日志配置。
logging.level.org.apache.tomcat=DEBUG
logging.level.org.apache.catalina=DEBUG
And then we’ll see something like:
然后我们会看到类似的情况。
2019-05-17 15:41:07.261 DEBUG 31160 --- [0124-Acceptor-0] o.apache.tomcat.util.threads.LimitLatch : Counting up[http-nio-40124-Acceptor-0] latch=1
2019-05-17 15:41:07.262 DEBUG 31160 --- [0124-Acceptor-0] o.apache.tomcat.util.threads.LimitLatch : Counting up[http-nio-40124-Acceptor-0] latch=2
2019-05-17 15:41:07.278 DEBUG 31160 --- [io-40124-exec-1] org.apache.tomcat.util.modeler.Registry : Managed= Tomcat:type=RequestProcessor,worker="http-nio-40124",name=HttpRequest1
...
2019-05-17 15:41:07.279 DEBUG 31160 --- [io-40124-exec-1] m.m.MbeansDescriptorsIntrospectionSource : Introspected attribute virtualHost public java.lang.String org.apache.coyote.RequestInfo.getVirtualHost() null
...
2019-05-17 15:41:07.280 DEBUG 31160 --- [io-40124-exec-1] o.a.tomcat.util.modeler.BaseModelMBean : preRegister org.apache.coyote.RequestInfo@1e6f89ad Tomcat:type=RequestProcessor,worker="http-nio-40124",name=HttpRequest1
2019-05-17 15:41:07.292 DEBUG 31160 --- [io-40124-exec-1] org.apache.tomcat.util.http.Parameters : Set query string encoding to UTF-8
2019-05-17 15:41:07.294 DEBUG 31160 --- [io-40124-exec-1] o.a.t.util.http.Rfc6265CookieProcessor : Cookies: Parsing b[]: jenkins-timestamper-offset=-19800000
2019-05-17 15:41:07.296 DEBUG 31160 --- [io-40124-exec-1] o.a.c.authenticator.AuthenticatorBase : Security checking request GET /greetings/Harry
2019-05-17 15:41:07.296 DEBUG 31160 --- [io-40124-exec-1] org.apache.catalina.realm.RealmBase : No applicable constraints defined
6. Conclusion
6.结语
In this quick article, we’ve learned the difference between Tomcat’s internal and access logs. Then, we saw how to enable and customize them.
在这篇快速文章中,我们已经了解了Tomcat内部日志和访问日志的区别。然后,我们看到如何启用和定制它们。
Make sure to check out the sample over on GitHub.
请务必查看GitHub上的样本,。