Programmer
Programmer

Reputation: 137

Using of Log4J in a project

How I use log4j in my project and also guide me what is the purpose of log4j. please guide me with detail? I already read different articles but i can't understand what is log4j and what's purpose of use. Thanks in advance :)

Upvotes: 1

Views: 596

Answers (4)

lkamal
lkamal

Reputation: 3938

To understand the basics, you can consider the way someone prints the details of a program into the console to find out how a program is executing.

Beginners are generally using the System.out.println(String) for this. However that practice has a number of issues in managing the messages. Just to point out one; there is no way of control which messages are printed, so all System.out.prints are printed.

To manage the messages (also called logs) properly; a logging framework like log4j, logback or slf4j can be used.

As a starting point, you can add log4j into your Java project using 5 simple steps.

  • Set up the project with log4j
  • Create log4j.properties file
  • Write a class to record log messages
  • Compile and run program
  • Check the log messages

Each step is detailed in "How to integrate log4j with Java project" tutorial.

Upvotes: 0

FrVaBe
FrVaBe

Reputation: 49341

First of all you have to understand what Application Logging is and why you need it. Wikipedia does a good job here in one sentence:

Computer data logging is the process of recording events, with a computer program usually an application software in a certain scope in order to provide an audit trail that can be used to understand the activity of the system and to diagnose problems.

So the purpose is to have a look at certain events of your application (e.g. monitor that a special method was invoked). The recording of the event can be done on multiple ways. You can write a statement to the console window, you can write a statement to a file (very common), you can even email the statement to someone (in case of very specific events that needs to be handled fast).

Logging frameworks will help you to do this job. They provide the possibility to give logging statements a specific "level" of importants and to configure the target of your logging statements (console, file, email, whatever).

Once you understand why you need a logging framework - you have to choose one. In Java you have several possibilities, e.g.:

  • log4j (1) - is very common but not maintained any more
  • log4j 2 - the Apache successor of log4j - still in beta phase
  • slf4j with logback - the successor of log4j from the author of log4j (1)
  • Java Logging API - rarely used (as far as I know)

IMHO a good choice at the moment would be slf4j with logback. log4j 2 looks promising but is still in beta phase.

Upvotes: 2

paary
paary

Reputation: 429

With Log4j, you can log details from your application. Its akin to using System.out.println(). But Log4j does more than that. By using Log4j, you can actually set different levels for different information that you wish to log. apart from this, you can log the details to a separate file instead of the console. I would strongly recommend you to go through the documentation of Log4j as it is not that difficult to understand.

Upvotes: 2

Erik Pragt
Erik Pragt

Reputation: 14617

Log4j is a logging framework, it prints log lines in a very configurable way. It's meant to be used so that you can configure the level of logging (eg debug, info, warning, error) without the need to change your code, in contrast to, say, using System.out.println().

Why would you want to use it if you don't know what it is?

Upvotes: 2

Related Questions