Skip to content

Commit 53bca31

Browse files
committed
logging updates
1 parent ea7791c commit 53bca31

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

nids/logging.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
# nids/logging.py
22

33
import logging
4+
from logging.handlers import RotatingFileHandler
45
from scapy.all import IP, IPv6
56
import os
67

78
def setup_logging():
8-
# Clear the log file if it exists
99
log_file = 'nids_logs.log'
10-
if os.path.exists(log_file):
11-
with open(log_file, 'w'):
12-
pass
13-
10+
max_log_size = 5 * 1024 * 1024 # 5 MB
11+
backup_count = 3
12+
1413
# Setup logging configuration
15-
logging.basicConfig(filename=log_file, level=logging.INFO,
16-
format='%(asctime)s:%(levelname)s:%(message)s')
14+
handler = RotatingFileHandler(log_file, maxBytes=max_log_size, backupCount=backup_count)
15+
logging.basicConfig(level=logging.INFO,
16+
format='%(asctime)s:%(levelname)s:%(message)s',
17+
handlers=[handler])
1718

1819
def log_prediction(packet, prediction, original_data, traffic_type, src_ip, dst_ip):
1920
summary = packet.summary() if IP in packet or IPv6 in packet else "Non-IP packet"
2021
features_str = ', '.join([f'{k}: {v}' for k, v in original_data.items()])
2122
log_message = f'Packet: {summary}, Prediction: {prediction.item()} ({traffic_type}), Source IP: {src_ip}, Destination IP: {dst_ip}, Features: [{features_str}]'
22-
logging.info(log_message)
23+
logging.info(log_message)

requirements.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
pandas
22
scikit-learn
33
torch
4-
scapy
4+
scapy
5+
pickle
6+
time
7+
logging

0 commit comments

Comments
 (0)