Skip to content

Commit

Permalink
Add a correct and clean logging configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothiraldan committed Jun 4, 2018
1 parent e37f8d9 commit 0eef9ad
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
25 changes: 25 additions & 0 deletions balto/_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging

VERBOSE_LOG_FORMAT = "[%(asctime)s] %(pathname)s:%(lineno)d: %(message)s"


def setup_logging(verbose=False, debug=False):
logger = logging.getLogger("balto")
logger.setLevel(logging.DEBUG)

# Reset handlers
logger.handlers = []

# Add handler
console = logging.StreamHandler()

if debug is False:
console.setLevel(logging.INFO)
else:
console.setLevel(logging.DEBUG)

if verbose is True:
formatter = logging.Formatter(VERBOSE_LOG_FORMAT)
console.setFormatter(formatter)

logger.addHandler(console)
18 changes: 16 additions & 2 deletions balto/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
from __future__ import print_function, unicode_literals

import argparse
import logging
import os
import shutil
import subprocess
import time
import webbrowser
from multiprocessing import Process

from balto._logging import setup_logging
from balto.server import server

LOGGER = logging.getLogger(__name__)


def main():
parser = argparse.ArgumentParser(description=__doc__)
Expand All @@ -25,11 +29,21 @@ def main():
action="store",
default="curses",
)
parser.add_argument(
"--verbose",
"-v",
help="activate the verbose mode",
action="store_true",
default=False,
)
parser.add_argument(
"--debug", help="activate the debug mode", action="store_true", default=False
)
args = parser.parse_args()

setup_logging(args.verbose, args.debug)

# Launch the server
balto_server_full_path = shutil.which("balto-server")
server_args = [balto_server_full_path, args.directory]
port = 8889

try:
Expand Down
5 changes: 4 additions & 1 deletion balto/runners/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
""" Base runner class
"""
import json
import logging

LOGGER = logging.getLogger(__name__)


def command_formatter(tool, tests_to_run, collect_only):
Expand Down Expand Up @@ -36,7 +39,7 @@ def parse_line(line):
try:
data = json.loads(decodedline)
except (json.JSONDecodeError, ValueError) as e:
# print("Invalid line", e, repr(decodedline))
LOGGER.debug("Invalid line: %r", decodedline, exc_info=True)
return

return data
Expand Down
4 changes: 4 additions & 0 deletions balto/runners/docker_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import logging
import tarfile
import tempfile
from io import BytesIO
Expand All @@ -15,6 +16,8 @@
DOCKER = DockerClient()
AIODOCKER = Docker()

LOGGER = logging.getLogger(__name__)


def prepare_string_for_tar(name, content):
dfinfo = tarfile.TarInfo(name)
Expand Down Expand Up @@ -97,6 +100,7 @@ async def _launch_container(self, docker_img, local=False):
await self.read_line(line)

def is_local_docker_host(self):
LOGGER.debug("Docker base url: %r", DOCKER.api.base_url)
if DOCKER.api.base_url in ("http+docker://localunixsocket",):
return True

Expand Down
5 changes: 4 additions & 1 deletion balto/runners/subprocess_runner.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import asyncio
import json
import logging
import shlex

from balto.runners.base import BaseRunner

LOGGER = logging.getLogger(__name__)


async def _read_stream(stream, cb):
while True:
Expand Down Expand Up @@ -39,6 +42,6 @@ async def launch_cmd(self, cmd):
return_code = await process.wait()

if return_code != 0:
print("cmd exited with return code: %r" % return_code)
LOGGER.warning("CMD %r exited with return code: %d", cmd, return_code)

return return_code
22 changes: 19 additions & 3 deletions balto/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
from aiohttp.web import Application, FileResponse, HTTPNotFound, run_app
from aiohttp_json_rpc import JsonRpc

from balto._logging import setup_logging
from balto.config import read_config
from balto.event_emitter import EventEmitter
from balto.store import Tests

LOGGER = logging.getLogger(__name__)


def get_static_path():
if getattr(sys, "frozen", False):
Expand Down Expand Up @@ -52,14 +55,15 @@ def server(directory):
tests = Tests(suites)

async def collect_all(request):
print("COLLECT ALL")
LOGGER.info("Collect ALL")
tasks = [
suite.collect_all(directory, em, loop=loop) for suite in suites.values()
]
await asyncio.gather(*tasks, loop=loop)
return "ok"

async def run_all(request):
LOGGER.info("Run ALL")
tasks = [
suite.launch_all(directory, em, loop=loop) for suite in suites.values()
]
Expand All @@ -68,7 +72,7 @@ async def run_all(request):

async def run_selected(request):
tasks = []
print("GOT PARAMS", request.params)
LOGGER.info("Run selected: %r", request.params)
for suite_name, suite_tests in request.params.items():
suite = suites[suite_name]
tasks.append(suite.launch_tests(directory, em, loop, suite_tests))
Expand All @@ -80,7 +84,7 @@ async def run_selected(request):
logging.getLogger("aiohttp-json-rpc.server").setLevel(logging.DEBUG)

async def forward_notifications(message):
print("MESSAGE", message)
LOGGER.debug("Forwarding to %d clients: %r", len(rpc.clients), message)
for client in rpc.clients:
data = {"jsonrpc": "2.0", "id": None, "method": "test", "params": message}
r = await client.ws.send_str(json.dumps(data))
Expand All @@ -106,8 +110,20 @@ def main():
parser.add_argument(
"directory", help="The directory LITR should start looking for its config file"
)
parser.add_argument(
"--verbose",
"-v",
help="activate the verbose mode",
action="store_true",
default=False,
)
parser.add_argument(
"--debug", help="activate the debug mode", action="store_true", default=False
)
args = parser.parse_args()

setup_logging(args.verbose, args.debug)

server(args.directory)


Expand Down

0 comments on commit 0eef9ad

Please sign in to comment.