tomcat access log

tomcat access log 的配置对应 server.xml 的 Valve节点属性值className为 org.apache.catalina.valves.AccessLogValve的配置。

几个属性中,className为对应处理类,directory为目录,pattern为日志输出模式,prefix为日志文件前缀,suffix为日志文件后缀。pattern是复杂的,其他很好理解。

配置如: 

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log." suffix=".txt"/>

其中pattern的配置值(如:%h %l %u %t &quot;%r&quot; %s %b)的每个格式详细为关键,熟悉后可以根据实际需要灵活配置,了解其中含义也是理解access日志的前提。

以下,是pattern属性值各模式配置详细描述(实际上注释在其直接抽象父类org.apache.catalina.valves.AbstractAccessLogValve):

Abstract implementation of the Valve interface that generates a web server access log with the detailed line contents matching a configurable pattern. The syntax of the available patterns is similar to that supported by the Apache HTTP Server mod_log_config module.

Patterns for the logged message may include constant text or any of the following replacement strings, for which the corresponding information from the specified Response is substituted:

  • %a - Remote IP address
  • %A - Local IP address
  • %b - Bytes sent, excluding HTTP headers, or '-' if no bytes were sent
  • %B - Bytes sent, excluding HTTP headers
  • %h - Remote host name (or IP address if enableLookups for the connector is false)
  • %H - Request protocol
  • %l - Remote logical username from identd (always returns '-')
  • %m - Request method
  • %p - Local port
  • %q - Query string (prepended with a '?' if it exists, otherwise an empty string
  • %r - First line of the request
  • %s - HTTP status code of the response
  • %S - User session ID
  • %t - Date and time, in Common Log Format format
  • %u - Remote user that was authenticated
  • %U - Requested URL path
  • %v - Local server name
  • %D - Time taken to process the request, in millis
  • %T - Time taken to process the request, in seconds
  • %F - Time taken to commit the response, in millis
  • %I - current Request thread name (can compare later with stacktraces)
  • %X - Connection status when response is completed:
    • X = Connection aborted before the response completed.
    • + = Connection may be kept alive after the response is sent.
    • - = Connection will be closed after the response is sent.

In addition, the caller can specify one of the following aliases for commonly utilized patterns:

  • common - %h %l %u %t "%r" %s %b
  • combined - %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"

There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest.
It is modeled after the Apache HTTP Server log configuration syntax:

  • %{xxx}i for incoming headers
  • %{xxx}o for outgoing response headers
  • %{xxx}c for a specific cookie
  • %{xxx}r xxx is an attribute in the ServletRequest
  • %{xxx}s xxx is an attribute in the HttpSession
  • %{xxx}t xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)

Conditional logging is also supported. This can be done with the conditionUnless and conditionIf properties. If the value returned from ServletRequest.getAttribute(conditionUnless) yields a non-null value, the logging will be skipped. If the value returned from ServletRequest.getAttribute(conditionIf) yields the null value, the logging will be skipped. The condition attribute is synonym for conditionUnless and is provided for backwards compatibility.

For extended attributes coming from a getAttribute() call, it is you responsibility to ensure there are no newline or control characters.

Tomcat的`catalina.log.bin`文件通常是日志备份文件,它包含了Apache Tomcat服务器运行过程中的详细信息,包括请求处理、错误信息等。这个文件通常是在服务器重启或定期清理标准日志文件(`catalina.out`或`localhost_access_log.*`)后生成的。 分析`catalina.log.bin`的方法如下: 1. **恢复原始日志**:首先,你需要使用Tomcat的工具`bin/tomcat.util.RTUtilFile.unzipLog()`将`.bin`文件解压回`.log`格式的日志文件。例如,在命令行里输入: ``` java -jar $CATALINA_HOME/bin/bootstrap.jar org.apache.catalina.startup.RTUtilFile.unzipLog $CATALINA_BASE/logs/catalina.log.bin catalina.log ``` 其中 `$CATALINA_HOME` 和 `$CATALINA_BASE` 需要替换为你实际的Tomcat安装路径。 2. **打开并查看**:现在你可以使用文本编辑器(如Notepad++或TextMate)打开`catalina.log`文件,寻找你需要的信息,比如错误堆栈跟踪、访问记录、应用部署和运行状态等。 3. **搜索关键字**:如果你知道某个特定的事件或错误代码,可以直接在搜索框内输入关键词快速定位相关部分。 4. **错误排查**:对于错误信息,通常会包含异常堆栈跟踪,这对于定位问题源很有帮助。通过追踪堆栈,可以了解错误发生在哪一段代码中。 5. **日志配置检查**:如果发现频繁的日志记录,可能是日志级别设置过高,需要检查`server.xml`或`logging.properties`文件中的日志相关配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值