Professional Documents
Culture Documents
Log4J Tutorial: Donate Using Paypal
Log4J Tutorial: Donate Using Paypal
Who am I? My name is Veera. Im a Java developer. I have been developing web applications in Java for the past 5 years. Almost in all my projects, I have used Log4j for logging. I have shared my knowledge in my blog in the past years. Now I compiled all my articles into a single eBook so that you can easily get to know about Log4j. I hope youll find this work useful. If you like, You can also contribute by donating a small amount!
Introduction:
Log4j is a flexible logging library, an open source project from Apache. Using Log4j, we can replace the debugging print line statements, like System.out.println(Value is + someVariable), with a configurable logging statement like logger.debug(Value is + someVariable). The advantage in using Log4j is, if you do not want to print the debugging print lines in production application, you can easily switch them off using Log4j configuration file log4j.properties. Log4j is not just for logging into console. You can log to files, emails (SMTP server), a remote server or a database. You can easily switch between the logging destinations, switch off logs of certain levels or format the log message as per your requirement. The frameworks is such flexible.
No more System.out.println():
Lets make Log4j to write log messages to console. Yeah! you are going to replace the age-oldbullet-proof-debugging-method i.e. System.out.println(). Before you do that, you need to understand what goes in the log4j.properties file. You will be using this file mainly to configure the below things: Appender this is the class file which does the actual logging. It could be a simple Console appender which dumps the log messages to stdout (screen) or a file appender, which sends the log messages to a log file or JDBC appender. Layout is nothing but how the log message is formatted. This format is very similar to C language's printf function formatting. Logger a logger ties the appender with the log messages that are coming from the Java application. Basically, a logger tells that the log messages from these packages
should go to some appender which will log the message in the defined layout. That said, now open the log4j.properties file and type-in (remember, TYPE IN) the below code in it. #define the console appender log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender # now define the layout for the appender log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n # map console appender as a root logger,to log all messages to this appender log4j.rootLogger = DEBUG, consoleAppender The above configuration tells Log4j to log all messages to a console appender. When you run your program, you will see the log messages displayed on your console, just how your System.out.println() would do. To test this, create a Java file, put the below code and run it. public static void main(String args[]){ private static Logger logger = Logger.getLogger(MyclassName.class); logger.debug("Hello World"); }
What you just did was simple. Instead of using a console appender, we used a file appender. Theres a simple FileAppender is available to you. But here, youve used a advanced version of it, which creates a new log file everytime the file size reaches the mazimun size you set (which is 2 MB in our case). Cool huh!?
Bonus: Creating a new log file each time your programs runs:
If you want your program log messages to a new log file each time it runs, you need to create a
custom Log4j Appender. Heres a detailed article about this: How to create a new log file for each time the application runs?
Bonus: Logging DEBUG and INFO log level messages to separate files:
Do you want to write different log levels into different files? You should read this article to see how to do that. Writing different log levels in different log files.
- The End -
Who am I? My name is Veera. Im a Java developer. I have been developing web applications in Java for the past 5 years. Almost in all my projects, I have used Log4j for logging. I have shared my knowledge in my blog in the past years. Now I compiled all my articles into a single eBook so that you can easily get to know about Log4j. I hope youll find this work useful. If you like, You can also contribute by donating a small amount!