forked from treble-maker123/deep-face-hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
38 lines (33 loc) · 1.36 KB
/
logger.py
File metadata and controls
38 lines (33 loc) · 1.36 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
import os
from time import time, strftime
from datetime import datetime
from utils import *
class Logger(object):
'''
Convenience class for logging metrics to a file.
'''
def __init__(self, **kwargs):
self.print_to_stdout = kwargs.get("print_to_std", True)
self._write_to_file = kwargs.get("write_to_file", False)
current_dir = os.getcwd()
file_name = kwargs.get("file_name", self._get_time())
self.output_path = kwargs.get("output_path",
current_dir + "/logs/{}.txt"
.format(file_name))
if self._write_to_file:
mkdir(current_dir + "/logs")
def __enter__(self):
if self._write_to_file:
self.file = open(self.output_path, "a+") # create and append
self.file.write("{}: Logger initialized\n".format(self._get_time()))
return self
def __exit__(self, type, value, traceback):
if self._write_to_file:
self.file.write("{}: Logger closing\n\n\n".format(self._get_time()))
self.file.close()
def write(self, message):
if self.print_to_stdout: print(message)
if self._write_to_file:
self.file.write("{}: {}\n".format(self._get_time(), message))
def _get_time(self):
return datetime.now().strftime("%Y-%m-%d-%H:%M:%S")