Skip to content

Commit 7ed6491

Browse files
committed
Centralize logging and remove old logging dependencies
Signed-off-by: m09 <142691+m09@users.noreply.github.com>
1 parent b50c395 commit 7ed6491

File tree

7 files changed

+27
-21
lines changed

7 files changed

+27
-21
lines changed

formatml/pipelines/codrep/index.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import List
55

66
from asdf import open as asdf_open
7-
from coloredlogs import install as coloredlogs_install
87

98
from formatml.data.fields.binary_label_field import BinaryLabelsField
109
from formatml.data.fields.graph_fields.internal_type_field import InternalTypeField
@@ -15,6 +14,7 @@
1514
from formatml.data.types.codrep_label import CodRepLabel
1615
from formatml.parsing.parser import Nodes
1716
from formatml.pipelines.pipeline import register_step
17+
from formatml.utils.helpers import setup_logging
1818

1919

2020
def add_arguments_to_parser(parser: ArgumentParser) -> None:
@@ -52,7 +52,7 @@ def index(
5252
log_level: str,
5353
) -> None:
5454
"""Index UASTs with respect to some fields."""
55-
coloredlogs_install(level=log_level, fmt="%(name)27s %(levelname)8s %(message)s")
55+
setup_logging(log_level)
5656
logger = getLogger(__name__)
5757

5858
input_dir_path = Path(input_dir).expanduser().resolve()

formatml/pipelines/codrep/parse.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
from time import time
55

66
from asdf import AsdfFile
7-
from coloredlogs import install as coloredlogs_install
87

98
from formatml.data.types.codrep_label import CodRepLabel
109
from formatml.parsing.java_parser import JavaParser
1110
from formatml.parsing.parser import ParsingException
1211
from formatml.pipelines.pipeline import register_step
12+
from formatml.utils.helpers import setup_logging
1313

1414

1515
def add_arguments_to_parser(parser: ArgumentParser) -> None:
@@ -29,7 +29,7 @@ def add_arguments_to_parser(parser: ArgumentParser) -> None:
2929
)
3030
def codrep_train(*, input_dir: str, output_dir: str, log_level: str) -> None:
3131
"""Parse a CodRep 2019 dataset into UASTs."""
32-
coloredlogs_install(level=log_level, fmt="%(name)27s %(levelname)8s %(message)s")
32+
setup_logging(log_level)
3333
logger = getLogger(__name__)
3434
input_dir_path = Path(input_dir).expanduser().resolve()
3535
output_dir_path = Path(output_dir).expanduser().resolve()

formatml/pipelines/codrep/tensorize.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
from pickle import dump as pickle_dump, load as pickle_load
99

1010
from asdf import open as asdf_open
11-
from coloredlogs import install as coloredlogs_install
1211

1312
from formatml.data.instance import Instance
1413
from formatml.data.types.codrep_label import CodRepLabel
1514
from formatml.parsing.parser import Nodes
1615
from formatml.pipelines.pipeline import register_step
16+
from formatml.utils.helpers import setup_logging
1717

1818

1919
def add_arguments_to_parser(parser: ArgumentParser) -> None:
@@ -53,7 +53,7 @@ def tensorize(
5353
log_level: str,
5454
) -> None:
5555
"""Tensorize the UASTs."""
56-
coloredlogs_install(level=log_level, fmt="%(name)27s %(levelname)8s %(message)s")
56+
setup_logging(log_level)
5757
logger = getLogger(__name__)
5858

5959
uasts_dir_path = Path(uasts_dir).expanduser().resolve()

formatml/pipelines/codrep/train.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pickle import load as pickle_load
66
from typing import List, Optional
77

8-
from coloredlogs import install as coloredlogs_install
98
from torch.nn import Linear
109
from torch.optim import Adam
1110
from torch.optim.lr_scheduler import StepLR
@@ -16,6 +15,7 @@
1615
from formatml.modules.misc.graph_embedding import GraphEmbedding
1716
from formatml.pipelines.pipeline import register_step
1817
from formatml.training.trainer import Trainer
18+
from formatml.utils.helpers import setup_logging
1919

2020

2121
def add_arguments_to_parser(parser: ArgumentParser) -> None:
@@ -128,7 +128,7 @@ def train(
128128
log_level: str,
129129
) -> None:
130130
"""Run the training."""
131-
coloredlogs_install(level=log_level, fmt="%(name)27s %(levelname)8s %(message)s")
131+
setup_logging(log_level)
132132
logger = getLogger(__name__)
133133

134134
tensors_dir_path = Path(tensors_dir).expanduser().resolve()

formatml/utils/helpers.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
from datetime import datetime
22
from itertools import chain
3+
from logging import Filter, getLogger, LogRecord
34
from pathlib import Path
45
from sys import exit, stderr
56
from typing import Any, Iterable, List, Optional, Tuple, Type, TypeVar, Union
67

8+
from coloredlogs import install as coloredlogs_install
79
from dulwich.errors import NotGitRepository
810
from dulwich.porcelain import status
911
from dulwich.repo import Repo
1012

1113

14+
class ShortNameFilter(Filter):
15+
def filter(self, record: LogRecord) -> int:
16+
record.shortname = record.name.split(".")[-1] # type: ignore
17+
return 1
18+
19+
20+
def setup_logging(log_level: str) -> None:
21+
coloredlogs_install(
22+
level=log_level,
23+
fmt="%(asctime)s %(shortname)10s %(message)s",
24+
datefmt="%H:%M:%S",
25+
)
26+
getLogger().handlers[0].addFilter(ShortNameFilter())
27+
28+
1229
def get_sha_and_dirtiness(prompt_on_dirty: bool = True) -> Optional[Tuple[str, bool]]:
1330
try:
1431
git_status = status()

requirements.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ dgl == 0.3
44
bblfsh == 2.12.7
55
asdf == 2.3.3
66
dulwich == 0.19.11
7-
tf-nightly == 1.15.0.dev20190710
8-
future == 0.17.1
9-
Pillow == 6.1.0
7+
tensorboard == 1.14.0

setup.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,7 @@
3131
url="https://github.com/src-d/formatml",
3232
packages=find_packages(exclude=["tests"]),
3333
entry_points={"console_scripts": ["formatml=formatml.__main__:main"]},
34-
install_requires=[
35-
"coloredlogs",
36-
"bblfsh <3.0",
37-
"asdf",
38-
"dulwich",
39-
"tf-nightly",
40-
# Below are extra deps for TF
41-
"Pillow",
42-
"future",
43-
],
34+
install_requires=["coloredlogs", "bblfsh <3.0", "asdf", "dulwich", "tensorboard"],
4435
include_package_data=True,
4536
license="Apache-2.0",
4637
classifiers=[

0 commit comments

Comments
 (0)