log
Class Log

java.lang.Object
  extended bylog.Log

public class Log
extends Object

Log is a very easy-to-use logging system, replacing efficiently "System.out.println()" debugging. Compared to the standard Logging class, or compared to the well known Log4J package, this one aims to follow Pareto's law: You will get 80% of the functionalities, but you will spend only 20% of your time. And most users even need less than 80%...

The fastest way to learn how to use this library is probably to study and test the two examples below:

Example 1:

import log.*;

public class LogTest {

  public static void main(String[] args) {
    Log.log("Start of the program");
      // logs a message with the default level, set by the setDefaultLevel()
      // method
    execute(); 
  }

  private static void execute() {
    Log.enter();
      // logs a method entry with the default level, set by the
      // setDefaultLevel() method
    Log.exit("I am quitting the execute() method");
      // logs a method exit with the default level, set by the
      // setDefaultLevel() method, and adds the specified message
  }
}
 

Example 2:

import log.*;
import mess.*;

public class LogTest2 {

  public static void main(String[] args) {
    Log.log("Start of the program");
      // logs a message with the default level, set by the setDefaultLevel()
      // method
    Log.setFile("./mylogs/report.log");
      // we want to record the logging information in a file called report.log
      // that is in the mylogs directory
    Log.setFileLevel(3);
      // logging instructions will be recorded in the file only if the level  
      // is <= 3 (Log.INFO is a shortcut that worth 3)
    Log.setFileLevel("mess.Message", 4);
      // but for the mess.Message class, we define the level = 4 (Log.DEBUG is
      // a shortcut that worth 4) 
    Log.setConsoleLevel(1);
      // in the console we want only very important logging information 
      // (Log.SEVERE is a shortcut that worth 1)
    execute(); 
    Message message = new Message("This is a message");
    message.display();
  }

  private static void execute() {
    Log.enter();
      // logs a method entry with the default level, set by the
      // setDefaultLevel() method
    Log.exit("I am quitting the execute() method");
      // logs a method exit with the default level, set by the
      // setDefaultLevel() method, and adds the specified message
  }
} 


package mess;

import log.*;

public class Message {
  private String text;

  public Message(String text) {
    Log.enter("I am entering the constructor", 1);
      // logs a method entry with the specified level and adds the specified
      // message    
    this.text = text;
    Log.exit();
      // logs a method exit with the default level, set by the setDefaultLevel()
      // method    
  }

  public void display() {
    Log.enter(1);
      // logs a method entry with the specified level
    Log.log("I am going to display a message", 1);
      // logs a message with the specified level
    System.out.println(text);
    Log.exit(1);
      // logs a method exit with the specified level    
  }
}
 

Version:
0.1
Author:
Michel Deriaz

Field Summary
static int ALL
          ALL is the maximum value for a message level.
static int DEBUG
          DEBUG is a message level used essentially during the debugging process.
static int INFO
          INFO is a message level that indicates an information.
static int OFF
          OFF is the minimum value for a message level.
static int SEVERE
          SEVERE is a message level that indicates a serious failure.
static int WARNING
          WARNING is a message level that indicates a warning.
 
Method Summary
static void enter()
          Logs a method entry according to the default levels.
static void enter(int level)
          Logs a method entry according to the specified level.
static void enter(Object msg)
          Logs a method entry according to the default levels, and adds the specified message.
static void enter(Object msg, int level)
          Logs a method entry according to the specified level, and adds the specified message.
static void exit()
          Logs a method exit according to the default levels.
static void exit(int level)
          Logs a method exit according to the specified level.
static void exit(Object msg)
          Logs a method exit according to the default levels, and adds the specified message.
static void exit(Object msg, int level)
          Logs a method exit according to the specified level, and adds the specified message.
static void log(Object msg)
          Logs the specified message according to the default levels.
static void log(Object msg, int level)
          Logs the specified message according to the specified level.
static void setConsoleLevel(int consoleLevel)
          Sets the current console level.
static void setConsoleLevel(String className, int consoleLevel)
          Sets the current console level for a specific class.
static void setDefaultLevel(int defaultLevel)
          Sets the default level value to use during a display operation for all the messages for whose the user did not specified a level.
static void setFile(String filename)
          Sets the file that records logging information.
static void setFile(String filename, int count)
          Initialize a set of files that record logging information.
static void setFileLevel(int fileLevel)
          Sets the current file level.
static void setFileLevel(String className, int fileLevel)
          Sets the current file level for a specific class.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OFF

public static final int OFF
OFF is the minimum value for a message level. It is initialized to Integer.MIN_VALUE and can be used to turn off logging.

See Also:
Constant Field Values

SEVERE

public static final int SEVERE
SEVERE is a message level that indicates a serious failure. It is initialized to 1.

See Also:
Constant Field Values

WARNING

public static final int WARNING
WARNING is a message level that indicates a warning. It is initialized to 2.

See Also:
Constant Field Values

INFO

public static final int INFO
INFO is a message level that indicates an information. It is initialized to 3.

See Also:
Constant Field Values

DEBUG

public static final int DEBUG
DEBUG is a message level used essentially during the debugging process. It is initialized to 4.

See Also:
Constant Field Values

ALL

public static final int ALL
ALL is the maximum value for a message level. It is initialized to Integer.MAX_VALUE and can be used to display all the logging messages.

See Also:
Constant Field Values
Method Detail

setFile

public static void setFile(String filename)
Sets the file that records logging information. The filename can be written with its path. For example "./mylogs/report.log" will write in a file called report.log in the mylogs subdirectory (if necessary, all the subdirectories are created during execution).

Parameters:
filename - the filename
See Also:
setFile(String, int)

setFile

public static void setFile(String filename,
                           int count)
Initialize a set of files that record logging information. The filename can be written with its path. For example "./mylogs/report.log" will write in a file called report.log in the mylogs subdirectory (if necessary, all the subdirectories are created during execution). The second parameter, count, specifies the cycle; the first file gets the extension ".001", the second ".002" and so on until the value specified by count is reached (and then the cycle starts again).

Parameters:
filename - the filename
count - the cyle (1 <= count <= 999)
See Also:
setFile(String)

setConsoleLevel

public static void setConsoleLevel(int consoleLevel)
Sets the current console level. All the messages with a smaller or equal level will be displayed, the others will be ignored.

Parameters:
consoleLevel - the current console level
See Also:
setConsoleLevel(String, int)
Default Value:
the default value for consoleLevel is 3 (Log.INFO)

setConsoleLevel

public static void setConsoleLevel(String className,
                                   int consoleLevel)
Sets the current console level for a specific class. All the messages with a smaller or equal level will be displayed, the others will be ignored. Note that the level specified for a specific class via this method overrides the level specified via the setConsoleLevel(int consoleLevel) method. If the class is inside a package, the name must be fully specified, for example: java.awt.Component. Note that once this method is used, the logging process will have to check every time if the current class has or not a specific level, which is a time consuming operation.

Parameters:
className - the name of the class, with its package
consoleLevel - the current console level for this class
See Also:
setConsoleLevel(int)

setFileLevel

public static void setFileLevel(int fileLevel)
Sets the current file level. All the messages with a smaller or equal level will be recorded, the others will be ignored.

Parameters:
fileLevel - the current file level
See Also:
setFileLevel(String, int)
Default Value:
the default value for fileLevel is 3 (Log.INFO)

setFileLevel

public static void setFileLevel(String className,
                                int fileLevel)
Sets the current file level for a specific class. All the messages with a smaller or equal level will be recorded, the others will be ignored. Note that the level specified for a specific class via this method overrides the level specified via the setFileLevel(int fileLevel) method. If the class is inside a package, the name must be fully specified, for example: java.awt.Component. Note that once this method is used, the logging process will have to check every time if the current class has or not a specific level, which is a time consuming operation.

Parameters:
className - the name of the class, with its package
fileLevel - the current console level for this class
See Also:
setFileLevel(int)

setDefaultLevel

public static void setDefaultLevel(int defaultLevel)
Sets the default level value to use during a display operation for all the messages for whose the user did not specified a level.

Parameters:
defaultLevel - the default level
Default Value:
the default value for defaultLevel is 3 (Log.INFO)

log

public static void log(Object msg)
Logs the specified message according to the default levels.

Parameters:
msg - the message

log

public static void log(Object msg,
                       int level)
Logs the specified message according to the specified level.

Parameters:
msg - the message
level - the level

enter

public static void enter()
Logs a method entry according to the default levels.


enter

public static void enter(int level)
Logs a method entry according to the specified level.

Parameters:
level - the level

enter

public static void enter(Object msg)
Logs a method entry according to the default levels, and adds the specified message.

Parameters:
msg - the message

enter

public static void enter(Object msg,
                         int level)
Logs a method entry according to the specified level, and adds the specified message.

Parameters:
msg - the message
level - the level

exit

public static void exit()
Logs a method exit according to the default levels.


exit

public static void exit(int level)
Logs a method exit according to the specified level.

Parameters:
level - the level

exit

public static void exit(Object msg)
Logs a method exit according to the default levels, and adds the specified message.

Parameters:
msg - the message

exit

public static void exit(Object msg,
                        int level)
Logs a method exit according to the specified level, and adds the specified message.

Parameters:
msg - the message
level - the level