Logs V3

2 minute read

1. Logs V3

Hello there,

This week I’ve taken a different approach to the log’s data management. First of all analyzing the new log files with the new information registered in it.

//LOGIN traze = “1 | “ + str(datetime.now().strftime(“%d/%m/%Y %H:%M:%S”)) + “ | “ + username + “ | “ + client_ip + “ | “ + user_agent + “\n”

// LOGOUT traze = “2 | “ + str(datetime.now().strftime(“%d/%m/%Y %H:%M:%S”)) + “ | “ + username + “ | “ + client_ip + “ | “ + user_agent + “\n”

// NEW SIMULATION traze = “3 | “ + str(datetime.now().strftime(“%d/%m/%Y %H:%M:%S”)) + “ | “ + user.username + “ | “ + client_ip + “ | “ + simulation_type + “ | “ + exercise_id + “ | “ + host.ip + “ | “ + container.id + “ | “ + user_agent + “\n”

// END SIMULATION - no yet in the current log files traze = “4 | “ + str(datetime.now().strftime(“%d/%m/%Y %H:%M:%S”)) + “ | “ + user.username + “ | “ + client_ip + “ | “ + simulation_type + “ | “ + exercise_id + “ | “ + host.ip + “ | “ + container.id + “ | “ + user_agent + “\n”

// ERROR traze = “5 | 500 Internal Server Error” + “\n”

NEW LOG FILENAME STRUCTURE “/logs/” + str(date.today()) + “-log.txt”

2. Creating a new DDBB Structure

On the models.py Django file I’ve created 2 new tables:

typeSymulation = { (‘new’, ‘New’), (‘end’, ‘End’) }

class Simulation(models.Model): type = models.CharField(max_length=40, choices=typeSymulation) date = models.DateTimeField() username = models.CharField(max_length=200) client_ip = models.CharField(max_length=200) simulation_type = models.CharField(max_length=200) exercise_id = models.CharField(max_length=200) host_ip = models.CharField(max_length=200) container_id = models.CharField(max_length=200) user_agent = models.CharField(max_length=200)

typeSession = { (‘new’, ‘New’), (‘end’, ‘End’) } class Session(models.Model): type = models.CharField(max_length=40, choices=typeSymulation) date = models.DateTimeField() username = models.CharField(max_length=200) client_ip = models.CharField(max_length=200) user_agent = models.CharField(max_length=200)

And executed the following commands to make the changes effective:

  • By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration
    $ python3 manage.py makemigrations prototypePolls

  • The sqlmigrate command takes migration names and returns their SQL
    $ python3 manage.py sqlmigrate prototypePolls 0001

  • Now, run migrate again to create those model tables in your database:
    $ python3 manage.py migrate

3. HTML Django View

After that I’ve created a simple Django view to dynamically load data to the DDBB and to see it as plain text.