使用日志文件记录数据报包的信息

时间:2014-12-21 11:04:48

标签: java logging

标题

嗨,我使用simpleFormatter使用Netbeans IDE在名为projet.formatter.log的日志文件中记录有关数据包的数据,但它显示的日志文件位于XMLFormat而不是以简单易读的格式。这是代码段

标题

private static final Logger LOGGER = Logger.getLogger(log_projet.class.getName());
public log_projet(DatagramPacket sock) throws IOException{


    Handler fileHandler = null;
    Formatter simpleFormatter = null;

    try{
        fileHandler = new FileHandler("./projet.formatter.log"); // prints the log at a target destination
        simpleFormatter = new SimpleFormatter();
        LOGGER.addHandler(fileHandler);
        LOGGER.log(Level.INFO, "The packet is adressed to {0}", sock.getSocketAddress());// returns the address and the port number of the host
        LOGGER.log(Level.INFO, "There are {0}" + " bytes of data ", sock.getLength());
        LOGGER.log(Level.INFO, "Time entred router{0}", temps_entree_paquet_dans_routeur());
        // add time transfer
        LOGGER.log(Level.INFO, "Time left router{0}", time_left_router());
        // source port and destination port
        //ip address of sender and receiver

        fileHandler.setFormatter(simpleFormatter); // formats the logrecords
        fileHandler.setLevel(Level.ALL);
    } catch(IOException ex){
        LOGGER.log(Level.SEVERE,"Error occured in file handler.",ex);
    }
}

1 个答案:

答案 0 :(得分:0)

在将FileHandler附加到记录器并开始写入日志消息之前设置格式化程序。

    private static final Logger LOGGER = Logger.getLogger(log_projet.class.getName());
    public log_projet(DatagramPacket sock) throws IOException{


    Handler fileHandler = null;
    Formatter simpleFormatter = null;

    try{
        fileHandler = new FileHandler("./projet.formatter.log");
        simpleFormatter = new SimpleFormatter();
        fileHandler.setFormatter(simpleFormatter); // formats the logrecords
        fileHandler.setLevel(Level.ALL);

        LOGGER.addHandler(fileHandler);
        LOGGER.log(Level.INFO, "The packet is adressed to {0}", sock.getSocketAddress());// returns the address and the port number of the host
        LOGGER.log(Level.INFO, "There are {0}" + " bytes of data ", sock.getLength());
        LOGGER.log(Level.INFO, "Time entred router{0}", temps_entree_paquet_dans_routeur());
        // add time transfer
        LOGGER.log(Level.INFO, "Time left router{0}", time_left_router());
        // source port and destination port
        //ip address of sender and receiver
    } catch(IOException ex){
        LOGGER.log(Level.SEVERE,"Error occured in file handler.",ex);
    }
}