In all my previous posts under java logging, I have configured logger and handler programmatically. We can externalize those configurations to properties file also. The advantage of this approach is that we can change configurations at any time without recompiling java class. In this post I will show with example how to do that. Let’s…… Continue reading Externalizing the logger and handler configuration to properties file
Category: Logging
Setting Log Levels for Logger and Handler
In this post under Java Logging, I will explain with example how to set log levels for Logger and Handler implementations. Please not the log level of Logger and Handler should be same otherwise some messages will not be logged by the handler. We set the log levels by calling “setLevel” method available on Logger…… Continue reading Setting Log Levels for Logger and Handler
Rotating File Handler
In this post under logging, I will show how to create a rotating file handler. A rotating file handler create a set of files, with each file having a count in the file name indicating the total number of files created. Each file has a size limit, which when reached a new file is created…… Continue reading Rotating File Handler
Java Util Logging ErrorManager example
In this post under java logging, I will explain the purpose of ErrorManager and how we can use it. Whenever we write the below code logger.info(“Hello my name is Sumanth1”); We are requesting the Handler to log the message to a destination. What if an error happens at the Handler level when an attempt is…… Continue reading Java Util Logging ErrorManager example
Logging to database using java logging framework
This post explains how to log messages to the database. Create a table with below schema CREATE TABLE logging ( loggername VARCHAR(50) NULL DEFAULT NULL, threadId INT(11) NULL DEFAULT NULL, loglevel VARCHAR(50) NULL DEFAULT NULL, logmessage VARCHAR(50) NULL DEFAULT NULL ) Create a custom handler which will log the message to the database. In our…… Continue reading Logging to database using java logging framework
Setting Filter at Logger level
In the post “Setting Filter at Handler level”, I showed how to set filter at handler level. In this post I will explain how to set filter at logger level. An use case of setting filter at logger level, is restricting logging of messages based on the user who logged it. Custom Filter 1 package…… Continue reading Setting Filter at Logger level
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
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