Featured

Java 16 logging

One of the features I liked in Java16 are the logging changes. The updated logging API works on handlers. There are different handlers FileHandler, MemoryHandler and ConsoleHandler. Depending on the need, these handlers can be configured in runtime.

Here is a scenario., if you want to debug or pipe entry of the logging information going to a different stream based on the condition. You can configure this in runtime.

Here is a run down code where a log file will get created in runtime based on a condition.

package me.sathish;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingSample {
private static Logger logger = Logger.getLogger("me.sathish.package");
private static FileHandler fh;
private static FileHandler ch;
static {
try {
fh = new FileHandler("mylog.txt");
ch = new FileHandler("Secondfile.txt");
} catch (IOException e) {
logger.log(Level.SEVERE, "Logging file is not found");
}
}
public static void main(String[] args) {
int start = 5;
int counter = 0;
logger.addHandler(fh);
logger.setLevel(Level.ALL);
for (int i = 0; i < start; i++) {
if (i == 3) {
logger.addHandler(ch);
logger.setLevel(Level.ALL);
}
logger.log(Level.SEVERE, "This is the counter " + counter++);
}
}
}
view raw LoggingSample.java hosted with ❤ by GitHub
Logging Gist

In the code block we have a condition where a file handler is added when the counter reaches a value of 3. So now based on this condition we will have two file handlers that will get generated. The first one will have all the entries of the code.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2021-08-28T04:37:27.363135Z</date>
<millis>1630125447363</millis>
<nanos>135000</nanos>
<sequence>0</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter0</message>
</record>
<record>
<date>2021-08-28T04:37:27.412285Z</date>
<millis>1630125447412</millis>
<nanos>285000</nanos>
<sequence>1</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter1</message>
</record>
<record>
<date>2021-08-28T04:37:27.413234Z</date>
<millis>1630125447413</millis>
<nanos>234000</nanos>
<sequence>2</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter2</message>
</record>
<record>
<date>2021-08-28T04:37:27.414026Z</date>
<millis>1630125447414</millis>
<nanos>26000</nanos>
<sequence>3</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter3</message>
</record>
<record>
<date>2021-08-28T04:37:27.414967Z</date>
<millis>1630125447414</millis>
<nanos>967000</nanos>
<sequence>4</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter4</message>
</record>
</log>
view raw mylog.txt hosted with ❤ by GitHub

The second file has only the entries that are satisfying the condition.,

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2021-08-28T04:37:53.555130Z</date>
<millis>1630125473555</millis>
<nanos>130000</nanos>
<sequence>3</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter 3</message>
</record>
<record>
<date>2021-08-28T04:37:53.555951Z</date>
<millis>1630125473555</millis>
<nanos>951000</nanos>
<sequence>4</sequence>
<logger>me.sathish.package</logger>
<level>SEVERE</level>
<class>me.sathish.LoggingSample</class>
<method>main</method>
<thread>1</thread>
<message>This is the counter 4</message>
</record>
</log>
view raw Secondfile.txt hosted with ❤ by GitHub