Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, July 11, 2011

Setting up log4j in your Java Project

For those that are wondering about the need for a logging utility in their project when there are already debuggers, you might want to read the following passage from the book "The Practice of Programming": 
As personal choice, we tend not to use debuggers beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost in details of complicated data structures and control flow; we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places. Clicking over statements takes longer than scanning the output of judiciously-placed displays. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugging sessions are transient.
Log4j is a Java-based logging utility and is a project of the Apache Software Foundation.
To add log4j to your project, first you will have to download it to your lib folder in your project. Make sure to add it to your classpath. If you are using Eclipse, just right click on the jar file in the lib directory, and go to Build Path -> Add to Build Path.
This tutorial is going to cover configuring log4j via a properties file called "log4j.properties". Configuring can also be done with a different technique using XML. 
It is considered a good coding practice to include your log4j.properties file in a "config" directory. 
Another library that we will be using for the purpose of this tutorial is the DatedFileAppender library. 

Friday, September 24, 2010

Java Notes - Access Control and Declarations

Starting with this post, I will be posting notes regarding certain topics about the Java Programming Language. I  am going back over the topics and I think it is a good idea to keep notes of the most important concepts that a Java programmer must master.

The first topic on which I am posting notes is "Access Control and Declarations".
So let us get started.


Source File Declaration


First, we start with the rules for a Java source file: 
  • There can be only one public class per source code file.
  • Comments can appear at the beginning or end of any line in the source code file
  • If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java.
  • If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
  • If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.
  • Import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.
  • A file can have more than one nonpublic class.