Spaces:
Sleeping
Sleeping
Commit
·
c42cb7b
1
Parent(s):
d3d8436
Add multiple datetime template / Add present year if needed
Browse files- config/log_definitions.py +20 -0
- utils/log2pandas.py +32 -8
config/log_definitions.py
CHANGED
|
@@ -58,4 +58,24 @@ log_definitions = {
|
|
| 58 |
{"name": "record_type", "pos": 4, "type": str},
|
| 59 |
],
|
| 60 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
|
|
|
| 58 |
{"name": "record_type", "pos": 4, "type": str},
|
| 59 |
],
|
| 60 |
},
|
| 61 |
+
"linux_log": {
|
| 62 |
+
"sep": " ",
|
| 63 |
+
"fields": [
|
| 64 |
+
{"name": "datetime", "pos": slice(0, 3), "type": "datetime"},
|
| 65 |
+
{"name": "level", "pos": 3, "type": str},
|
| 66 |
+
{"name": "component", "pos": 4, "type": str},
|
| 67 |
+
{"name": "pid", "pos": 5, "type": str},
|
| 68 |
+
{"name": "Content", "pos": slice(6, None), "type": str},
|
| 69 |
+
],
|
| 70 |
+
},
|
| 71 |
+
"ssh_log": {
|
| 72 |
+
"sep": " ",
|
| 73 |
+
"fields": [
|
| 74 |
+
{"name": "datetime", "pos": slice(0, 3), "type": "datetime"},
|
| 75 |
+
{"name": "level", "pos": 3, "type": str},
|
| 76 |
+
{"name": "component", "pos": 4, "type": str},
|
| 77 |
+
{"name": "pid", "pos": 5, "type": str},
|
| 78 |
+
{"name": "Content", "pos": slice(6, None), "type": str},
|
| 79 |
+
],
|
| 80 |
+
},
|
| 81 |
}
|
utils/log2pandas.py
CHANGED
|
@@ -18,6 +18,8 @@ class LogParser:
|
|
| 18 |
|
| 19 |
def parse_line(self, line):
|
| 20 |
"""Parse une ligne du fichier log en utilisant la définition fournie."""
|
|
|
|
|
|
|
| 21 |
tokens = line.strip().split()
|
| 22 |
# On ignore la ligne si elle ne contient pas assez de tokens
|
| 23 |
if len(tokens) < len(self.log_definition["fields"]):
|
|
@@ -36,18 +38,40 @@ class LogParser:
|
|
| 36 |
except IndexError:
|
| 37 |
value = None
|
| 38 |
|
| 39 |
-
# Nettoyage des caractères qui entourent la valeur (crochets, parenthèses, etc.)
|
| 40 |
-
if value:
|
| 41 |
-
value = value.strip("[](){}<>")
|
| 42 |
-
|
| 43 |
# Conversion du type
|
| 44 |
if "type" in field:
|
| 45 |
typ = field["type"]
|
| 46 |
if typ == "datetime":
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
value = None
|
| 52 |
elif typ == "direction":
|
| 53 |
value = "download" if value == "o" else "upload"
|
|
|
|
| 18 |
|
| 19 |
def parse_line(self, line):
|
| 20 |
"""Parse une ligne du fichier log en utilisant la définition fournie."""
|
| 21 |
+
# Commencer par remplacer les [ et ] par des espaces
|
| 22 |
+
line = line.replace("[", " ").replace("]", " ")
|
| 23 |
tokens = line.strip().split()
|
| 24 |
# On ignore la ligne si elle ne contient pas assez de tokens
|
| 25 |
if len(tokens) < len(self.log_definition["fields"]):
|
|
|
|
| 38 |
except IndexError:
|
| 39 |
value = None
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Conversion du type
|
| 42 |
if "type" in field:
|
| 43 |
typ = field["type"]
|
| 44 |
if typ == "datetime":
|
| 45 |
+
formats = [
|
| 46 |
+
"%a %b %d %H:%M:%S %Y", # Format typique
|
| 47 |
+
"%Y-%m-%d %H:%M:%S", # ISO-like format
|
| 48 |
+
"%d/%m/%Y %H:%M:%S", # European format
|
| 49 |
+
"%m/%d/%Y %H:%M:%S", # US format
|
| 50 |
+
"%Y%m%d%H%M%S", # Compact format
|
| 51 |
+
"%Y-%m-%dT%H:%M:%S", # ISO format
|
| 52 |
+
"%Y-%m-%dT%H:%M:%S.%f", # ISO with microseconds
|
| 53 |
+
"%b %d %H:%M:%S", # Jun 14 15:16:01
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
for date_format in formats:
|
| 57 |
+
try:
|
| 58 |
+
# Si l'année n'est pas présente dans le format,
|
| 59 |
+
# on l'ajoute en utilisant l'année actuelle
|
| 60 |
+
if "%Y" not in date_format:
|
| 61 |
+
# Add current year to the date string
|
| 62 |
+
current_year = datetime.now().year
|
| 63 |
+
value_with_year = f"{value} {current_year}"
|
| 64 |
+
# Add year to format string
|
| 65 |
+
format_with_year = f"{date_format} %Y"
|
| 66 |
+
value = datetime.strptime(
|
| 67 |
+
value_with_year, format_with_year
|
| 68 |
+
)
|
| 69 |
+
else:
|
| 70 |
+
value = datetime.strptime(value, date_format)
|
| 71 |
+
break
|
| 72 |
+
except ValueError:
|
| 73 |
+
continue
|
| 74 |
+
else: # No formats matched
|
| 75 |
value = None
|
| 76 |
elif typ == "direction":
|
| 77 |
value = "download" if value == "o" else "upload"
|