]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/logger.py
4868fdab056107ab456231d37da21d25516fb6dc
7 logger
= logging
.getLogger("nitsi.logger")
9 class Logger_Exeception(BaseException
):
12 # This function should create the necessary folders
13 # and touch the logging files
14 def init_logging(path
):
15 logger
.debug("Init logging directory")
16 if not os
.path
.isdir(path
):
17 logger
.error("{} is not a valid directory".format(path
))
20 path
= os
.path
.abspath(path
)
21 except BaseException
as e
:
22 logger
.error("Failed to get the absolute path for: {}".format(path
))
24 log_dir
= "{}/log".format(path
)
26 if not os
.path
.exists(log_dir
):
29 time_dir
= log_dir
+ "/" + time
.strftime("%Y-%m-%d_%H-%M-%S" ,time
.gmtime(time
.time()))
31 if os
.path
.exists(time_dir
):
32 logger
.error("Path {} alreday exist".format(time_dir
))
33 raise Logger_Exeception
40 class TestFormatter(logging
.Formatter
):
41 def __init__(self
, start_time
=None, name
=None, longest_machine_name
=10):
42 super().__init
__(fmt
="[%(asctime)s] %(message)s")
43 logger
.debug("Initiating TestFormatter for")
44 if start_time
== None:
45 self
.starttime
= time
.time()
47 self
.starttime
= start_time
54 self
.longest_machine_name
= longest_machine_name
56 def converter(self
, recordtime
):
58 # This returns a timestamp relatively to the time when we started
61 recordtime
-= self
.starttime
63 return time
.gmtime(recordtime
)
65 def format(self
, record
):
66 return "[{}][{:^{align}}] {}".format(self
.formatTime(record
),
69 align
=self
.longest_machine_name
)
71 def formatTime(self
, record
, datefmt
=None):
72 ct
= self
.converter(record
.created
)
73 t
= time
.strftime("%H:%M:%S", ct
)
74 s
= "{}.{:03d}".format(t
, round(record
.msecs
,None))
78 class Log_Formatter(logging
.Formatter
):
80 super().__init
__(fmt
="[%(asctime)s] [%(levelname)s] %(message)s")
81 logger
.debug("Initiating Log_Formatter")
83 def format(self
, record
):
84 # We use 8 to align the levelname, because CRITICAL ist the longest name and has 8 chars
85 return "[{}][{:^}][{:^8}] {}".format(self
.formatTime(record
),
90 def formatTime(self
, record
, datefmt
=None):
91 t
= time
.strftime("%H:%M:%S", time
.gmtime(record
.created
))
92 s
= "{}.{:03d}".format(t
, round(record
.msecs
,None))