How do I make a log file in which I save every event that I want from my system?

2

What I have is the following:

Logger logger = Logger.getLogger("MyLog");  
FileHandler fh;  

try {  
    fh = new FileHandler(file_log);  
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();  
    fh.setFormatter(formatter);  

    logger.info("Primera prueba");  
    logger.info("¿Todo bien?");

} catch (SecurityException | IOException ex) {  
    ex.printStackTrace();  
}

The problem with that is that it eliminates the above. I would like to add events and not replace the previous ones.

    
asked by Pablo Contreras 28.08.2017 в 20:18
source

2 answers

2

For the FileHandler object there is a constructor that receives a second parameter of type boolean . If this argument is True , everything you indicate will be added to the file without overwriting what is already inside it.

fh = new FileHandler(file_log, true);  
    
answered by 28.08.2017 / 22:10
source
2

A cleaner, simpler alternative that is already proven by many projects and multiple uses is to use libraries that provide logging functionality, without the need to use native Java logging (which frankly leaves much to be desired).

Some of the most popular libraries for Java are log4j2 and logback . These bookstores have, at least:

  • Set the application's log level.
  • Set the level of log to apply to a package or packages. This may be different from the log level of the application. For example, your application in general may have an INFO log level but the Spring libraries (to name a few) may have a WARN or ERROR log level, so your log is not filled with unnecessary content for review.
  • Log output strategies. For example: write in console, in file, in database, send the log for a service, etc.
  • Output format strategies for log messages. For example: [fecha y hora] [nivel de log] [nombre de clase] [nombre de hilo] <mensaje> [salto de línea] . It allows you to customize this output and make it homogeneous for all log messages.
  • Initialization of the logger is relatively simple. Just have a code similar to this:

    public class MiClase {
        private static final Logger logger = Logger.getLogger(MiClase.class);
    }
    
  • You can configure strategies to store the log. For example, RollingFileAppender allows a daily log file to be created with the base name of the log (eg miapp), the log date (eg 2017-08-28) and the log number of the day (eg 1, 2 , etc). Examples: miapp-2017-08-28.log, miapp-2017-08-28-1.log, miapp-2017-08-28-2.log, and so on (this in case the application has been rebooted and rerun the same day, do not delete the previous log or write it in. All this is configurable, according to your needs).

It should be noted that there is a framework called SLF4J . Its full name is Simple Logging Facade for Java. Eye, the name indicates that this framework is not for logging, but is a facade of logging . That is, this framework does not do the work of logging, but is an intermediate layer between your application and a logging framework. What is this for? Well, it is a way that your application can support the change of use of log framework according to your needs. For example, today you may be delegating the log to JUL (Java Util Logger, native Java logging) and then decide to change to one of the previously mentioned frameworks. SLF4J allows you, after changing a few jars, that the change is transparent at the level of your application and code. This offers great advantages since your code is abstracted from the framework as such and you just tie yourself to the facade. The facade can be associated with any other framework, as long as they implement the bridge that allows such integration.

    
answered by 29.08.2017 в 05:34