By default, all the logging are displayed in the console. As the logs register all the behavior of the application we might need them later in order to check or analyze the working of the application.
Log4j also support logging the content into text file for later use just by tweaking the values and proprieties of the log4j.xml. First we will show you the best practices and move up its explanation.
Java File
import org.apache.logging.log4j.*;
public class Demo {
private static Logger log = LogManager.getLogger(Demo.class.getName());
public static void main(String[] args) {
int i = 10;
log.debug("Debugging");
if(i==10){
log.info("Info_log");
log.error("Error_log");
log.fatal("Fatal_log");
log.warn("Fatal_log");
}
}
}
Log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="basePath">./logs</Property>
</Properties>
<Appenders>
<RollingFile name="File" fileName="${basePath}/prints.log" filePattern="${basePath}/prints-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<SizeBasedTriggeringPolicy size="500" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
Explanation
By choosing to log as a text file we must handle the below concerns,
Where to Log
How to Log
What to Log
Where to Log
It is good practice to log the files inside the project's base folder as it is general practices people work on the same project and they might bump in to the problem if we specify unique path which is not available in their system. This can be done in property tag of the xml file as shown below
<Property name="basePath">./logs</Property>
BasePath refers to Project's base directory and ./logs denotes that the logs are stored inside a folder names logs in the root directory.
How to Log
If we are saving it as a file we have to watch out that the same file is not appended over and over again so it will not become a huge file. It is also needs a file name as it is a rolling file and needs a new name for every roll out.
<RollingFile name="File" fileName="${basePath}/prints.log" filePattern="${basePath}/prints-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<SizeBasedTriggeringPolicy size="500" />
</RollingFile>
We pass the file name as the arguments of the tag name Rolling file, it must also include the absolute file path along with its name. File pattern will let you know what if the file name or file name pattern of the rolling file.
Then the PatternLayout pattern denotes how logs to be printed inside the file and its format. Finally we have a SizeBasedTriggeringPolicy this will allow our file to roll out if the file size has reached a certain point, say a file limit.
I have included the snap of the logs generated from the above code.