Setting Filter at Handler level

Java logging provides log levels at logger and handler level, to filter the messages. To make sure only required messages are logged. We can extend this functionality and provide our own filter, which will filter messages based on message’s metadata in addition to filtering by log levels. We can add this filter at logger level…… Continue reading Setting Filter at Handler level

Supplier Functional Interface Example

Java 8 added a new functional interface named “Supplier”. The purpose of this interface is to supply instances. I will be explaining using the below example. In the example I have want of list of employees. I will separate the logic behind employee instance creation and adding to list. The employee instance will be created…… Continue reading Supplier Functional Interface Example

Consumer Functional Interface Example

Java 8 added a new functional interface named “Consumer”. The purpose of this interface is to consume/process the information received, it doesn’t return anything. I will be explaining using the below example. In the example I have list of employees whom I want to terminate. I will pass the list of employees to an implementation…… Continue reading Consumer Functional Interface Example

BiPredicate Simple Example

This post explains BiPredicate functional interface. BiPredicate is similar to Predicate functional interface except BiPredicate takes two arguments instead of one. In the below example we will filter list of Person entities based on first and last name. The predicate class name will be “NamePredicate”. NamePredicate package Function; import java.util.function.BiPredicate; public class NamePredicate implements BiPredicate…… Continue reading BiPredicate Simple Example

Different ways to convert collection to an array

This post explains two ways through which we can convert a collection to an array. The collection can be a list or set. First approach is simply calling “toArray” on collection instance. This method returns an Object array, When converting to array it doesn’t consider the data type of the entries in the collection. Below…… Continue reading Different ways to convert collection to an array

Creating a custom log message format

This post explains how to create a custom log message format. Out of the box java provides two log formats SimpleFormatter and XMLFormatter. Both extends the abstract super class Formatter. We can create our own custom format. Below is the code of the custom format CustomFormatter 1 package Logging; 2 3 import java.util.logging.Formatter; 4 import…… Continue reading Creating a custom log message format

Changing the default log message format

In this post I will be explaining how to change the default log message format provided by java util loggging. In case of logging to console (done by ConsoleHandler), the default format used is an instance of java.util.logging.SimpleFormatter. In case of logging to file (done by FileHandler), the default format used is an instance of…… Continue reading Changing the default log message format

Redirecting logging to a file

In this post I will explain how to program the logger to log messages to a file instead of logging to console. Below is the code Main code 1 package Logging; 2 3 import java.io.IOException; 4 import java.util.logging.FileHandler; 5 import java.util.logging.Logger; 6 7 public class LoggingDemo2 { 8 public static void main(String[] args) throws IOException…… Continue reading Redirecting logging to a file

Java logging simple example

This post explains java logging with simple example. Main code 1 package Logging; 2 3 import java.io.IOException; 4 import java.util.logging.Logger; 5 6 public class LoggingDemo1 { 7 public static void main(String[] args) throws IOException { 8 Logger logger = Logger.getLogger(“logger1”); 9 logger.info(“Hello my name is Mathew”); 10 logger.warning(“Hello my name is Mathew”); 11 logger.config(“Hello my…… Continue reading Java logging simple example

Quartz Simple Example

Quartz schedular framework can be used to schedule jobs. This post explains how we can use it with a simple example. We need the below jars 1) c3p0-0.9.1.1.jar 2) log4j-1.2.16.jar 3) quartz-2.2.3.jar 4) quartz-jobs-2.2.3.jar 5) slf4j-api-1.7.7.jar 6) slf4j-log4j12-1.7.7.jar log4j.xml file in the classpath with the below configurations log4j.xml <?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>…… Continue reading Quartz Simple Example