Windows 环境 tomcat日志配置

tomcat每次启动时,自动在logs目录下生产以下日志文件,按照日期自动备份

 

  localhost.2016-07-05.txt   //经常用到的文件之一 ,程序异常没有被捕获的时候抛出的地方

  catalina.2016-07-05.txt  //经常用到的文件之一,程序的输出,tomcat的日志输出等等

  manager.2016-07-05.txt //估计是manager项目专有的

  host-manager.2016-07-05.txt//估计是manager项目专有的

  localhost_access_log.2016-10-01.txt //tomcat访问日志记录,需要配置

1.让所有文件都输出到同一个文件中

 打开Tomcat目录conf\logging.properties,修改如下,所有日志输出到tomcat开头的文件中

1catalina.org.apache.juli.FileHandler.level = FINE

1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

# 1catalina.org.apache.juli.FileHandler.prefix = catalina.

1catalina.org.apache.juli.FileHandler.prefix = tomcat.

 

2localhost.org.apache.juli.FileHandler.level = FINE

2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

# 2localhost.org.apache.juli.FileHandler.prefix = localhost.

2localhost.org.apache.juli.FileHandler.prefix = tomcat.

 

3manager.org.apache.juli.FileHandler.level = FINE

3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

# 3manager.org.apache.juli.FileHandler.prefix = manager.

3manager.org.apache.juli.FileHandler.prefix = tomcat.

 

4host-manager.org.apache.juli.FileHandler.level = FINE

4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

# 4host-manager.org.apache.juli.FileHandler.prefix = host-manager.

4host-manager.org.apache.juli.FileHandler.prefix = tomcat.

2.打开访问日志

默认 tomcat 不记录访问日志,如下方法可以使 tomcat 记录访问日志

编辑 ${catalina}/conf/server.xml 文件. 注 :${catalina} 是 tomcat 的安装目录

把以下的注释 (<!-- -->) 去掉即可。

 <!--

        <Valve className="org.apache.catalina.valves.AccessLogValve"

         directory="logs"  prefix="localhost_access_log." suffix=".txt"

         pattern="common" resolveHosts="false"/>

  -->

上面的pattern可以修改格式

该项值可以为: common 与 combined ,这两个预先设置好的格式对应的日志输出内容如下:

common 的值: %h %l %u %t %r %s %b

combined 的值: %h %l %u %t %r %s %b %{Referer}i %{User-Agent}i

pattern 也可以根据需要自由组合, 例如 pattern="%h %l"

对于各fields字段的含义请参照 :

 

http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html 中的 Access Log Valve 项

 

即可输出tomcat的访问日志。其中pattern的参数设置如下

修改为:

%h %l %u %t &quot;%r&quot; %s %b %{Referer}i %{User-Agent}i

%{User-Agent}记录客户端浏览器的相关信息

Values for the pattern attribute are made up of literal text strings, combined with pattern identifiers prefixed by the "%" character to cause replacement by the corresponding variable value from the current request and response. The following pattern codes are supported:

  • %a - Remote IP address
  • %A - Local IP address
  • %b - Bytes sent, excluding HTTP headers, or '-' if zero
  • %B - Bytes sent, excluding HTTP headers
  • %h - Remote host name (or IP address if resolveHosts is false)
  • %H - Request protocol
  • %l - Remote logical username from identd (always returns '-')
  • %m - Request method (GET, POST, etc.)
  • %p - Local port on which this request was received
  • %q - Query string (prepended with a '?' if it exists)
  • %r - First line of the request (method and request URI)
  • %s - HTTP status code of the response
  • %S - User session ID
  • %t - Date and time, in Common Log Format
  • %u - Remote user that was authenticated (if any), else '-'
  • %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
  • %I - Current request thread name (can compare later with stacktraces)

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

The shorthand pattern name common (which is also the default) corresponds to '%h %l %u %t "%r" %s %b'.

 

127.0.0.1 192.168.254.108 - -1 127.0.0.1 HTTP/1.1 - GET 80&<60; GET /rightmainima/leftbott4.swf HTTP/1.1 304 5563A67708646B6AA299C33D59BE132A [22/Sep/2007:10:08:52 +0800] - /rightmainima/leftbott4.swf localhost 0 0.000

%a      这是记录访问者的IP,在日志里是127.0.0.1
%A这是记录本地服务器的IP,在日志里是192.168.254.108
%b这是发送信息的字节数,不涵括http头,如果字节数为0的话,显示为-
%B看tomcat的解释,没看出来与b%的区别,但我这里显示为-1,没想明白,望知道者告知,我把官方解释贴出来吧 Bytes sent, excluding HTTP headers
%h这个就是服务器名称了,如果resolveHosts为false的话,这里就是IP地址了,我的日志里是127.0.0.1
%H访问者使用的协议,这里是HTTP/1.1
%l这个也不太清楚,官方也说这个always return '-' 官方解释:Remote logical username from identd (可能这样翻译:记录浏览者进行身份验证时提供的名字)(always returns '-')
%m访问的方式,是GET还是POST,我这是GET
%p本地接收访问的端口,呵呵,我这里是80啦
%q比如你访问的是aaa.jsp?bbb=ccc,那么这里就显示?bbb=ccc,明白了吧,这个q是querystring的意思
%r官方解释:First line of the request (method and request URI),不是很明白
%s这个是http的状态,我这里返回的是304,咱们经常看见访问某个网页报错误500什么的,那也会返回500
%S用户的session ID,这个session ID大家可以另外查一下详细的解释,反正每次都会生成不同的session ID
%t这就是时间啦,好像有一个Common Log Format可以改,不过我没找到
%u得到了验证的访问者,否则就是"-"
%U访问的URL地址,我这里是/rightmainima/leftbott4.swf
%v服务器名称,可能就是你url里面写的那个吧,我这里是localhost
%D官方解释:Time taken to process the request, in millis,应该是访问发生的时间,以毫秒记
%T官方解释:Time taken to process the request, in seconds,应该是访问发生的时间,以秒记

3.修改tomcat日志级别

Tomcat 日志分为下面5类:

catalina 、 localhost 、 manager 、 admin 、 host-manager

每类日志的级别分为如下 7 种:

SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value)

日志级别的设定方法

修改 conf/logging.properties 中的内容,设定某类日志的级别

示例:

设置 catalina 日志的级别为: FINE

1catalina.org.apache.juli.FileHandler.level = FINE

禁用 catalina 日志的输出:

1catalina.org.apache.juli.FileHandler.level = OFF

输出 catalina 所有的日志消息均输出:

1catalina.org.apache.juli.FileHandler.level = ALL

转载于:https://my.oschina.net/berthome/blog/1487004

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值