Java Logging (Part 2)

Logging Components:

  1. Logger – doing the actual logging process by callings its log or logp method.
  2. Handler – it handles the way we need to publish the logging to. for example into a file or output, etc. every Logger can have 1 or more handlers.
  3. Formatter – present the way we want to publish the log, in a simple String ro XML format. every handler has 1 formatter.

Logger.log() pass the String of message encapsulated into a logRecord to Handler and each handler pass the content into its Formatter and that formatter depends on its configuration, generate the final string and pass it back to its Handler and then handler will put the log content into the file or output or etc.

logger mylooger = Logger.getLogger("loggername")

Below examples is showing how to use the 2 common known formatter with logger.

import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

* @author Unknown_
public class PracticeStandardLogging2 {

private static Logger logger = Logger.getLogger("com.navid.MyClass");

public PracticeStandardLogging2() {

public void myMethod1() throws IOException {

// creating handler
Handler handler1 =<strong> new ConsoleHandler();</strong>
// creating formatter
Formatter formatter1 = <strong>new SimpleFormatter();</strong>

//preparing the formatter and handlers.
<strong> handler1.setFormatter(formatter1);</strong>
<strong>        logger.addHandler(handler1);</strong>

logger.warning("something happened - logging into console");
logger.fine("just logged a fine level - logging into console.");
// creating handler
<span style="color: #800000;">Handler handler2 = new <strong>FileHandler</strong>("%h\\practiceLogger_java_%g.log", 1000, 4, true);</span>
// creating formatter
Formatter formatter2 = <strong>new SimpleFormatter();</strong>

//preparing the formatter and handlers.

for (int index = 0; index < 200; index++) {

logger.warning("something happened reported into a file.");
logger.fine("just logged a fine level - reported into a file.");

public static void main(String[] args) throws IOException {
PracticeStandardLogging2 p = new PracticeStandardLogging2();


in above example class, there are two handlers:

  1. ConsoleHandler – which direct the output of logging into the console.err or as standard if that maps to Console.out, you can see the logging in console.
  2. FileHandler – which direct the output of logging into the file.

FileHandler Methods:

in this line of code, basically we are telling fileHandler to loop through all the log files with mentioned patterns.

Handler handler2 = new FileHandler(“%h\\practiceLogger_java_%g.log”, 1000, 4, true);

  • %h: is poting to host machine user home directory, e.g: in windows it will be something like. C:\users\navid\practiceLogger_java_.log
  • %g: is the number that will be generated autoamatically by logger.
  • 1000: is the limit amount of size that is presented in byte and is referring to the limited amount of every file size before creating or moving to another log file.
  • 4: is referring to total amount of files that could be generated by logger.
  • true/false: is appendable capabilities of the logger to the already existed files.

after running the above example test I have these files in my home directory

11/20/2016  11:47 PM               849 practiceLogger_java_0.log
11/20/2016  11:47 PM             1,132 practiceLogger_java_1.log
11/20/2016  11:47 PM             1,132 practiceLogger_java_2.log
11/20/2016  11:47 PM             1,132 practiceLogger_java_3.log
  • %t there is another switch like above swiches but this one is pointing to the temp directory in every operating system %t.
  • \\ can be used to dig into the directories.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s