Skip to content

Commit 1d6645c

Browse files
author
Ashutosh Tiwari
committed
logger done! need to add node2vec tests
1 parent 8e27ec4 commit 1d6645c

File tree

4 files changed

+10
-38
lines changed

4 files changed

+10
-38
lines changed

graph_ml/models/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from ..models.torch_node2vec import TorchNode2Vec
21
from ..models.gensim_node2vec import GensimNode2Vec
2+
from ..models.torch_node2vec import TorchNode2Vec

graph_ml/utils/logger.py

+6-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
1-
import logging
21
import asyncio
3-
from logging import getLogger, StreamHandler, FileHandler, Formatter
4-
from concurrent.futures import ThreadPoolExecutor
2+
import logging
3+
from logging import Formatter, StreamHandler, getLogger
4+
55
from colorama import Fore, Style, init
6+
67
from ..utils import utils
78

89
# Initialize colorama for Windows compatibility
910
init(autoreset=True)
1011

1112

12-
class AsyncFileHandler(FileHandler):
13-
def __init__(self, filename):
14-
super().__init__(filename)
15-
self.loop = asyncio.get_event_loop()
16-
self.executor = ThreadPoolExecutor()
17-
18-
async def emit(self, record):
19-
msg = self.format(record)
20-
await self.loop.run_in_executor(self.executor, self._write, msg)
21-
22-
def _write(self, msg):
23-
with self._open() as file:
24-
file.write(f"{msg}\n")
25-
file.flush()
26-
27-
2813
class ColorFormatter(Formatter):
2914
"""Custom formatter to add colors depending on the log level."""
3015

@@ -49,7 +34,7 @@ def __new__(cls, *args, **kwargs):
4934
cls._instance = super(AsyncLogger, cls).__new__(cls)
5035
return cls._instance
5136

52-
def __init__(self, name, file_path=None):
37+
def __init__(self, name):
5338
if hasattr(self, "_initialized") and self._initialized:
5439
return # Prevent re-initialization
5540
self._initialized = True
@@ -67,14 +52,6 @@ def __init__(self, name, file_path=None):
6752
console_handler.setFormatter(formatter)
6853
self.logger.addHandler(console_handler)
6954

70-
# File Handler (Asynchronous)
71-
if file_path:
72-
async_file_handler = AsyncFileHandler(file_path)
73-
async_file_handler.setFormatter(
74-
Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
75-
) # No color for file
76-
self.logger.addHandler(async_file_handler)
77-
7855
async def info(self, message):
7956
await self._log_async(self.logger.info, message)
8057

@@ -116,5 +93,5 @@ def debug(self, message):
11693

11794

11895
# Exposing the logger as a singleton instance
119-
logger_instance = AsyncLogger(name="graph_ml", file_path="graph_ml.log")
96+
logger_instance = AsyncLogger(name="graph_ml")
12097
logger = LoggerWrapper(logger_instance)

graph_ml/utils/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
import asyncio
12
import os
23

34
import numpy as np
45
from numba import njit
56
from scipy import sparse
6-
import asyncio
7-
87

98
# no global variables in this module
109

tests/test_logger.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from graph_ml.utils.logger import logger
2-
from os import path
32

43

5-
def test_output_file():
4+
def test_logger():
65
logger.info("Test info message")
76
logger.error("Test error message")
87
logger.debug("Test debug message")
9-
# check output file graph_ml.log exists
10-
assert path.exists("graph_ml.log")
11-
# check output file is not empty
12-
assert path.getsize("graph_ml.log") > 0
8+
assert True

0 commit comments

Comments
 (0)