From f9dd406465a5ab9e2d07f04f6ed9b7832418d03f Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Mon, 17 Sep 2012 19:28:29 +0200 Subject: [PATCH] Test the logging facility --- github/Logging.py | 4 ++ github/Requester.py | 8 ++-- github/__init__.py | 1 + github/tests/AllTests.py | 1 + github/tests/Logging.py | 37 +++++++++++++++++++ .../tests/ReplayData/Logging.testLogging.txt | 5 +++ 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 github/Logging.py create mode 100644 github/tests/Logging.py create mode 100644 github/tests/ReplayData/Logging.testLogging.txt diff --git a/github/Logging.py b/github/Logging.py new file mode 100644 index 000000000..043fde0dd --- /dev/null +++ b/github/Logging.py @@ -0,0 +1,4 @@ +import logging + +def get_logger(): + return logging.getLogger('github') diff --git a/github/Requester.py b/github/Requester.py index 942ed5d27..e11044b76 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -26,15 +26,13 @@ import simplejson as json #pragma no cover import GithubException +import Logging + class Requester: __httpConnectionClass = httplib.HTTPConnection __httpsConnectionClass = httplib.HTTPSConnection - @staticmethod - def __logger(): - return logging.getLogger('github') - @classmethod def injectConnectionClasses( cls, httpConnectionClass, httpsConnectionClass ): cls.__httpConnectionClass = httpConnectionClass @@ -114,7 +112,7 @@ def requestRaw( self, verb, url, parameters, input ): if "x-ratelimit-remaining" in headers and "x-ratelimit-limit" in headers: self.rate_limiting = ( int( headers[ "x-ratelimit-remaining" ] ), int( headers[ "x-ratelimit-limit" ] ) ) - logger = self.__logger() + logger = Logging.get_logger() if logger.isEnabledFor(logging.DEBUG): logger.debug(' '.join(map(unicode, [verb, self.__base_url + url, parameters, input, "==>", status, str(headers), str(output)]))) return status, headers, output diff --git a/github/__init__.py b/github/__init__.py index ef0a03abd..91c2954cb 100644 --- a/github/__init__.py +++ b/github/__init__.py @@ -16,3 +16,4 @@ from InputFileContent import InputFileContent from InputGitAuthor import InputGitAuthor from InputGitTreeElement import InputGitTreeElement +from Logging import get_logger diff --git a/github/tests/AllTests.py b/github/tests/AllTests.py index 71453e570..dcdc9cdb7 100644 --- a/github/tests/AllTests.py +++ b/github/tests/AllTests.py @@ -51,6 +51,7 @@ from PaginatedList import * from Exceptions import * from Enterprise import * +from Logging import * from Issue33 import * from Issue50 import * diff --git a/github/tests/Logging.py b/github/tests/Logging.py new file mode 100644 index 000000000..8f1b1ee3b --- /dev/null +++ b/github/tests/Logging.py @@ -0,0 +1,37 @@ +# Copyright 2012 Vincent Jacques +# vincent@vincent-jacques.net + +# This file is part of PyGithub. http://vincent-jacques.net/PyGithub + +# PyGithub is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License +# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see . + +import logging + +import github + +import Framework + +class Logging( Framework.TestCase ): + class MockHandler: + def __init__( self ): + self.level = logging.DEBUG + self.handled = None + + def handle( self, record ): + self.handled = record.getMessage() + + def testLogging( self ): + self.maxDiff = None + logger = github.get_logger() + logger.setLevel( logging.DEBUG ) + handler = self.MockHandler() + logger.addHandler( handler ) + + self.assertEqual( self.g.get_user().name, "Vincent Jacques" ) + self.assertEqual( handler.handled, u'GET https://api.github.com/user None None ==> 200 {\'status\': \'200 OK\', \'content-length\': \'806\', \'x-github-media-type\': \'github.beta; format=json\', \'x-content-type-options\': \'nosniff\', \'vary\': \'Accept, Authorization, Cookie\', \'x-ratelimit-remaining\': \'4993\', \'server\': \'nginx\', \'last-modified\': \'Fri, 14 Sep 2012 18:47:46 GMT\', \'connection\': \'keep-alive\', \'x-ratelimit-limit\': \'5000\', \'etag\': \'"434dfe5d3f50558fe3cea087cb95c401"\', \'cache-control\': \'private, s-maxage=60, max-age=60\', \'date\': \'Mon, 17 Sep 2012 17:12:32 GMT\', \'content-type\': \'application/json; charset=utf-8\'} {"owned_private_repos":3,"disk_usage":18612,"following":28,"type":"User","public_repos":13,"location":"Paris, France","company":"Criteo","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png","plan":{"space":614400,"private_repos":5,"name":"micro","collaborators":1},"blog":"http://vincent-jacques.net","login":"jacquev6","public_gists":3,"html_url":"https://github.com/jacquev6","hireable":false,"created_at":"2010-07-09T06:10:06Z","private_gists":5,"followers":13,"name":"Vincent Jacques","email":"vincent@vincent-jacques.net","bio":"","total_private_repos":3,"collaborators":0,"gravatar_id":"b68de5ae38616c296fa345d2b9df2225","id":327146,"url":"https://api.github.com/users/jacquev6"}' ) diff --git a/github/tests/ReplayData/Logging.testLogging.txt b/github/tests/ReplayData/Logging.testLogging.txt new file mode 100644 index 000000000..df2303199 --- /dev/null +++ b/github/tests/ReplayData/Logging.testLogging.txt @@ -0,0 +1,5 @@ +https GET api.github.com None /user {'Authorization': 'Basic login_and_password_removed'} null +200 +[('status', '200 OK'), ('content-length', '806'), ('x-github-media-type', 'github.beta; format=json'), ('x-content-type-options', 'nosniff'), ('x-ratelimit-limit', '5000'), ('vary', 'Accept, Authorization, Cookie'), ('x-ratelimit-remaining', '4993'), ('server', 'nginx'), ('last-modified', 'Fri, 14 Sep 2012 18:47:46 GMT'), ('connection', 'keep-alive'), ('etag', '"434dfe5d3f50558fe3cea087cb95c401"'), ('cache-control', 'private, s-maxage=60, max-age=60'), ('date', 'Mon, 17 Sep 2012 17:12:32 GMT'), ('content-type', 'application/json; charset=utf-8')] +{"owned_private_repos":3,"disk_usage":18612,"following":28,"type":"User","public_repos":13,"location":"Paris, France","company":"Criteo","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png","plan":{"space":614400,"private_repos":5,"name":"micro","collaborators":1},"blog":"http://vincent-jacques.net","login":"jacquev6","public_gists":3,"html_url":"https://github.com/jacquev6","hireable":false,"created_at":"2010-07-09T06:10:06Z","private_gists":5,"followers":13,"name":"Vincent Jacques","email":"vincent@vincent-jacques.net","bio":"","total_private_repos":3,"collaborators":0,"gravatar_id":"b68de5ae38616c296fa345d2b9df2225","id":327146,"url":"https://api.github.com/users/jacquev6"} +