Mess mess is everywhere... but not in yours user libraries!
In this post I would like to show you some aspects of logging in Java applications.
Many times during programming you are obliged to add some JAR's to your build path.
Everything is fine until you need to know API of the method/function from this JAR's.
Everything is fine until you need to know API of the method/function from this JAR's.
Let's create some example project in Eclipse
I gave the name for this project : TestLog4j , and I created a Test class with the main method in package:
com.selfimproveit.
I don't know what is your attitude to the clarity of your folders/documents etc, but I like to have everything under control. In my Eclipse Juno IDE I created inside home eclipse path a folder named "JARS" and I download there in to the separate folders all jars, libs. (You will see this on the end of this topic).
Now we must download some jars. There are few logging framework, the most popular is Log4j.
You can download it from this link.
There is possibility to download Log4j, directly from http://logging.apache.org/ but you will download just log4.jar and examples without sources and javadoc. (I will tell you later why sources and javadocs are so important to me).
You should download 3 following files (the version could be different, when I was wrote this post 1.2.17 version was the current version)
This is simple, from now we could start using Log4j but we need to be more flexible.
What I mean is, what will happen if we change the logging framework? Our code will be rewritten once again - this is sad situation, isn't?
We should have some "bridge" between our application and logging framework. Our "medicine" is SLF4J
Now we should download SLF4J. Now let's add some JAR's to our build path and finally start coding!
First of all add this JAR's to your build path:
- log4j-1.2.17.jar
- slf4j-api-1.7.2.jar
- slf4j-log4j12-1.7.2.jar
After that, we must create a log4j.properties file. Go to NEW--> OTHER--> Properties File
(If you don't have this feature in your Eclipse, you may download JBoss Tools plugins for your Eclipse).
Give a name to your file : log4j. Add the following code into log4j.properties file
Now our program example looks as follows:
STDOUT - it means show this on console
ERROR - error level is only displayed/logged
If we run this program, the error should be displayed to our console.
Now If you click with CTRL button on the caption "LoggerFactory" you should see an API of this class, but whoa!? What's going on? Probably you will see something like this:
ERROR - error level is only displayed/logged
If we run this program, the error should be displayed to our console.
Now If you click with CTRL button on the caption "LoggerFactory" you should see an API of this class, but whoa!? What's going on? Probably you will see something like this:
I hate this view, in next post I will show you how to fix this up, also I will show you more options with logging.
Brak komentarzy:
Prześlij komentarz