Apache Log4j is the logging framework for java applications.
Using log4j, the developer can configure at run-time the verbosity of logs he needs without changing any log statement in code.
How it works?
The main objects/configs in the log4j framework that we work with are loggers, appenders, layouts, patterns,and log-levels.
|Logger||Loggers are objects with given names through which application makes logging calls. Every logger has one or more appender tied to it.|
|Appender||An appender is the destination where logs are recorded, for eg. console, file, db etc.|
|Layout||Alayout is tied to an appender and determines the format of logged messages for eg., plain text, xml format, html format etc.|
|Pattern||A pattern is tied to a layout and determines the fields that are logged in message besides the given message string, for eg. timestamp, class name, method name, line number etc.|
|Log Level||The only other thing that you need to know about log4j to get going is the concept of log-level. See, when you write logs in your app, you can specify the criticality or level of log, say for example ordinary log, important log or critical log . Now you can configure log4j to print logs whichare ordinary or a higher level. So, you can choose at run-time, the verbosity of logs.|
If you are not using Maven, you can download
log4j here: https://archive.apache.org/dist/logging/log4j/
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Artefacts needed to get started with log4j use. For using log4j, the only requirement is to have
log4j-<version>.jar in the classpath.
apache-log4j-extras-<version>.jar are required only for the examples “Rolling & Archiving File On Size”, “Rolling & Archiving File Per Day” and “Rolling & Archiving File Per Hour/Minute”. For all other examples, the extras jar is not required in the classpath.
<dependency> <groupId>log4j</groupId> <artifactId>apache-log4j-extras</artifactId> <version>1.0</version> </dependency>