-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
81 lines (60 loc) · 2.25 KB
/
logger.py
File metadata and controls
81 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import logging
from typing import Callable, Any
import time
import functools
import wandb
class Logger:
def __init__(self, dev: bool = True, wandb_logger: bool = True):
self.dev = dev
self.wandb = None
self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)
timestr = time.strftime("%Y%m%d-%H%M%S")
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
fh = logging.FileHandler(filename=f"./logs/test-{timestr}.logs", mode="a")
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
self.logger.addHandler(ch)
self.logger.addHandler(fh)
def wandb_init(self, config):
self.wandb = wandb.init(
project=config.project,
name=config.name,
tags=config.tags + ["dev"],
)
def log(self, msg: str | None = None, params: dict[str, str] | None = None) -> None:
if msg:
self.logger.info(msg=msg)
if params and self.wandb:
self.wandb.log(params)
def log_config(self, config):
if self.wandb:
self.wandb.config.update(config)
else:
raise ValueError()
def timing(logger: Logger, dev: bool = True):
if dev:
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
logger.log(f"Calling {func.__name__}")
ts = time.time()
value = func(*args, **kwargs)
te = time.time()
logger.log(f"Finished {func.__name__}")
if logger:
logger.log("func:%r took: %2.4f sec" % (func.__name__, te - ts))
return value
return wrapper
return decorator
else:
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
value = func(*args, **kwargs)
return value
return wrapper
return decorator