Prev Next

Java / log4j

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. Difference between apache commons logging and log4j.

Apache commons logging is a facade API that abstracts the underlying implementation. Using commons logging, one can decide between different implementations such as log4j, slf4j and java util Logging without touching the code.

log4j is a logging framework that provides the actual implementation.

2. Can log4j logging be stored to database?

Yes. The logging can be written to a database, file, console, JMS and Apache flume.

3. What is log4j?

log4j framework is a reliable, fast and light weigtht logging API. It is written using Java and distributed under Apache software license.

It is highly configurable using external configuration files at runtime. It supports log direct to various destinations including databases, file, console, Unix syslog and many more.

4. Components of log4j framework.

There are mainly 3 components.

Logger captures logging information.

Appender publishes logging information to various destinations.

Layout formats the logging data in different configured styles to support readability.

5. Advantages of using log4j framework.
  • Thread safe.
  • Optimized for performance.
  • Supports more than one appender per logger.
  • Extensible.
  • Logging behavior is specified in a configuration file loaded during runtime.
  • Supports different logging levels such as WARN, INFO, and ERROR.
  • The logging format can be easily changed my altering the Layout class or by extending it.
  • The destination and the log writing strategy can be altered by implementing Appender interface.
6. What are the different log levels in Log4j?

There are 8 levels. You can also define your custom levels by sub-classing the Level class.

  • ALL,
  • DEBUG,
  • INFO,
  • WARN,
  • ERROR,
  • FATAL,
  • TRACE,
  • and OFF.
7. What is Appender in Log4j?

Appender is used to deliver LogEvents to its destination. For example, it is used to write the logs in the file.

8. Explain different logging levels in log4j.

The DEBUG level designates fine-grained informational events useful to debug an application.

The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.

The WARN level designates potentially harmful situations.

The ERROR level designates error events that might still allow the application to continue running.

The TRACE Level designates finer-grained informational events than the DEBUG.

FATAL level designates very severe error events that will presumably lead the application to abort.

The ALL Level has the lowest possible rank and is intended to turn on all logging.

The OFF Level has the highest possible rank and is intended to turn off logging.

9. Explain few format characters used in log4j.

The format characters used in log4j are,

L- it is used to output the line number from where the logging request was processed or issued.

m- It is used to output the application supplied message related to the logging event.

p- It is used to output the priority of the logging event.

C- It is used to output the class name of the caller issuing the logging request.

10. Is log4j thread safe?

Yes, Log4j is a thread-safe; its components are built to be used in multithreaded systems.

11. How can you log into the database using Log4j?

The log4j API provides the object org.apache.log4j.jdbc. JDBCAppender object that can put logging information in a database.

12. Different types of Appenders in Log4j.

There are many types of appenders that include,

  • ConsoleAppender logs to standard output,
  • FileAppender writes logs to some file,
  • Rolling file appender appends to a file with a maximum size.
13. Explain layouts in log4j.

An appender uses Layout to format the log before writing to the destination. The layout allows formatting logging information in various patterns and styles.

14. How does log Levels work in Log4J?

A log request of level n in a logger with level m is enabled if m >= n.

For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

15. Explain immediateFlush property of FileAppender.

ImmediateFlush property enables the output stream to the file being flushed with each append operation.

By default the property is set to true.

To switch off immediateFlush use the below configuration. When set to false, it may improve performance.

log4j.appender.FILE.ImmediateFlush=false
16. Why should we prefer log4j framework while Java provides logging API?

Log4j has following advantages over standard logging API:

  • robust logging.
  • log4j has more features available than standard logging API.
  • configuring and using log4j is easier.
  • log4j also has a much more robust log formatting.
  • more handlers are available for log4j.
17. What is SLF4J?

The Simple Logging Facade for Java (SLF4J) is a simple facade or abstraction for various logging frameworks such as java.util.logging, LogBack, log4j allowing the end user to plug in the desired logging framework at deployment time. It is not itself a logging library, but a generic interface to one of many logging libraries.

18. Mention larger logging domains for your applications.

Diagnostic logging are used to understand the state of the system at a particular point of interest.

Security logging establishes accountability and useful for forensics.

Audit logging is the digital evidence and used for auditing purposes.

«
»
Annotations

Comments & Discussions