public class Logger
extends java.lang.Object
The pre-defined Logging levels are:
LEVEL_FATAL //highest level: only FATAL messages are logged LEVEL_ERROR LEVEL_WARN LEVEL_INFO LEVEL_DEBUG LEVEL_FINEST //lowest level: everything will be logged
In addition to logging levels, there are flags that can be set so as to influence the format of log records. For instance you can unset the flag LOG_TIME so that the current time is not automatically included in a log record (for better run time performance). Here is a list of all the flags:
LOG_THREAD_NAME //flag for whether to include current thread name in a log record LOG_TIME //flag for whether to include current time in a log record
Usage:
Example: A sample class using the Logger API.
package oracle.lbs.foo; public class Bar { private static Logger log = Logger.getLogger("oracle.lbs.foo.Bar"); int a; pubilc void zero() { a = 0; log.info("set a to zero"); } public int divide(int m) { if(a==0) { log.error("Dividing "+m+" by zero"); return Number.NaN; } ... } public static void main(String[] args) { //tweaking the global logging configurations Logger.setGlobalLevel(Logger.LEVEL_WARN); Logger.setGlobalFlags(Logger.LOG_TIME | Logger.LOG_THREAD_NAME); //special logging level for classes under "oracle.sdovis.stylex" package Logger.setGlobalLevel(Logger.LEVEL_DEBUG, "oracle.sdovis.stylex"); ...... } }
Modifier and Type | Field and Description |
---|---|
static int |
LEVEL_DEBUG
the DEBUG logging level.
|
static int |
LEVEL_ERROR
the ERROR logging level.
|
static int |
LEVEL_FATAL
the highest logging level; Logs unrecoverable program events.
|
static int |
LEVEL_FINEST
the finest possible logging level.
|
static int |
LEVEL_INFO
the INFO logging level, used for logging program configuration and other useful information.
|
static int |
LEVEL_WARN
the WARN logging level.
|
static int |
LOG_THREAD_NAME
Set this flag if log records should include the name of the logging thread.
|
static int |
LOG_TIME
Set this flag if log records should include the date/time of the logging event.
|
Modifier and Type | Method and Description |
---|---|
static int |
addGlobalOutputStream(java.io.PrintStream ps)
Adds a global log outputstream.
|
static int |
addGlobalOutputStream(java.io.PrintStream ps, java.lang.String name)
Adds a global log outputstream to the given namespace.
|
int |
addOutputStream(java.io.PrintStream ps)
Adds a logging output stream.
|
void |
debug(java.lang.Exception e)
Logs a debug exception
|
void |
debug(java.lang.String message)
Logs a debug message
|
void |
debug(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
Logs a debug message
|
void |
error(java.lang.Exception e)
Logs a error exception
|
void |
error(java.lang.String message)
Logs an error
|
void |
error(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
Logs an error
|
void |
fatal(java.lang.Exception e)
Logs a fatal exception
|
void |
fatal(java.lang.String message)
Logs a fatal event
|
void |
fatal(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
Logs a fatal event
|
void |
finest(java.lang.Exception e)
Logs a debug exception
|
void |
finest(java.lang.String message)
Logs a very fine-level message
|
void |
finest(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
Logs a very fine-level message
|
int |
getFlags()
Gets the current flags of this instance.
|
static int |
getGlobalFlags()
Gets the current global logging flags.
|
static int |
getGlobalLevel()
Gets the current global logging level.
|
int |
getLevel()
Gets the logging level of this instance.
|
static Logger |
getLogger(java.lang.String name)
Creates an instance of Logger with given name.
|
static Logger |
getLogger(java.lang.String name, int level)
Creates an instance of Logger with given name and level.
|
void |
info(java.lang.Exception e)
Logs an info exception
|
void |
info(java.lang.String message)
Logs a piece of information (such as configuration et al).
|
void |
info(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
Logs a piece of information (such as configuration et al).
|
static void |
readGlobalConfig(java.util.Properties p)
Reads the propeties and sets global configuration accordingly.
|
void |
setFlags(int flags)
Sets the flags (an OR'ed integer of all desired flags)
|
static void |
setGlobalFlags(int f)
Sets the global logging flags.
|
static void |
setGlobalFlags(int f, java.lang.String name)
Sets the global logging flags for the named loggers only.
|
static void |
setGlobalLevel(int level)
Sets the global logging level.
|
static void |
setGlobalLevel(int level, java.lang.String name)
Sets the global logging level for the named loggers.
|
static void |
setGlobalOutputStream(java.io.PrintStream ps)
Sets the global log outputsream.
|
static void |
setGlobalOutputStream(java.io.PrintStream ps, java.lang.String name)
Sets the global log outputsream for the named hiearchy.
|
void |
setLevel(int level)
Sets the logging level of this instance.
|
void |
setOutputStream(java.io.PrintStream ps)
Sets the logging output stream.
|
void |
warn(java.lang.Exception e)
Logs a warning exception
|
void |
warn(java.lang.String message)
Logs a warning
|
void |
warn(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
Logs a warning
|
public static final int LEVEL_FATAL
public static final int LEVEL_ERROR
public static final int LEVEL_WARN
public static final int LEVEL_INFO
public static final int LEVEL_DEBUG
public static final int LEVEL_FINEST
public static final int LOG_THREAD_NAME
public static final int LOG_TIME
public static Logger getLogger(java.lang.String name)
name
- the name for the new logger instance. The name should follow Java class package convention. Examples are "com.foo.bar" or "oracle.lbs.TestClass".public static Logger getLogger(java.lang.String name, int level)
name
- the name for the new logger instance. The name should follow Java class package convention. Examples are "com.foo.bar" or "oracle.lbs.TestClass".level
- the logging level for this instance. Any log messages that are lower than this level will be ignored.public static void setGlobalLevel(int level)
level
- the logging levelpublic static void setGlobalLevel(int level, java.lang.String name)
level
- the new logging levelname
- the namespace for the affected loggerspublic static int getGlobalLevel()
public static void setGlobalFlags(int f)
f
- the new global logging flags.public static void setGlobalFlags(int f, java.lang.String name)
f
- the new global logging flags.name
- the namespace of affected loggerspublic static int getGlobalFlags()
public static void readGlobalConfig(java.util.Properties p)
p
- the properties. The following three properties are supported:
log_level = fatal | error | warn | info | debug log_time = true | false log_thread_name = true | false
public static void setGlobalOutputStream(java.io.PrintStream ps)
ps
- the new PrintStream to use as logging destination.public static void setGlobalOutputStream(java.io.PrintStream ps, java.lang.String name)
ps
- the new PrintStream to use as logging destination.public static int addGlobalOutputStream(java.io.PrintStream ps)
ps
- the new outputstream to be added.public static int addGlobalOutputStream(java.io.PrintStream ps, java.lang.String name)
ps
- the new outputstream to be added.name
- the named loggers will be affected.public void fatal(java.lang.String message)
public void fatal(java.lang.Exception e)
public void fatal(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
public void error(java.lang.String message)
public void error(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
public void error(java.lang.Exception e)
public void warn(java.lang.String message)
public void warn(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
public void warn(java.lang.Exception e)
public void info(java.lang.String message)
public void info(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
public void info(java.lang.Exception e)
public void debug(java.lang.String message)
public void debug(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
public void debug(java.lang.Exception e)
public void finest(java.lang.String message)
public void finest(java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String message)
public void finest(java.lang.Exception e)
public void setLevel(int level)
public int getLevel()
public void setFlags(int flags)
public int getFlags()
public void setOutputStream(java.io.PrintStream ps)
public int addOutputStream(java.io.PrintStream ps)