Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Rich Logger (Resolves #81) #84

Merged
merged 4 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions starcli/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime, timedelta
from time import sleep
import logging
from rich.logging import RichHandler
ineelshah marked this conversation as resolved.
Show resolved Hide resolved
from random import randint
import re

Expand All @@ -12,6 +13,7 @@
from click import secho
import colorama
from bs4 import BeautifulSoup
import http.client

API_URL = "https://api.github.com/search/repositories"

Expand All @@ -27,26 +29,44 @@
"valid": "The request returned successfully, but an unknown exception occurred.",
}

FORMAT = "%(message)s"



httpclient_logger = logging.getLogger("http.client")

def httpclient_logging_debug(level=logging.DEBUG, debug_level=1):
hedyhli marked this conversation as resolved.
Show resolved Hide resolved

def httpclient_log(*args):
httpclient_logger.log(level, " ".join(args))

http.client.print = httpclient_log
http.client.HTTPConnection.debuglevel = 1


def debug_requests_on():
""" Turn on the logging for requests """

logging.basicConfig(
level=logging.DEBUG, format=FORMAT, datefmt="[%Y-%m-%d]", handlers=[RichHandler()]
)
logger = logging.getLogger(__name__)

try:
from http.client import HTTPConnection
httpclient_logging_debug(debug_level=1)

HTTPConnection.set_debuglevel(HTTPConnection, 1)
except ImportError:
import httplib

httplib.HTTPConnection.debuglevel = 2
httpclient_logging_debug(debug_level=2)
hedyhli marked this conversation as resolved.
Show resolved Hide resolved

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True



def convert_datetime(date, date_format="%Y-%m-%d"):
""" Safely convert a date string to datetime """
try:
Expand Down Expand Up @@ -98,6 +118,7 @@ def get_valid_request(url, auth=""):
f"{status_actions[handling_code]} {i} seconds...",
fg="bright_yellow",
) # Print and update a timer

sleep(1)
elif handling_code in status_actions:
secho(status_actions[handling_code], fg="bright_yellow")
Expand Down Expand Up @@ -154,8 +175,9 @@ def search(
date_format = "%Y-%m-%d" # date format in iso format
if debug:
debug_requests_on()
print("DEBUG: search: created param:", created)
print("DEBUG: search: order param: ", order)
logger = logging.getLogger(__name__)
logger.debug("Search: created param:" + created)
logger.debug("Search: order param: " + order)

day_range = 0 - randint(100, 400) # random negative from 100 to 400

Expand Down Expand Up @@ -193,12 +215,11 @@ def search(

url = f"{API_URL}?q={query}&sort=stars&order={order}" # use query to construct url
if debug:
print("DEBUG: search: url:", url) # print the url when debugging

logger.debug("Search: url:" + url) # print the url when debugging
if debug and auth:
print("DEBUG: auth: on")
logger.debug("Auth: on")
elif debug:
print("DEBUG: auth: off")
logger.debug("Auth: off")

request = get_valid_request(url, auth)
if request is None:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ def assertions(
"""
if exit_code:
assert result.exit_code == exit_code, f"`exit_code` should be '{exit_code}'"
if debug:
assert "DEBUG" in result.output, f"'DEBUG' not in `result.output`"
if output:
# if debug: # logs arent captured in result.output so it doesnt work
# assert "DEBUG" in result.output, f"'DEBUG' not in `result.output`"
if output and not debug:
assert result.output, "No cli output generated"
elif not output:
assert not result.output, "Cli output generated, but expected nothing"
Expand Down