-
Notifications
You must be signed in to change notification settings - Fork 25
/
logger.py
61 lines (45 loc) · 1.88 KB
/
logger.py
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
import logging
import sys
from google.auth.credentials import Credentials
from tools.logger.type import LogType
class Logger(logging.Logger):
"""Logger."""
def __init__(
self,
name: str,
project: str | None = None,
credentials: Credentials | None = None,
log_type: LogType = LogType.LOCAL,
) -> None:
"""Initialize local logger formatter.
Args:
name (str): Logger name
project (str | None, optional): Google Cloud Project ID. Defaults to None.
credentials (Credentials | None, optional): Credentials for Google Cloud.
Defaults to None.
log_type (LogType, optional): Local or something.
Defaults to LogType.LOCAL.
Examples:
>>> from tools import Logger, LogType
>>> local_logger = Logger(name=__name__, log_type=LogType.LOCAL)
>>> gc_logger = Logger(name=__name__, log_type=LogType.GOOGLE_CLOUD)
"""
super().__init__(name=name)
if log_type == LogType.GOOGLE_CLOUD:
import google.cloud.logging
from google.cloud.logging_v2.handlers import StructuredLogHandler
from tools.logger import GoogleCloudFormatter
client = google.cloud.logging.Client(
project=project, credentials=credentials
)
client.setup_logging()
formatter = GoogleCloudFormatter()
handler = StructuredLogHandler(stream=sys.stdout)
handler.setFormatter(formatter)
self.addHandler(handler)
return
from tools.logger import LocalFormatter
formatter = LocalFormatter()
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
self.addHandler(handler)