Skip to content

Commit

Permalink
[BUG] Change logging config (#307)
Browse files Browse the repository at this point in the history
* Change logging config.

* Some adjusts.

* Remove a code smell.
  • Loading branch information
moromimay authored Apr 6, 2021
1 parent 8077d86 commit 6d2a8f9
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 67 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ clean:
@find ./ -type f -name 'coverage.xml' -exec rm -f {} \;
@find ./ -type f -name '.coverage*' -exec rm -f {} \;
@find ./ -type f -name '*derby.log' -exec rm -f {} \;
@find ./ -type f -name 'logging.json' -exec rm -f {} \;
@find ./ -name '*.pyc' -exec rm -f {} \;
@find ./ -name '*.pyo' -exec rm -f {} \;
@find ./ -name '*~' -exec rm -f {} \;
Expand Down
7 changes: 0 additions & 7 deletions butterfree/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
"""Module docstring example, following Google's docstring style."""
import logging.config
import os
import sys

sys.path.insert(0, os.path.abspath("."))

logging.config.fileConfig(fname="butterfree/logging.conf")
14 changes: 8 additions & 6 deletions butterfree/_cli/migrate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import importlib
import inspect
import logging
import os
import pkgutil
import sys
Expand All @@ -11,13 +10,14 @@

from butterfree.clients import SparkClient
from butterfree.configs import environment
from butterfree.configs.logger import __logger
from butterfree.extract.readers import FileReader
from butterfree.migrations.database_migration import ALLOWED_DATABASE
from butterfree.pipelines import FeatureSetPipeline

app = typer.Typer(help="Apply the automatic migrations in a database.")

logger = logging.getLogger("migrate")
logger = __logger("migrate", True)


def __find_modules(path: str) -> Set[str]:
Expand Down Expand Up @@ -114,7 +114,9 @@ def __init__(

def _send_logs_to_s3(self, file_local: bool) -> None:
"""Send all migration logs to S3."""
file_reader = FileReader(id="name", path="logs/logging.json", format="json")
log_path = "../logging.json"

file_reader = FileReader(id="name", path=log_path, format="json")
df = file_reader.consume(self.spark_client)

path = environment.get_variable("FEATURE_STORE_S3_BUCKET")
Expand All @@ -126,10 +128,10 @@ def _send_logs_to_s3(self, file_local: bool) -> None:
**{"path": f"s3a://{path}/logging"},
)

if not file_local:
os.rmdir("logs/logging.json")
if not file_local and os.path.exists(log_path):
os.remove(log_path)

def run(self, generate_logs: bool) -> None:
def run(self, generate_logs: bool = False) -> None:
"""Construct and apply the migrations."""
for pipeline in self.pipelines:
for writer in pipeline.sink.writers:
Expand Down
24 changes: 24 additions & 0 deletions butterfree/configs/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Logger funcion."""

import logging


def __config(json_file_logs: bool = False) -> None:

if json_file_logs:
return logging.basicConfig(
format='{"name": "%(name)s", "timestamp": "%(asctime)-15s", '
'"level": "%(levelname)s", "message": "%(message)s"}',
level=logging.INFO,
filename="../logging.json",
)
return logging.basicConfig(
format="%(name)s:%(asctime)-15s:%(levelname)s:< %(message)s >",
level=logging.INFO,
)


def __logger(name: str, file_logs: bool = False) -> logging.Logger:

__config(file_logs)
return logging.getLogger(name)
52 changes: 0 additions & 52 deletions butterfree/logging.conf

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from typing import Any, Dict, List, Set

from butterfree.clients import AbstractClient
from butterfree.configs.logger import __logger
from butterfree.load.writers.writer import Writer
from butterfree.transform import FeatureSet

logger = logging.getLogger("database_migrate")
logger = __logger("database_migrate", True)


@dataclass
Expand Down
Empty file removed logs/logging.json
Empty file.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import find_packages, setup

__package_name__ = "butterfree"
__version__ = "1.2.0.dev6"
__version__ = "1.2.0.dev7"
__repository_url__ = "https://github.com/quintoandar/butterfree"

with open("requirements.txt") as f:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/butterfree/_cli/test_migrate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from unittest.mock import call

from typer.testing import CliRunner

from butterfree._cli import migrate
from butterfree._cli.main import app
from butterfree.migrations.database_migration import (
CassandraMigration,
MetastoreMigration,
)
from butterfree.pipelines import FeatureSetPipeline

runner = CliRunner()


class TestMigrate:
def test_migrate_success(self, mocker):
Expand Down Expand Up @@ -38,3 +43,12 @@ def test_migrate_all_pairs(self, mocker):
cassandra_pairs, any_order=True
)
migrate.Migrate._send_logs_to_s3.assert_called_once()

def test_app_cli(self):
result = runner.invoke(app, "migrate")
assert result.exit_code == 0

def test_app_migrate(self, mocker):
mocker.patch.object(migrate.Migrate, "run")
result = runner.invoke(app, ["migrate", "apply", "tests/mocks/entities/"])
assert result.exit_code == 0

0 comments on commit 6d2a8f9

Please sign in to comment.