Home java logging in java

logging in java




I met the next quote on the Internet:

logging should be competent. For System.out.PrintLN For the output of logs, it is necessary to cut hands after a week of training.

  • Tell me how in Java it is properly issued logging in offline applications, on Tomcat and in other cases (as far as I know different commands of records in the log).
  • How to choose priorities of messages? Is there any emptlasal agreement in what reports to which priority to install?
  • It is worth it with a priority below it stands in the settings, print in the log just all the actions that occur (i.e., the password has been checking, such a menu item is selected, the creation of a new copy of such a class has been successful and t. P. – i.e. in other words, the whole thing for the need to search for errors)?

Answer 1, Authority 100%

  • “The entry commands in the log” depends not from the application, but from the library used for logging.
  • Logging levels (priorities of messages) are selected based on common sense. In general, the names of the levels of login levels are quite speaking. For example, at the Error level – writing errors, due to which the further work of the program is impossible, and at the TRACE level – we write detailed debug information.
  • stands. One thing is the application in the production, and then excess logging is not necessary, and it is quite another matter development and testing. The difference between the development and production of the environment will consist in the logo configuration.

These are not the only questions you need to pay attention to. Inevitably pop up issues of compatibility of loggers used in different libraries, as well as productivity problems (not free of charge).

Answer 2, AUTHORITY 78%

Industrial de facto logging in Java is log4j . All other logging systems are from evil, including java.util.logging , Apache Commons-Loggins and so on. New-fashioned LogBack (by the way, the latter is still compatible with log4j)

Almost all systems support logging in Log4J style (I mean real Java systems, not Android, which is not quite java). If they do not support log4j – then it is most likely the wrong systems 🙂

In the normal application, the log management is usually made in the external configuration file log4j.properties , so usually control logs: namely, what to output and where to display comes down to shamanist with the log4j.properties . In not the most difficult version, it may look like this:

# root loogger option
log4j.rootlogger = info, File, stdout
# Direct Log Messages To A Log File
log4j.appender.file = org.apache.log4j.rollingfileAptender.
log4j.appender.file.file = C: \\ Loging.log
log4j.appender.file.maxFilesize = 1MB.
log4j.appender.file.maxBackupindex = 1.
log4j.appender.file.Layout = Org.apache.log4j.patternlayout
log4j.appender.file.layout.conversionpattern =% d {yyyy-mm-dd hh: mm: ss}% -5p% C {1}:% L -% m% n
# Direct Log Messages to Stdout
log4j.appender.stdout = org.apache.log4j.consoleAptender.
log4j.appender.stdout.target = System.out.
log4j.appender.stdout.Layout = org.apache.log4j.patternlayout
log4j.appender.stdout.Layout.conversionPattern =% d {yyyy-mm-dd hh: mm: ss}% -5p% C {1}:% L -% m% n

Regarding the question about the levels of logging. That unlawful arrangements are:

  • Level Info – Just informing about a certain event
  • Level Debug – Used when debugging
  • Level Warn – an error message or nonstandard situation that is potentially dangerous
  • Level Error – Error message after which the program is still possible
  • Fatal – error message, after which the normal operation of the program is impossible. Usually, after this, the operation of the program stops.


1) As already mentioned in the comments – I apparently slightly got excited (slightly). Nevertheless prom. Standard is logging in Log4J style – this doubt is not subject to. Just need to remember that the log4j has several Forms: one old good classic log4j, the second log4j v.2, then there are new-fashioned LogBack and SLF4J.

2) The question often arises (rather, not even a question, and Holivar): how much logging affects performance. I personally decide this question for myself as:

Somewhere I declare a constant (let’s say in the class MyMain ):

Public Final Static Boolean Debug = True; // False when leaving for production

and then in places where debug logging is being published:

if (mymain.debug)
  Logger.debug (...);

When you exit the product Debug We set False and that’s it. The compiler itself will already remove from the code of the logs wrapped if (Debug) – all satisfied.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions