Java Language log4j / log4j2

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Apache Log4j is a Java-based logging utility, it is one of several Java logging frameworks. This topic is to show how to setup and configure Log4j in Java with detailed examples on all of its possible aspects of usage.

Syntax

  • Logger.debug("text to log"); // Logging debugging info
  • Logger.info("text to log"); // Logging common info
  • Logger.error("text to log"); // Logging error info
  • Logger.warn("text to log"); // Logging warnings
  • Logger.trace("text to log"); // Logging trace info
  • Logger.fatal("text to log"); // Logging fatal errors
  • Log4j2 usage with parameter logging:
  • Logger.debug("Debug params {} {} {}", param1, param2, param3); // Logging debug with parameters
  • Logger.info("Info params {} {} {}", param1, param2, param3); // Logging info with parameters
  • Logger.error("Error params {} {} {}", param1, param2, param3); // Logging error with parameters
  • Logger.warn("Warn params {} {} {}", param1, param2, param3); // Logging warnings with parameters
  • Logger.trace("Trace params {} {} {}", param1, param2, param3); // Logging trace with parameters
  • Logger.fatal("Fatal params {} {} {}", param1, param2, param3); // Logging fatal with parameters
  • Logger.error("Caught Exception: ", ex ); // Logging exception with message and stacktrace (will automatically be appended)

Remarks

End of Life for Log4j 1 reached

On August 5, 2015 the Logging Services Project Management Committee announced that Log4j 1.x had reached end of life. For complete text of the announcement please see the Apache Blog. Users of Log4j 1 are recommended to upgrade to Apache Log4j 2.

From: http://logging.apache.org/log4j/1.2/



Got any Java Language Question?