/usr/share/pyshared/circuits/web/loggers.py is in python-circuits 2.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # Module: loggers
# Date: 6th November 2008
# Author: James Mills, prologic at shortcircuit dot net dot au
"""Logger Component
This module implements Logger Components.
"""
import os
import sys
import datetime
from io import IOBase
from email._parseaddr import _monthnames
from circuits.six import string_types
from circuits.core import handler, BaseComponent
def formattime():
now = datetime.datetime.now()
month = _monthnames[now.month - 1].capitalize()
return ("[%02d/%s/%04d:%02d:%02d:%02d]" %
(now.day, month, now.year, now.hour, now.minute, now.second))
class Logger(BaseComponent):
channel = "web"
format = "%(h)s %(l)s %(u)s %(t)s " \
"\"%(r)s\" %(s)s %(b)s \"%(f)s\" \"%(a)s\""
def __init__(self, file=None, logger=None, **kwargs):
super(Logger, self).__init__(**kwargs)
if isinstance(file, string_types):
self.file = open(os.path.abspath(os.path.expanduser(file)), "a")
elif isinstance(file, IOBase) or hasattr(file, "write"):
self.file = file
else:
self.file = sys.stdout
self.logger = logger
@handler("response", priority=-0.1)
def response(self, response):
self.log(response)
def log(self, response):
request = response.request
remote = request.remote
outheaders = response.headers
inheaders = request.headers
protocol = "HTTP/%d.%d" % request.protocol
if "X-Forwarded-For" in inheaders:
host = inheaders["X-Forwarded-For"]
else:
host = remote.name or remote.ip
atoms = {"h": host,
"l": "-",
"u": getattr(request, "login", None) or "-",
"t": formattime(),
"r": "%s %s %s" % (request.method, request.path, protocol),
"s": str(response.code),
"b": outheaders.get("Content-Length", "") or "-",
"f": inheaders.get("Referer", ""),
"a": inheaders.get("User-Agent", ""),
}
for k, v in list(atoms.items()):
if isinstance(v, str):
v = v.encode("utf8")
elif not isinstance(v, str):
v = str(v)
# Fortunately, repr(str) escapes unprintable chars, \n, \t, etc
# and backslash for us. All we have to do is strip the quotes.
v = repr(v)[1:-1]
# Escape double-quote.
atoms[k] = v.replace("\"", "\\\"")
if self.logger is not None:
self.logger.info(self.format % atoms)
else:
self.file.write(self.format % atoms)
self.file.write("\n")
self.file.flush()
|