Advertisement
Guest User

Untitled

a guest
May 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package sockets;
  2. import java.io.*;
  3. import java.time.format.DateTimeFormatter;
  4. import java.time.LocalDateTime;
  5.  
  6.  
  7. public class log {
  8.  
  9.  
  10.     public void EscribirLog(String tipo, String IP, String comando){//tipo: {conexion, error, comando, response} IP: puede ser del servidor como del cliente, comando: ls, get, put, delete
  11.  
  12.  
  13.         try {
  14.                 BufferedWriter log = new BufferedWriter(new FileWriter("log.txt", true));
  15.                 DateTimeFormatter formato = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
  16.                 LocalDateTime hora_actual = LocalDateTime.now();
  17.                 String hora_escribir = formato.format(hora_actual);
  18.  
  19.                 if (tipo.equals("conexion"))
  20.                 {
  21.                     String linea = hora_escribir + tipo + IP + "conexion entrante\n";
  22.                     log.write(linea);
  23.                     log.close();
  24.                 }
  25.  
  26.                 else if (tipo.equals("comando"))
  27.                 {
  28.                     String linea = hora_escribir + tipo + IP + comando+"\n";
  29.                     log.write(linea);
  30.                     log.close();
  31.                 }
  32.  
  33.                 else if (tipo.equals("error"))
  34.                 {
  35.                     String linea = hora_escribir + tipo + "conexion rechazada por " + IP + "\n";
  36.                     log.write(linea);
  37.                     log.close();
  38.                 }
  39.  
  40.                 else if (tipo.equals("response"))
  41.                 {
  42.                     String linea = hora_escribir + tipo + "servidor envía respuesta a " + IP + "\n";
  43.                     log.write(linea);
  44.                     log.close();
  45.                 }
  46.  
  47.  
  48.  
  49.             } catch (IOException e) {
  50.                 System.out.println("Ocurrió un error, es culpa del paro.");
  51.                 e.printStackTrace();
  52.             }
  53.         }
  54.  
  55.  
  56.  
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement