]>
git.ipfire.org Git - nitsi.git/blob - src/nitsi/logger.py
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")
18 path
= os
.path
.abspath(path
)
19 except BaseException
as e
:
20 logger
.error("Failed to get the absolute path for: {}".format(path
))
22 if os
.path
.isdir(path
):
23 log_dir
= "{}/log".format(path
)
24 elif os
.path
.isfile(path
):
25 log_dir
= "{}/{}.log".format(os
.path
.dirname(path
), os
.path
.basename(path
))
28 if not os
.path
.exists(log_dir
):
31 time_dir
= log_dir
+ "/" + time
.strftime("%Y-%m-%d_%H-%M-%S" ,time
.gmtime(time
.time()))
33 if os
.path
.exists(time_dir
):
34 logger
.error("Path {} alreday exist".format(time_dir
))
35 raise Logger_Exeception
42 class TestFormatter(logging
.Formatter
):
43 def __init__(self
, start_time
=None, name
=None, longest_machine_name
=10):
44 super().__init
__(fmt
="[%(asctime)s] %(message)s")
45 logger
.debug("Initiating TestFormatter for")
46 if start_time
== None:
47 self
.starttime
= time
.time()
49 self
.starttime
= start_time
56 self
.longest_machine_name
= longest_machine_name
58 def converter(self
, recordtime
):
60 # This returns a timestamp relatively to the time when we started
63 recordtime
-= self
.starttime
65 return time
.gmtime(recordtime
)
67 def format(self
, record
):
68 return "[{}][{:^{align}}] {}".format(self
.formatTime(record
),
71 align
=self
.longest_machine_name
)
73 def formatTime(self
, record
, datefmt
=None):
74 ct
= self
.converter(record
.created
)
75 t
= time
.strftime("%H:%M:%S", ct
)
76 s
= "{}.{:03d}".format(t
, round(record
.msecs
,None))
80 class Log_Formatter(logging
.Formatter
):
82 super().__init
__(fmt
="[%(asctime)s] [%(levelname)s] %(message)s")
83 logger
.debug("Initiating Log_Formatter")
85 def format(self
, record
):
86 # We use 8 to align the levelname, because CRITICAL ist the longest name and has 8 chars
87 return "[{}][{:^}][{:^8}] {}".format(self
.formatTime(record
),
92 def formatTime(self
, record
, datefmt
=None):
93 t
= time
.strftime("%H:%M:%S", time
.gmtime(record
.created
))
94 s
= "{}.{:03d}".format(t
, round(record
.msecs
,None))