How to print apache style log?

204 views
Skip to first unread message

罗晟

unread,
Feb 7, 2012, 10:30:38 PM2/7/12
to Tornado Web Server
Hi all,

How to tell tornado server to print apache style log? I can't figure
it out by checking the docs.
So I added one for myself:

...

def almost_apache_style_log(req):
'''without status code and body length'''
return '%s - - [%s +0800] "%s %s %s" - - "%s" "%s"' % \
(req.remote_ip, time.strftime("%d/%b/%Y:%X"), req.method,
req.uri,
req.version, getattr(req, 'referer', '-'),
req.headers['User-Agent'])


class BaseHandler(RequestHandler):
def initialize(self):
print almost_apache_style_log(self.request)

...

Does tornado have any standard way to do this?

regards,

Sheng

Felinx Lee

unread,
Feb 8, 2012, 1:02:27 AM2/8/12
to python-...@googlegroups.com
Try to override _request_summary in RequestHandler.
--
What can change the nature of a man?(Planescape Torment)
----------------------------------------------------------------------------------------
http://feilong.me            Felinx Lee's Blog (Chinese Only)
http://www.zhimaq.com IT Q&A (Chinese Only)
http://poweredsites.org  What powered your sites? PHP, Ruby or Python?

Ben Darnell

unread,
Feb 8, 2012, 1:59:33 AM2/8/12
to python-...@googlegroups.com
It's better to use Application.log_request
(http://www.tornadoweb.org/documentation/web.html#tornado.web.Application.log_request),
since that will be used for e.g. StaticFileHandler in addition to your
own handlers.

-Ben

罗晟

unread,
Feb 8, 2012, 3:18:55 AM2/8/12
to Tornado Web Server
thanks Ben, exactly as you suggested. Below is my new code fyi:

...

def almost_apache_style_log(handler):
'''without status code and body length'''
req = handler.request
print '%s - - [%s +0800] "%s %s %s" - - "%s" "%s"' % \
(req.remote_ip, time.strftime("%d/%b/%Y:%X"), req.method,
req.uri,
req.version, getattr(req, 'referer', '-'),
req.headers['User-Agent'])

class BaseHandler(RequestHandler):
def initialize(self):
# print almost_apache_style_log(self.request)
pass
...

settings = {
....
"log_function": almost_apache_style_log,
}

....

On 2月8日, 下午2时59分, Ben Darnell <b...@bendarnell.com> wrote:
> It's better to use Application.log_request
> (http://www.tornadoweb.org/documentation/web.html#tornado.web.Applicat...),
> since that will be used for e.g. StaticFileHandler in addition to your
> own handlers.
>
> -Ben
>
>
>
>
>
>
>
> On Tue, Feb 7, 2012 at 10:02 PM, Felinx Lee <felinx....@gmail.com> wrote:
> > Try to override _request_summary in RequestHandler.
>
> > On Wed, Feb 8, 2012 at 11:30 AM, 罗晟 <sheng.peisi....@gmail.com> wrote:
>
> >> Hi all,
>
> >> How to tell tornado server to print apache style log?  I can't figure
> >> it out by checking the docs.
> >> So I added one for myself:
>
> >> ...
>
> >> def almost_apache_style_log(req):
> >>    '''without status code and body length'''
> >>    return '%s - - [%s +0800] "%s %s %s" - - "%s" "%s"' % \
> >>            (req.remote_ip, time.strftime("%d/%b/%Y:%X"), req.method,
> >> req.uri,
> >>             req.version, getattr(req, 'referer', '-'),
> >> req.headers['User-Agent'])
>
> >> class BaseHandler(RequestHandler):
> >>    def initialize(self):
> >>        print almost_apache_style_log(self.request)
>
> >> ...
>
> >> Does tornado have any standard way to do this?
>
> >> regards,
>
> >> Sheng
>
> > --
> > What can change the nature of a man?(Planescape Torment)
> > --------------------------------------------------------------------------- -------------
> >http://feilong.me            Felinx Lee's Blog (Chinese Only)
> >http://www.zhimaq.comIT Q&A (Chinese Only)
Reply all
Reply to author
Forward
0 new messages