Skip to content

Commit

Permalink
Logging in synctransactions (#34)
Browse files Browse the repository at this point in the history
Logging in synctransactions
  • Loading branch information
madnesspie committed Apr 24, 2020
2 parents 625f156 + 624cc6a commit c934e25
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 26 deletions.
2 changes: 1 addition & 1 deletion django_obm/__init__.py
Expand Up @@ -11,4 +11,4 @@
# 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.
__version__ = "0.2.4"
__version__ = "0.2.5"
16 changes: 16 additions & 0 deletions django_obm/logger.py
@@ -0,0 +1,16 @@
import logging

logging.addLevelName(logging.DEBUG, "🐛 DEBUG")
logging.addLevelName(logging.INFO, "📑 INFO")
logging.addLevelName(logging.WARNING, "⚠️ WARNING")
logging.addLevelName(logging.ERROR, "🚨 ERROR")
logging.addLevelName(logging.CRITICAL, "💥 CRITICAL")


def info_filter(record):
return record.levelno < logging.WARNING


def get(name):
logger = logging.getLogger(name)
return logger
17 changes: 7 additions & 10 deletions django_obm/management/commands/synctransactions.py
Expand Up @@ -13,16 +13,16 @@
# limitations under the License.
import time
import traceback
from datetime import datetime

from django.conf import settings
from django.core.management.base import BaseCommand

from django_obm import models
from django_obm import logger, models


class Command(BaseCommand):
DEFAULT_FREQUENCY = 60
logger = logger.get(__name__)
help = "Sync transactions with blockchain with specified frequency."

def add_arguments(self, parser):
Expand Down Expand Up @@ -50,28 +50,25 @@ def add_arguments(self, parser):
help="The frequency of sync transactions running.",
)

def log(self, msg, style=None):
msg = f"{datetime.now()}: {msg}"
self.stdout.write(style(msg) if style else msg)

def handle(self, *args, **options):
frequency = options["frequency"]
self.log(
self.logger.info(
f"Start sync transactions proccess with "
f"{frequency} sec. frequency"
)

while True:
self.log("Run synchronization")
self.logger.debug("Run synchronization")
try:
result = models.Node.objects.sync_transactions()
except Exception as exc: # pylint: disable=broad-except
if options["raise_errors"]:
raise exc
self.log(traceback.format_exc(), style=self.style.ERROR)
self.logger.error(traceback.format_exc())
else:
self.log("Synchronized transactions", style=self.style.SUCCESS)
self.logger.debug("Synchronized transactions")

if options["once"]:
break
self.logger.debug(f"Sleep for {frequency} sec.")
time.sleep(frequency)
2 changes: 1 addition & 1 deletion django_obm/models.py
Expand Up @@ -116,7 +116,7 @@ def __str__(self):
@property
def confirmations_number(self) -> int:
# TODO: To obm
if self.block_number:
if self.block_number is not None:
latest_block_number = self.node.get_latest_block_number()
self.node.close()
return latest_block_number - self.block_number
Expand Down
46 changes: 44 additions & 2 deletions example/example/settings.py
Expand Up @@ -11,6 +11,9 @@
"""

import os
import sys

from django_obm import logger

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -25,7 +28,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = [] # type: ignore


# Application definition
Expand Down Expand Up @@ -119,6 +122,45 @@

STATIC_URL = "/static/"

# Logging
LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"verbose": {
"format": "[%(asctime)s] %(levelname)-7s %(name)-25s %(message)s",
"formatTime": "%d/%m/%Y %H:%M:%S",
"style": "%",
},
},
"filters": {
"info_filter": {
"()": "django.utils.log.CallbackFilter",
"callback": logger.info_filter,
},
},
"handlers": {
"stdout": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"stream": sys.stdout,
"filters": ["info_filter"],
"formatter": "verbose",
},
"stderr": {
"level": "WARNING",
"class": "logging.StreamHandler",
"formatter": "verbose",
},
},
"loggers": {
"django_obm.management.commands.synctransactions": {
"handlers": ["stdout", "stderr"],
"level": "DEBUG",
}
},
}

# Django OBM
OBM_NODES_INITIAL_CONFIG = [
{
Expand All @@ -134,7 +176,7 @@
"currency": {"name": "ethereum"},
"name": "geth",
"is_default": True,
"rpc_host": 'localhost',
"rpc_host": "localhost",
"rpc_port": 8545,
},
]
1 change: 1 addition & 0 deletions pylintrc
Expand Up @@ -58,6 +58,7 @@ disable=
too-few-public-methods,
fixme,
too-many-ancestors,
logging-format-interpolation,
R0801, # Stupid similar lines in 2 files :)


Expand Down
18 changes: 7 additions & 11 deletions tests/commands/test_synctransactions.py
Expand Up @@ -11,8 +11,6 @@
# 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 io

import pytest
from django.core import management

Expand All @@ -22,13 +20,12 @@ class TestCommand:
@staticmethod
@pytest.mark.django_db
@pytest.mark.usefixtures("bitcoin_core_node")
def test_command():
out = io.StringIO()
def test_command(caplog):
management.call_command(
"synctransactions", once=True, stdout=out,
"synctransactions", once=True
)
assert "Start" in out.getvalue()
assert "Synchronized" in out.getvalue()
assert "Start" in caplog.text
assert "Synchronized" in caplog.text

@staticmethod
@pytest.mark.django_db
Expand All @@ -40,9 +37,8 @@ def test_command():
({"frequency": 15}, "15 sec. frequency",),
),
)
def test_command_with_frequency(options, output):
out = io.StringIO()
def test_command_with_frequency(caplog, options, output):
management.call_command(
"synctransactions", **options, once=True, stdout=out,
"synctransactions", **options, once=True
)
assert output in out.getvalue()
assert output in caplog.text
44 changes: 43 additions & 1 deletion tests/settings.py
Expand Up @@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys

from django_obm import logger

SECRET_KEY = "fake-key"
INSTALLED_APPS = [
Expand All @@ -28,6 +31,45 @@
}
}

# Logging
LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"verbose": {
"format": "[%(asctime)s] %(levelname)-7s %(name)-25s %(message)s",
"formatTime": "%d/%m/%Y %H:%M:%S",
"style": "%",
},
},
"filters": {
"info_filter": {
"()": "django.utils.log.CallbackFilter",
"callback": logger.info_filter,
},
},
"handlers": {
"stdout": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"stream": sys.stdout,
"filters": ["info_filter"],
"formatter": "verbose",
},
"stderr": {
"level": "WARNING",
"class": "logging.StreamHandler",
"formatter": "verbose",
},
},
"loggers": {
"django_obm.management.commands.synctransactions": {
"handlers": ["stdout", "stderr"],
"level": "DEBUG",
}
},
}

# Django OBM
OBM_LIST_TRANSACTIONS_COUNT = 10
OBM_REST_SUBTRACT_TRANSACTION_FEE_FROM_AMOUNT_DEFAULT = True
Expand All @@ -46,7 +88,7 @@
"currency": {"name": "ethereum"},
"name": "geth",
"is_default": True,
"rpc_host": 'localhost',
"rpc_host": "localhost",
"rpc_port": 8545,
},
]
Expand Down

0 comments on commit c934e25

Please sign in to comment.