Saturday, 2 August 2014

configuring logging

It is possible to configure logging API, using configuration file. By default, logging API uses the configuration file at 'jre8\lib\logging.properties.

You can specify custom configuration file at the time of running the program.
java -Djava.util.logging.config.file=myfile ProgName

If no configuration file specified at the time of running, then default configuration file will be taken.

Below are the list properties that you can set for logging

1. handlers : handlers will be installed during VM startup. You can add multiple handlers by specifying comma separated list.
   Ex: handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

2. .level : Specifies the default logging level.
     Ex: .level= INFO

3. Handler Specific Properties
a. Console Handler Specific
java.util.logging.ConsoleHandler.level specifies the default level for the Handler.
Ex : java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.filter specifies the name of a Filter class to use
Ex: java.util.logging.ConsoleHandler.filter = Custom_Filter
java.util.logging.ConsoleHandler.formatter specifies the name of a Formatter class to use.
Ex: java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.encoding the name of the character set encoding to use.
Ex: java.util.logging.ConsoleHandler.encoding = 'UTF-8'

b. FileHandler Specific
java.util.logging.FileHandler.level specifies the default level for the Handler
java.util.logging.FileHandler.filter specifies the name of a Filter class to use
java.util.logging.FileHandler.formatter specifies the name of a Formatter class to use
java.util.logging.FileHandler.encoding the name of the character set encoding to use
java.util.logging.FileHandler.limit specifies an approximate maximum amount to write (in bytes) to any one file. If limit is set to zero, then there is no limit on the size.
Ex: java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count specifies the number of log files to use in the log file rotation.
java.util.logging.FileHandler.pattern specifies a pattern for generating the output file name.
Ex: java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.append specifies whether the FileHandler should append onto any existing files. You can set value to true/false, based on the value it perform operation.

c. Memory Handler Specific
java.util.logging.MemoryHandler.level specifies the level for the Handler.
java.util.logging.MemoryHandler.filter specifies the name of a Filter class to use.
java.util.logging.MemoryHandler.size defines the buffer size.
java.util.logging.MemoryHandler.push defines the push Level. By default push level set to SEVERE.
Ex: java.util.logging.MemoryHandler.push = SEVERE
java.util.logging.MemoryHandler.target specifies the name of the target Handler class.

d. SocketHandler Specific
java.util.logging.SocketHandler.level specifies the default level for the Handler.
java.util.logging.SocketHandler.filter specifies the name of a Filter class to use.
java.util.logging.SocketHandler.formatter specifies the name of a Formatter class to use.
java.util.logging.SocketHandler.encoding the name of the character set encoding to use.
java.util.logging.SocketHandler.host specifies the target host name to connect to.
java.util.logging.SocketHandler.port specifies the target TCP port to use.
Ex: java.util.logging.SocketHandler.port = 23456

e. StreamHandler specific
java.util.logging.StreamHandler.level specifies the default level for the Handler.
java.util.logging.StreamHandler.filter specifies the name of a Filter class to use.
java.util.logging.StreamHandler.formatter specifies the name of a Formatter class to use.
java.util.logging.StreamHandler.encoding the name of the character set encoding to use.

Application with default configuration
import java.io.IOException;
import java.util.logging.*;

public class ConfigEx {
    static final Logger myLogger = Logger.getLogger("configEx");
    
    public static void main(String args[]) throws IOException{
        FileHandler handle1 = null;
        
        try{
            handle1 =  new FileHandler("log.out");
            myLogger.addHandler(handle1);
            
            myLogger.log(Level.FINE, "FINE Message");
            myLogger.log(Level.FINER, "FINER Message");
            myLogger.log(Level.FINEST, "FINEST Message");
            myLogger.log(Level.INFO, "INFO Message");
            myLogger.log(Level.WARNING, "WARNING Message");
            myLogger.log(Level.SEVERE, "SEVERE Message");
        }
        finally{
            if(handle1 != null)
                handle1.close();
        }
    }
}

Run the Application, then you can see the below messages in console.

Jul 29, 2014 2:22:53 PM ConfigEx main
INFO: INFO Message
Jul 29, 2014 2:22:53 PM ConfigEx main
WARNING: WARNING Message
Jul 29, 2014 2:22:53 PM ConfigEx main
SEVERE: SEVERE Message

'log.out' contains information like below.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2014-07-29T14:24:10</date>
  <millis>1406624050661</millis>
  <sequence>0</sequence>
  <logger>configEx</logger>
  <level>INFO</level>
  <class>ConfigEx</class>
  <method>main</method>
  <thread>1</thread>
  <message>INFO Message</message>
</record>
<record>
  <date>2014-07-29T14:24:10</date>
  <millis>1406624050701</millis>
  <sequence>1</sequence>
  <logger>configEx</logger>
  <level>WARNING</level>
  <class>ConfigEx</class>
  <method>main</method>
  <thread>1</thread>
  <message>WARNING Message</message>
</record>
<record>
  <date>2014-07-29T14:24:10</date>
  <millis>1406624050702</millis>
  <sequence>2</sequence>
  <logger>configEx</logger>
  <level>SEVERE</level>
  <class>ConfigEx</class>
  <method>main</method>
  <thread>1</thread>
  <message>SEVERE Message</message>
</record>
</log>

Application with custom configuration

custom_config.properties file contains below configurations
# Default Level
.level= WARNING

# Adding Handlers
handlers= java.util.logging.ConsoleHandler

# Adding File Handler specific configurations
java.util.logging.FileHandler.limit = 10000
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

Run the same Application like below.
java -Djava.util.logging.config.file=custom_config.properties ConfigEx

Below messages come in console.
Jul 29, 2014 2:28:15 PM ConfigEx main
WARNING: WARNING Message
Jul 29, 2014 2:28:15 PM ConfigEx main
SEVERE: SEVERE Message

log.out contains information like below.
Jul 29, 2014 2:28:15 PM ConfigEx main
WARNING: WARNING Message
Jul 29, 2014 2:28:15 PM ConfigEx main
SEVERE: SEVERE Message







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment