Skip to content

Commit

Permalink
Merge pull request #10 from Multiscale-Genomics/logger-progress
Browse files Browse the repository at this point in the history
Logger progress functionality
  • Loading branch information
athina1 committed Jun 6, 2018
2 parents 54754d0 + 2e1755e commit a674465
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ Author: WHO?

- Change N.

**********
31/05/2018
**********
Author: Mark McDowall

- Added Tests for the logger
- Changed the functionality of the PROGRESS logger so that it is more flexible and can be used within the VRE

**********
26/10/2017
**********
Expand Down
104 changes: 104 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""
.. See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import pytest

from utils import logger


@pytest.mark.debug
def test_debug(capsys):
"""
Test the basic use of the DEBUG function
"""
logger.debug("test")
captured = capsys.readouterr()
assert captured[0] == "DEBUG: test\n"


@pytest.mark.info
def test_info(capsys):
"""
Test the basic use of the INFO function
"""
logger.info("test")
captured = capsys.readouterr()
assert captured[0] == "INFO: test\n"


@pytest.mark.warn
def test_warn(capsys):
"""
Test the basic use of the WARN function
"""
logger.warn("test")
captured = capsys.readouterr()
assert captured[1] == "WARNING: test\n"


@pytest.mark.error
def test_error(capsys):
"""
Test the basic use of the ERROR function
"""
logger.error("test")
captured = capsys.readouterr()
assert captured[1] == "ERROR: test\n"


@pytest.mark.fatal
def test_fatal(capsys):
"""
Test the basic use of the FATAL function
"""
logger.fatal("test")
captured = capsys.readouterr()
assert captured[1] == "FATAL: test\n"


@pytest.mark.progress
def test_progress_00(capsys):
"""
Test the basic use of the PROGRESS function
"""
logger.progress("test")
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test\n"


@pytest.mark.progress
def test_progress_01(capsys):
"""
Test the PROGRESS function for reporting task completion
"""
logger.progress("test", status="RUNNING")
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test - RUNNING\n"

logger.progress("test", status="DONE")
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test - DONE\n"


@pytest.mark.progress
def test_progress_02(capsys):
"""
Test the PROGRESS function for reporting tool progress
"""
logger.progress("test", task_id=2, total=5)
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test (2/5)\n"
84 changes: 62 additions & 22 deletions utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@
It provides the following commonly used logging levels:
DEBUG: Detailed information, typically of interest only when
diagnosing problems.
INFO: Confirmation that Tool execution is working as expected.
diagnosing problems.
INFO: Confirmation that Tool execution is working as expected.
WARNING: An indication that something unexpected happened, but that the
Tool can continue working successfully.
ERROR: A more serious problem has occurred, and the Tool will not be
able to perform some function.
FATAL: A serious error, indicating that the Tool may be unable to
continue running.
continue running.
As well as the following non-standard levels:
PROGRESS: Provide the VRE with information about Tool execution progress,
in the form of a percentage (0-100)
PROGRESS: Provide the VRE with information about Tool execution progress
"""

import sys
Expand All @@ -54,16 +53,17 @@
STDERR_LEVELS = [WARNING, ERROR, FATAL, CRITICAL]

_levelNames = {
FATAL : 'FATAL',
ERROR : 'ERROR',
WARNING : 'WARNING',
PROGRESS : 'PROGRESS',
INFO : 'INFO',
DEBUG : 'DEBUG',
WARN : 'WARNING',
CRITICAL : 'FATAL'
FATAL: 'FATAL',
ERROR: 'ERROR',
WARNING: 'WARNING',
PROGRESS: 'PROGRESS',
INFO: 'INFO',
DEBUG: 'DEBUG',
WARN: 'WARNING',
CRITICAL: 'FATAL'
}


def __log(level, message, *args, **kwargs):
if level not in _levelNames:
level = INFO
Expand All @@ -81,7 +81,7 @@ def debug(message, *args, **kwargs):
'message' is the message format string, and the args are the arguments
which are merged into msg using the string formatting operator. (Note that
this means that you can use keywords in the format string, together with a
single dictionary argument.)
single dictionary argument.)
"""
return __log(DEBUG, message, *args, **kwargs)

Expand Down Expand Up @@ -117,15 +117,55 @@ def fatal(message, *args, **kwargs):
critical=fatal

## Special loggers
def progress(percentage):
def progress(message, *args, **kwargs):
"""
Provides information about progress.
Provides information about Tool progress.
Logs a message containing information about Tool progress, with level
PROGRESS.
The arguments are interpreted as for debug() (see below for exceptions).
This function provides two pre-baked log message formats, that can be
activated by specifying the following items in **kwargs:
status : Status of the Tool
logs "MESSAGE - STATUS"
task_id : Current task; requires also the "total" item
logs "MESSAGE (TASK_ID/TOTAL)
Example
-------
.. code-block:: python
:linenos:
class TestTool(Tool):
# ...
def run(self, input_files, input_metadata, output_files):
logger.progress("TestTool starting", status="RUNNING")
total_tasks = 3
self.task1()
logger.progress("TestTool", task_id=1, total=total_tasks)
self.task2()
logger.progress("TestTool", task_id=2, total=total_tasks)
self.task3()
logger.progress("TestTool", task_id=3, total=total_tasks)
In fact it logs a message containing the percentage with level PERCENTAGE.
logger.progress("TestTool", status="DONE")
return True
"""
if percentage > 100:
percentage = 100
if percentage < 0:
percentage = 0
return __log(PROGRESS, "{}", percentage)
if "status" in kwargs:
return __log(PROGRESS, "{} - {}", message, kwargs["status"])
elif "task_id" in kwargs:
return __log(PROGRESS, "{} ({}/{})", message, kwargs["task_id"], kwargs["total"])
else:
return __log(PROGRESS, message, *args, **kwargs)
return False

0 comments on commit a674465

Please sign in to comment.