taggedLog 1.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: taggedLog
3
+ Version: 1.0.1
4
+ Summary: structured log for python
5
+ Author: epsilonkn
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/epsilonkn/Log-Manager/tree/main
8
+ Project-URL: Issues, https://github.com/epsilonkn/Log-Manager/issues
9
+ Requires-Python: >=3.12
10
+ Description-Content-Type: text/markdown
@@ -0,0 +1,12 @@
1
+ """
2
+ This module allow the user to create a structured log file for its python projects.
3
+ The module is used this way :
4
+
5
+ Log.start_log(...)
6
+ Log.info(...)
7
+ Log.warning(...)
8
+ Log.error(...)
9
+ Log.close_log()
10
+
11
+ read the docstrings of each method for further explainations
12
+ """
@@ -0,0 +1,5 @@
1
+ import sys
2
+ from importlib.metadata import version
3
+
4
+ if "--version" in sys.argv:
5
+ print("log ", version("tag-logger"))
@@ -0,0 +1,180 @@
1
+ import os
2
+ import time
3
+
4
+
5
+ class LogNotInstanciatedError(BaseException):
6
+ pass
7
+
8
+
9
+ class Log:
10
+
11
+ __version__ = 1.0
12
+
13
+ _instance = None
14
+
15
+
16
+ def __init__(self, cwd_path : str = "./", log_name : str = "module.log"):
17
+ """
18
+ Constructeur de la classe log, n'est pas fait pour être lancé directement.
19
+ Pour ouvrir le log, utilisez la méthode "start_log"
20
+
21
+ Args:
22
+ cwd_path (str, optional): chemin vers le dossier devant contenir le log. Defaults to "./".
23
+ log_name (str, optional): Nom du fichier de Logs. Defaults to "module.log".
24
+ """
25
+
26
+ self.path = cwd_path
27
+ self.opened = False
28
+ if log_name in os.listdir(cwd_path) :
29
+ self.f = os.path.join(cwd_path, log_name)
30
+ else :
31
+ f = open(os.path.join(cwd_path, log_name), 'w')
32
+ f.close()
33
+ self.f = os.path.join(cwd_path, log_name)
34
+
35
+
36
+ @classmethod
37
+ def start_log(cls, cwd : str = "./", log_name : str = "module.log", module_version : float = -1):
38
+ """
39
+ méthode d'ouverture du log
40
+
41
+ Args:
42
+ cwd (str, optional): chemin vers le dossier devant contenir le log. Defaults to "./".
43
+ log_name (str, optional): Nom du fichier de Logs. Defaults to "module.log".
44
+ module_version (float, optional): Version du module appelant le Log. Defaults to -1.
45
+ """
46
+ if not cls._instance :
47
+ cls._instance = Log(cwd, log_name)
48
+ if not cls._instance.opened :
49
+ init = "\n<login>"
50
+ detail = f"\n\t<init>\
51
+ \n\t\t<timestamp>{time.asctime()}</timestamp>\
52
+ \n\t\t<version>log version :{cls.__version__}, module version : {module_version}</version>\
53
+ \n\t</init>"
54
+ init += detail
55
+ cls._write_in_log(init)
56
+
57
+
58
+
59
+ @classmethod
60
+ def close_log(cls):
61
+ """
62
+ Ferme le log à l'appel du module l'ayant ouvert
63
+
64
+ Raises:
65
+ LogNotInstanciatedError: Erreur renvoyée si le Log n'a pas été ouvert avant fermeture
66
+ """
67
+ if not cls._instance :
68
+ raise LogNotInstanciatedError("Le log n'a pas été ouvert, fermeture impossible")
69
+ else :
70
+ cls._write_in_log("\n</login>")
71
+
72
+
73
+ @classmethod
74
+ def _write_in_log(cls, message : str):
75
+ """
76
+ Méthode d'écriture dans le log,
77
+ appelée uniquement par les méthodes d'ajout d'une entrée
78
+
79
+ Args:
80
+ message (str): message à écrire dans le log
81
+ """
82
+ with open(cls._instance.f, 'a', encoding='utf-8') as f :
83
+ f.writelines(message)
84
+
85
+
86
+ @classmethod
87
+ def error(cls,
88
+ error : BaseException,
89
+ addon : str = "",
90
+ timestamp : bool = True,
91
+ module_name : str = "unknown",
92
+ function : str = "unknown",
93
+ caller : str = "module"
94
+ ) -> None:
95
+ """
96
+ Ecrit une erreur dans le log
97
+
98
+ Args:
99
+ error (BaseException): erreur renvoyée par le module appelant
100
+ addon (str, optional): message complémentaire précisant l'erreur. Defaults to "".
101
+ timestamp (bool, optional): horodatage de l'entrée. Defaults to True.
102
+ module_name (str, optional): nom du module appelant la méthode. Defaults to "unknown".
103
+ function (str, optional): nom de la fonction appelant la méthode. Defaults to "unknown".
104
+ caller (str, optional): nom de l'entité appelant la méthode. Defaults to "module".
105
+
106
+ Example:
107
+
108
+ erreur envoyée par la fonction main d'un fichier script.py :
109
+ error(Exception("example"), "this is an add-on", module_name="script.py", function="main", caller="module")
110
+
111
+ erreur envoyée par le developpeur du module script.py dans la fonction main :
112
+ error(Exception("example"), "this action is prohibited", module_name="script.py", function="main", caller="dev")
113
+
114
+
115
+ """
116
+ message = "\n\t<error>"
117
+ message += f"\n\t\t<timestamp>{time.asctime()}</timestamp>" if timestamp else ""
118
+ message += f"\n\t\t<module>{module_name}</module><function>{function}</function><caller>{caller}</caller>"
119
+ message += f"\n\t\t<message>line {error.__traceback__.tb_lineno}, {error.__str__()}, {addon}</message>"
120
+ message += "\n\t</error>"
121
+
122
+ cls._write_in_log(message)
123
+
124
+
125
+ @classmethod
126
+ def info(cls,
127
+ *input : str,
128
+ timestamp : bool = True,
129
+ module_name : str = "unknown",
130
+ function : str = "unknown",
131
+ caller : str = "module",
132
+ tag : str | None = None
133
+ ) -> None:
134
+ """
135
+ Methode d'écriture d'une information ou plusieurs dans le log
136
+
137
+ Args:
138
+ *input (str): messages à écrire dans le log, il peut y en avoir plusieurs à la suite
139
+ timestamp (bool, optional): horodatage de l'entrée. Defaults to True.
140
+ module_name (str, optional): nom du module appelant la méthode. Defaults to "unknown".
141
+ function (str, optional): nom de la fonction appelant la méthode. Defaults to "unknown".
142
+ caller (str, optional): nom de l'entité appelant la méthode. Defaults to "module".
143
+ tag (str | None, optional): tag precisant le type de message, peut être "resultat", "operation", "reponse". Defaults to None.
144
+ """
145
+ addtag = f" tag={tag}" if tag else ""
146
+ message = f"\n\t<info{addtag}>"
147
+ message += f"\n\t\t<timestamp>{time.asctime()}</timestamp>" if timestamp else ""
148
+ message += f"\n\t\t<module>{module_name}</module><function>{function}</function><caller>{caller}</caller>"
149
+ message += "\n\t\t<message>"
150
+ for msg in input :
151
+ message += (str(msg) + "\n\t\t")
152
+ message += "</message>\n\t</info>"
153
+ cls._write_in_log(message)
154
+
155
+
156
+ @classmethod
157
+ def warning(cls,
158
+ warning_message : str = "",
159
+ timestamp : bool = True,
160
+ module_name : str = "unknown",
161
+ function : str = "unknown",
162
+ caller : str = "module"
163
+ ) -> None:
164
+ """
165
+ Méthode d'écriture d'un warning dans le log
166
+
167
+ Args:
168
+ warning_message (str, optional): Warning à écrire dans le log. Defaults to "".
169
+ timestamp (bool, optional): horodatage de l'entrée. Defaults to True.
170
+ module_name (str, optional): nom du module appelant la méthode. Defaults to "unknown".
171
+ function (str, optional): nom de la fonction appelant la méthode. Defaults to "unknown".
172
+ caller (str, optional): nom de l'entité appelant la méthode. Defaults to "module".
173
+ """
174
+ message = "\n\t<warning>"
175
+ message += f"\n\t\t<timestamp>{time.asctime()}</timestamp>" if timestamp else ""
176
+ message += f"\n\t\t<module>{module_name}</module><function>{function}</function><caller>{caller}</caller>"
177
+ message += f"\n\t\t<message>{warning_message}</message>"
178
+ message += "\n\t</warning>"
179
+
180
+ cls._write_in_log(message)
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "taggedLog"
3
+ version = "1.0.1"
4
+ description = "structured log for python"
5
+ dependencies = []
6
+ authors = [{ name="epsilonkn" }]
7
+ requires-python = ">=3.12"
8
+ readme = "README.md"
9
+ license = "MIT"
10
+ license-files = ["LICENSE"]
11
+
12
+ [build-system]
13
+ requires = ["setuptools>=61", "wheel"]
14
+ build-backend = "setuptools.build_meta"
15
+
16
+ [project.scripts]
17
+ taggedLog = "log.cli:main"
18
+
19
+ [project.urls]
20
+ Homepage = "https://github.com/epsilonkn/Log-Manager/tree/main"
21
+ Issues = "https://github.com/epsilonkn/Log-Manager/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: taggedLog
3
+ Version: 1.0.1
4
+ Summary: structured log for python
5
+ Author: epsilonkn
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/epsilonkn/Log-Manager/tree/main
8
+ Project-URL: Issues, https://github.com/epsilonkn/Log-Manager/issues
9
+ Requires-Python: >=3.12
10
+ Description-Content-Type: text/markdown
@@ -0,0 +1,9 @@
1
+ pyproject.toml
2
+ log/__init__.py
3
+ log/__main__.py
4
+ log/log.py
5
+ taggedLog.egg-info/PKG-INFO
6
+ taggedLog.egg-info/SOURCES.txt
7
+ taggedLog.egg-info/dependency_links.txt
8
+ taggedLog.egg-info/entry_points.txt
9
+ taggedLog.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ taggedLog = log.cli:main
@@ -0,0 +1 @@
1
+ log