1. Apache Log4J
2. Java Logging (java.util.logging)
Logging is required to monitor the state of the running application, debug the application, ignorant developers heavily relies on System.out.println(), which is quite expensive in terms of resources and you dont have any way to manage these messages.
To control this we have different API's like mentioned above.
Using Log4J from Apache:
Log4j consists of 3 aspects: logger, appender and, layout relations between three can be expressed as "logger logs to an appender using a layout"
Each class in an application can have seperate logger or a common logger, now this logger should know where to send request for logging. This where are known as appenders which can be:
- FileAppender (flat file)
- JDBCAppender (database )
- Console Appender (Console)
- SMTPAppender (Email)
- JMSAppender (to remote JMS servers)
- SocketAppender (to remote server).
there are few more.
Now we need to decide what and how we want the out out of that log, this is decided by layout
Each class can have a logger as mentioned above similarly each logger should have log level. There are five different log levels (ordered):
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
To configure a class with a logger, appender, layout and log level we need to use an external configuration file (log4j.properties is the default)
Sample configuratiion file is as:
--------------------------------
# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=INFO, CONSOLE
# A1 is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# Change the level of messages for various packages.
log4j.logger.com.tj.cand.controller=WARN
log4j.logger.com.tj.cand.domain=WARN
log4j.logger.com.tj.cand.dao=DEBUG
--------------------------------
Usage:
Logger log = Logger.getLogger(MyController.class);
log.info("This is a logging message);
0 comments:
Post a Comment