Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'sys': 0.05; 'dict': 0.09; 'overwrite': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:l 60': 0.09; 'server,': 0.12; 'def': 0.13; 'logging,': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:logging': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'apache': 0.21; 'from:addr:web.de': 0.23; 'specify': 0.24; 'code': 0.25; 'module': 0.26; 'function': 0.27; 'skip:[ 10': 0.27; 'import': 0.27; 'mapping': 0.28; 'class': 0.29; 'host': 0.30; 'format:': 0.30; 'python3': 0.30; 'skip:% 10': 0.30; 'contributed': 0.32; 'skip:l 30': 0.32; 'to:addr:python-list': 0.33; 'there': 0.33; 'follows:': 0.34; 'jason': 0.34; 'skip:# 10': 0.34; 'certain': 0.34; 'skip:h 40': 0.34; 'header:X-Complaints- To:1': 0.34; 'record': 0.35; 'skip:" 20': 0.35; 'data.': 0.36; 'class.': 0.36; 'http': 0.36; 'skip:" 10': 0.36; 'entry': 0.37; 'but': 0.37; 'received:org': 0.37; 'using': 0.37; 'could': 0.37; 'help': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'results': 0.64; 'want,': 0.71; 'nice,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: logging and httphandler Date: Sat, 14 Jan 2012 09:49:54 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849099.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326531011 news.xs4all.nl 6981 [2001:888:2000:d::a6]:45000 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18966 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')