天天看点

通过web的方式动态查看tomcat的catalina.out的日志(web.py)

              Tomcat  Log  Viewer

    通过web的方式动态访问Tomcat的catalina.out的日志。

1:安装web.py

下载web.py

# wget http://webpy.org/static/web.py-0.33.tar.gz

安装web.py

# tar zxvf web.py-0.33.tar.gz

# cd web.py-0.33

使所有的web程序可以访问

# python setup.py install

2:编写Python脚本,通过web.py的小web服务程序实现动态访问

# mkdir python

# cd python

# vi logview.py

  import web

  import os

  urls = (

       '/', 'index'

  ) 

  class index:

    def GET(self):

        command = 'tail -n100 /opt/tomcat_iphone/logs/catalina.out'

        textlist = os.popen(command).readlines()#执行linux命令的哦

        result = '<h1>Last 100 lines log</h1>'

        for line in textlist:

                result = '%s\n%s'%(result,line)

        return result#其实直接return textline也是可以哦

  if __name__ == "__main__":

    app = web.application(urls, globals())

    app.run()

###command定义文件,根据实际情况修改查看多少行,或tomcat的日志位置

3:启动web.py服务,指定端口

# nohup python  /root/python/logview.py 8000 &

###端口8000可以随意指定,只要没被占用,默认为8080

4:公网端口映射

122.192.xxx.xxx 8000 -->172.31.2.94 8000

5:通过浏览器访问catalina.out ,F5刷新即可得到最新的100行的日志

<a href="http://blog.51cto.com/attachment/201211/213500624.png" target="_blank"></a>

本文转自 kuchuli 51CTO博客,原文链接:http://blog.51cto.com/lgdvsehome/1071293,如需转载请自行联系原作者