logging and httphandler
Peter Otten
__peter__ at web.de
Sat Jan 14 03:49:54 EST 2012
Jason Friedman wrote:
> I am logging to my Apache web server, using this Apache format:
>
> LogFormat "%{%Y-%m-%d %H:%M:%S}t %U %q" scriptlog
> CustomLog /var/log/apache2/script.log scriptlog
>
> My code is as follows:
>
> #!/usr/bin/env python3
> import logging, logging.handlers, sys
> logger = logging.getLogger('simple_example')
> logger.setLevel(logging.DEBUG)
> host = "localhost:9090"
> url = "make_deployment_group"
> http_handler = logging.handlers.HTTPHandler(host, url, method='GET')
> http_formatter = logging.Formatter('%(name)s - %(levelname)8s -
> %(message)s') http_handler.setFormatter(http_formatter)
> logger.addHandler(http_handler)
> logger.warn('warn message')
>
> which results in a entry at /var/log/apache2/script.log:
>
> 2012-01-14 05:22:52
>
make_deployment_group?threadName=MainThread&name=simple_example&thread=139923654465280&created=1326518572.83&process=31122&processName=MainProcess&args=%28%29&module=my-
logging&filename=my-logging.py&levelno=30&exc_text=None&pathname=my-
logging.py&lineno=33&asctime=2012-01-14+05%3A22%3A52%2C825&msg=warn+message&exc_info=None&message=warn+message&funcName=%3Cmodule%3E&relativeCreated=20.0970172882&levelname=WARNING&msecs=825.411081314
>
> All the information one could want, which is nice, but is there a way
> to specify I want only certain information sent via the HTTP request?
>>> help(logging.handlers.HTTPHandler.mapLogRecord)
Help on function mapLogRecord in module logging.handlers:
mapLogRecord(self, record)
Default implementation of mapping the log record into a dict
that is sent as the CGI data. Overwrite in your class.
Contributed by Franz Glasner.
Applied to your example:
import logging, logging.handlers, sys
class HTTPHandler(logging.handlers.HTTPHandler):
wanted = ["levelname", "msg", "yadda"]
def mapLogRecord(self, record):
return {name: getattr(record, name, "#missing") for name in self.wanted}
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
host = "localhost:9090"
url = "make_deployment_group"
http_handler = HTTPHandler(host, url, method='GET')
http_formatter = logging.Formatter('%(name)s - %(levelname)8s - %(message)s')
http_handler.setFormatter(http_formatter)
logger.addHandler(http_handler)
logger.warn('warn message')
More information about the Python-list
mailing list