-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathc_logging.py
78 lines (63 loc) · 2.25 KB
/
c_logging.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
'''
Copyright 2025 philippoo66
Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import time
import utils
class cLogging:
def __init__(self, log_file="app.log"):
"""
Initialisiert die cLogging-Klasse mit einem Logdateinamen.
"""
self.log_file = log_file
self.log_handle = None
def open_log(self):
"""
Öffnet die Logdatei im Anhängemodus.
"""
try:
self.log_handle = open(self.log_file, 'a')
self.do_log([], "Log file opened.")
except Exception as e:
print(f"Error opening log file: {e}")
def close_log(self):
"""
Schließt die Logdatei.
"""
if self.log_handle:
self.do_log([], "Log file closed.")
self.log_handle.close()
self.log_handle = None
def do_log(self, data, pre):
"""
Schreibt einen Logeintrag in die Logdatei mit Zeitstempel.
"""
if self.log_handle:
# timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# log_entry = f"[{timestamp}] {message}\n"
# self.log_handle.write(log_entry)
sd = utils.bbbstr(data)
self.log_handle.write(f"{pre}\t{int(time.time()*1000)}\t{sd}\n")
# else:
# print("Log file is not open. Cannot write log entry.")
def __del__(self):
"""
Sicherstellen, dass die Logdatei geschlossen wird, wenn die Instanz zerstört wird.
"""
self.close_log()
# for global use
vitolog = cLogging('vitolog.txt')
# Beispielverwendung
if __name__ == "__main__":
logger = cLogging("my_app.log")
logger.open_log()
logger.do_log([], "This is a test log entry.")
logger.close_log()