How do we use LOGGER in java?

Java Object oriented programmi Published on 15 February 2020

Log means something informative in a continuous manner. In the application development world to find what happens at the server layer Log is used. Logging is a very important part of development and testing.

It helps to find out the errors at runtime. As we know while development all code is available which we can check by debugging but in case of a live environment it very difficult to judge the issues. 


Various logging example 


Database logging like in Mysql, Oracle, MongoDB, etc.

Server logging like in Tomcat, Apache, etc.

Development Logging in Java, Php, Python, Hit logs, etc.


Let talk about Logging in Java Application development. Java is very famous and widely used for enterprise-level applications like banking software memory management, clouds, and others. 


For development face we have System.out.Println() for the use of log we can write the important point and can see the output on the console. For the large scale project using printing, streaming is not convenient as well as maintenance is very difficult.

For overcoming this situation we use the framework name Log4j. It is very lightweight and you can easily implement it. It is fast and flexible to use.


Various types of segments in Log4j.


① Logger

② Appender

③ Layout


➊ What is Logger….?

The Logger is a class who extends the Category class who implements the AppenderAttachable interface. The main function of this class is used to log the messages. Its various methods are.


Method Names
Descriptions
static public Logger getLogger(Class class) 
static public Logger getLogger(String name) 
static public Logger getLogger(String name, LoggerFactory factory)
The name of the logger to retrieve.
static public Logger getRootLogger() 
This method is the only way to retrieve the root logger.

public void trace(Object msg)
public void trace(Object msg, Throwable t)
Log a message object with the {@link org.apache.log4j.Level#TRACE TRACE} level.
boolean public isTraceEnabled()
It is used to Check whether this category is enabled for the TRACE level.
public void debug(Object msg)
public void debug(Object msg, Throwable t)
The restriction level of this is the lowest used to print the message with their levels.

public void info(Object msg)
It is more restricted than debug and used for more informative.
public void warn(Object msg)
It is more restricted than info and used for warning kind of logs.
public void error(Object msg)
It is more restricted than warn and used for error logs.
public void fatal(Object message)
For printing the only message we can use FATAL.


The restriction level.

Trace < Debug < Info < Warn < Error < Fatal


➋ What is Appender..?

For publishing the information of log into various destinations like databases, console or file Appender is used.


➌ What is Layout..?

For providing different formatting or Design to the logs we use layout.


LOG4J vs System.out.Println()..?


➡ Managing the Log is easy because we can change the scope of Logger by giving scope we can on-off logger as per required that save times in case of production.

Example to use the Logger 

➡It is easy to check the warning and error from logs file cause developers are more often give attention to the WARN and DEBUG messages. 

➡ It generates better output and metadata for the understanding and debugging for the developers. It allows the developer to use their pattern for logging.



import org.apache.log4j.*;


public class Main{

/**

* Create Logger Object

*/

private static final Logger LOG4J= Logger.getLogger(Main.class);

public static void main(String s[]) {

LOG4J.warn("LoggerExample:Warn Message");

LOG4J.fatal("LoggerExample:Fatal Message");

LOG4J.error("LoggerExample:Error Message");

LOG4J.info("LoggerExample:Info Message");

LOG4J.debug("LoggerExample:Debug Message");

}


}


After execute the following example following output comes


Console output

2020-01-22 19:21:37 WARN Main:156 - LoggerExample:Warn Message

2020-01-22 19:21:37 FATAL Main:157 - LoggerExample:Fatal Message

2020-01-22 19:21:37 ERROR Main:158 - LoggerExample:Error Message

2020-01-22 19:21:37 INFO Main:159 - LoggerExample:Info Message

2020-01-22 19:21:37 DEBUG Main:160 - LoggerExample:Debug Message


Info.log output

2020-01-22 19:12:02 DEBUG Main:156 - LoggerExample:Debug Message

2020-01-22 19:12:02 INFO Main:157 - LoggerExample:Info Message


Error.log output

2020-01-22 19:12:02 WARN Main:158 - LoggerExample:Warn Message

2020-01-22 19:12:02 ERROR Main:159 - LoggerExample:Error Message

2020-01-22 19:12:02 FATAL Main:160 - LoggerExample:Fatal Message