Skip to content

Commit

Permalink
Merge pull request #12 from Multiscale-Genomics/logger_timestamp
Browse files Browse the repository at this point in the history
Logger timestamp
  • Loading branch information
athina1 committed Jul 6, 2018
2 parents 5dd6798 + 993c4ee commit 9c74830
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
19 changes: 10 additions & 9 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
limitations under the License.
"""

import re
import pytest

from utils import logger
Expand All @@ -27,7 +28,7 @@ def test_debug(capsys):
"""
logger.debug("test")
captured = capsys.readouterr()
assert captured[0] == "DEBUG: test\n"
assert re.search("DEBUG: test", captured[0])


@pytest.mark.info
Expand All @@ -37,7 +38,7 @@ def test_info(capsys):
"""
logger.info("test")
captured = capsys.readouterr()
assert captured[0] == "INFO: test\n"
assert re.search("INFO: test", captured[0])


@pytest.mark.warn
Expand All @@ -47,7 +48,7 @@ def test_warn(capsys):
"""
logger.warn("test")
captured = capsys.readouterr()
assert captured[1] == "WARNING: test\n"
assert re.search("WARNING: test", captured[1])


@pytest.mark.error
Expand All @@ -57,7 +58,7 @@ def test_error(capsys):
"""
logger.error("test")
captured = capsys.readouterr()
assert captured[1] == "ERROR: test\n"
assert re.search("ERROR: test", captured[1])


@pytest.mark.fatal
Expand All @@ -67,7 +68,7 @@ def test_fatal(capsys):
"""
logger.fatal("test")
captured = capsys.readouterr()
assert captured[1] == "FATAL: test\n"
assert re.search("FATAL: test", captured[1])


@pytest.mark.progress
Expand All @@ -77,7 +78,7 @@ def test_progress_00(capsys):
"""
logger.progress("test")
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test\n"
assert re.search("PROGRESS: test", captured[0])


@pytest.mark.progress
Expand All @@ -87,11 +88,11 @@ def test_progress_01(capsys):
"""
logger.progress("test", status="RUNNING")
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test - RUNNING\n"
assert re.search("PROGRESS: test - RUNNING", captured[0])

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


@pytest.mark.progress
Expand All @@ -101,4 +102,4 @@ def test_progress_02(capsys):
"""
logger.progress("test", task_id=2, total=5)
captured = capsys.readouterr()
assert captured[0] == "PROGRESS: test (2/5)\n"
assert re.search("PROGRESS: test \(2\/5\)", captured[0])
12 changes: 10 additions & 2 deletions utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""

import sys
import datetime

"""
This is the logging facility of the mg-tool-api. It is meant to provide
Expand Down Expand Up @@ -66,13 +67,20 @@


def __log(level, message, *args, **kwargs):
"""
Function to print out the logging input
"""
log_time = datetime.datetime.now()
log_ts = "{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
log_time.year, log_time.month, log_time.day,
log_time.hour, log_time.minute, log_time.second)
if level not in _levelNames:
level = INFO
outstream = sys.stdout
if level in STDERR_LEVELS:
outstream = sys.stderr
outstream.write("{}: {}\n".format(
_levelNames[level], message.format(*args, **kwargs)))
outstream.write("{} | {}: {}\n".format(
log_ts, _levelNames[level], message.format(*args, **kwargs)))
return True


Expand Down

0 comments on commit 9c74830

Please sign in to comment.