How to work with Log in Java?

Asked

Viewed 7,956 times

2

I need to make a condition based on the Log of my Java project.

Ex:

Enquanto(TextoDoLog == "PalavraTal"){
    faça algo;
}

But I don’t know if I have to create some class, or method, how to handle it.

  • 1

    Your question is very open. What are you calling "Log" in this context? Where does it come from? What makes it change?

  • I’m working with Start, Clean(delete), Migrate and Inform the metadata of a bank... Log is the description of what is happening during execution Ex: Downloading... Executing... INFO.. Finished These things

  • Are you already using something to store the log steps? A variable or something like?

  • There you go. private Static final Logger logMigrations = Logger.getLogger(Commandndoinit.class.getName(); is what is being declared at the beginning of the class. But I don’t know if I can use it yet.

1 answer

1

Java has some frameworks of logging, being the Log4j the most famous of them.

Like any framework, a previous configuration is required for you to use it, but in the case of Log4j is very simple.


Once configured, you can "log" your application only with the method calls as below:

  • logger.error("Ocorreu um erro aqui");

  • logger.warn("Atenção!");

  • logger.info("Informação :P");

Among others...


With Log4j you can configure how the log will be stored: whether it will only be displayed on the server console, whether it will save a part to a file, etc.

Even, you can format how the log is stored, including the level (error, info, warn, debug and etc), date, time, the class and even the method where the log was created.


Here has a tutorial on how to configure and use Log4j.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.