-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.py
106 lines (79 loc) · 2.62 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/env/bin python3
"""
Logger class for postoga.
Manages the logging of postoga, recording
all the steps and errors that occur during
the execution of the tool.
It is recommended to use the `Log` class
in the following way:
``` python
from logger import Log
log = Log("path/to/log", "name")
log.start()
log.record("This is a message.")
log.close()
```
The above code will create a log file in
`path/to/log` named `name.log` with the
following contents:
```
Log file for name.
This is a message.
```
This class have been included in postoga
to better manage the logging of the tool.
"""
import os
import datetime
from constants import Constants
from modules.utils import shell
import logging
__author__ = "Alejandro Gonzales-Irribarren"
__email__ = "jose.gonzalesdezavala1@unmsm.edu.pe"
__github__ = "https://github.com/alejandrogzi"
__version__ = "0.9.0-devel"
class Log:
"""Logger class for postoga."""
def __init__(self, path: str, log_file: str):
self.log_file = os.path.join(path, log_file)
self.version = __version__
# self.commit = shell(Constants.Commands.COMMIT)
# self.branch = shell(Constants.Commands.BRANCH)
def start(self):
logging.basicConfig(
filename=self.log_file,
level=logging.INFO,
format="[%(asctime)s] - %(levelname)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def intro(self):
start_message = f"{'#'*36}\npostoga: the post-TOGA processing pipeline"
version = f"version: {self.version}"
# commit = f"commit: {self.commit}"
# branch = f"branch: {self.branch}\n\n"
# metadata = "\n".join([version, commit, branch])
metadata = "\n".join([version])
with open(self.log_file, "w") as log:
log.write("\n".join([start_message, metadata]))
print(start_message + "\n" + metadata)
def record(self, message, timestamp=True):
logging.info(message)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp}] - INFO: {message}")
@classmethod
def connect(cls, path, log_file):
log = cls(path, log_file)
log.start()
return log
def close(self):
end_message = f"postoga finished!\n{'#'*36}"
logging.info(end_message)
if __name__ == "__main__":
# To create a new log file
log = Log("path/to/log", "name")
log.start()
log.record("This is a message.")
log.record("Another message.")
# To use an existing log file
# log = Log.connect("my_existing_log.log")
# log.record("Log this message in the existing log.")