org.apache.commons.logging.impl

Class SimpleLog

Implemented Interfaces:
Log, Serializable

public class SimpleLog
extends java.lang.Object
implements Log, Serializable

Simple implementation of Log that sends all enabled log messages, for all defined loggers, to System.err. The following system properties are supported to configure the behavior of this logger:

In addition to looking for system properties with the names specified above, this implementation also checks for a class loader resource named "simplelog.properties", and includes any matching definitions from this resource (if it exists).

Version:
$Id: SimpleLog.java,v 1.21 2004/06/06 20:47:56 rdonkin Exp $
Authors:
Scott Sanders
Rod Waldhoff
Robert Burrell Donkin

Field Summary

protected static String
DEFAULT_DATE_TIME_FORMAT
The default format to use when formating dates
static int
LOG_LEVEL_ALL
Enable all logging levels
static int
LOG_LEVEL_DEBUG
"Debug" level logging.
static int
LOG_LEVEL_ERROR
"Error" level logging.
static int
LOG_LEVEL_FATAL
"Fatal" level logging.
static int
LOG_LEVEL_INFO
"Info" level logging.
static int
LOG_LEVEL_OFF
Enable no logging levels
static int
LOG_LEVEL_TRACE
"Trace" level logging.
static int
LOG_LEVEL_WARN
"Warn" level logging.
protected int
currentLogLevel
The current log level
protected static DateFormat
dateFormatter
Used to format times
protected static String
dateTimeFormat
The date and time format to use in the log message
protected String
logName
The name of this simple log instance
private String
shortLogName
The short name of this simple log instance
protected static boolean
showDateTime
Include the current time in the log message
protected static boolean
showLogName
Include the instance name in the log message?
protected static boolean
showShortName
Include the short name ( last component ) of the logger in the log message.
protected static Properties
simpleLogProps
Properties loaded from simplelog.properties
protected static String
systemPrefix
All system properties used by SimpleLog start with this

Constructor Summary

SimpleLog(String name)
Construct a simple log with given name.

Method Summary

void
debug(Object message)
Log a message with debug log level.
void
debug(Object message, Throwable t)
Log an error with debug log level.
void
error(Object message)
Log a message with error log level.
void
error(Object message, Throwable t)
Log an error with error log level.
void
fatal(Object message)
Log a message with fatal log level.
void
fatal(Object message, Throwable t)
Log an error with fatal log level.
private static boolean
getBooleanProperty(String name, boolean dephault)
private static ClassLoader
getContextClassLoader()
Return the thread context class loader if available.
int
getLevel()
Get logging level.
private static InputStream
getResourceAsStream(String name)
private static String
getStringProperty(String name)
private static String
getStringProperty(String name, String dephault)
void
info(Object message)
Log a message with info log level.
void
info(Object message, Throwable t)
Log an error with info log level.
boolean
isDebugEnabled()
Are debug messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

boolean
isErrorEnabled()
Are error messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

boolean
isFatalEnabled()
Are fatal messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

boolean
isInfoEnabled()
Are info messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

protected boolean
isLevelEnabled(int logLevel)
Is the given log level currently enabled?
boolean
isTraceEnabled()
Are trace messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

boolean
isWarnEnabled()
Are warn messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

protected void
log(int type, Object message, Throwable t)
Do the actual logging.
void
setLevel(int currentLogLevel)
Set logging level.
void
trace(Object message)
Log a message with trace log level.
void
trace(Object message, Throwable t)
Log an error with trace log level.
void
warn(Object message)
Log a message with warn log level.
void
warn(Object message, Throwable t)
Log an error with warn log level.
protected void
write(StringBuffer buffer)
Write the content of the message accumulated in the specified StringBuffer to the appropriate output destination.

Field Details

DEFAULT_DATE_TIME_FORMAT

protected static final String DEFAULT_DATE_TIME_FORMAT
The default format to use when formating dates

LOG_LEVEL_ALL

public static final int LOG_LEVEL_ALL
Enable all logging levels
Field Value:
0

LOG_LEVEL_DEBUG

public static final int LOG_LEVEL_DEBUG
"Debug" level logging.
Field Value:
2

LOG_LEVEL_ERROR

public static final int LOG_LEVEL_ERROR
"Error" level logging.
Field Value:
5

LOG_LEVEL_FATAL

public static final int LOG_LEVEL_FATAL
"Fatal" level logging.
Field Value:
6

LOG_LEVEL_INFO

public static final int LOG_LEVEL_INFO
"Info" level logging.
Field Value:
3

LOG_LEVEL_OFF

public static final int LOG_LEVEL_OFF
Enable no logging levels
Field Value:
7

LOG_LEVEL_TRACE

public static final int LOG_LEVEL_TRACE
"Trace" level logging.
Field Value:
1

LOG_LEVEL_WARN

public static final int LOG_LEVEL_WARN
"Warn" level logging.
Field Value:
4

currentLogLevel

protected int currentLogLevel
The current log level

dateFormatter

protected static DateFormat dateFormatter
Used to format times

dateTimeFormat

protected static String dateTimeFormat
The date and time format to use in the log message

logName

protected String logName
The name of this simple log instance

shortLogName

private String shortLogName
The short name of this simple log instance

showDateTime

protected static boolean showDateTime
Include the current time in the log message

showLogName

protected static boolean showLogName
Include the instance name in the log message?

showShortName

protected static boolean showShortName
Include the short name ( last component ) of the logger in the log message. Defaults to true - otherwise we'll be lost in a flood of messages without knowing who sends them.

simpleLogProps

protected static final Properties simpleLogProps
Properties loaded from simplelog.properties

systemPrefix

protected static final String systemPrefix
All system properties used by SimpleLog start with this

Constructor Details

SimpleLog

public SimpleLog(String name)
Construct a simple log with given name.
Parameters:
name - log name

Method Details

debug

public final void debug(Object message)
Log a message with debug log level.
Specified by:
debug in interface Log

debug

public final void debug(Object message,
                        Throwable t)
Log an error with debug log level.
Specified by:
debug in interface Log

error

public final void error(Object message)
Log a message with error log level.
Specified by:
error in interface Log

error

public final void error(Object message,
                        Throwable t)
Log an error with error log level.
Specified by:
error in interface Log

fatal

public final void fatal(Object message)
Log a message with fatal log level.
Specified by:
fatal in interface Log

fatal

public final void fatal(Object message,
                        Throwable t)
Log an error with fatal log level.
Specified by:
fatal in interface Log

getBooleanProperty

private static boolean getBooleanProperty(String name,
                                          boolean dephault)

getContextClassLoader

private static ClassLoader getContextClassLoader()
Return the thread context class loader if available. Otherwise return null. The thread context class loader is available for JDK 1.2 or later, if certain security conditions are met.

getLevel

public int getLevel()
Get logging level.

getResourceAsStream

private static InputStream getResourceAsStream(String name)

getStringProperty

private static String getStringProperty(String name)

getStringProperty

private static String getStringProperty(String name,
                                        String dephault)

info

public final void info(Object message)
Log a message with info log level.
Specified by:
info in interface Log

info

public final void info(Object message,
                       Throwable t)
Log an error with info log level.
Specified by:
info in interface Log

isDebugEnabled

public final boolean isDebugEnabled()
Are debug messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

Specified by:
isDebugEnabled in interface Log

isErrorEnabled

public final boolean isErrorEnabled()
Are error messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

Specified by:
isErrorEnabled in interface Log

isFatalEnabled

public final boolean isFatalEnabled()
Are fatal messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

Specified by:
isFatalEnabled in interface Log

isInfoEnabled

public final boolean isInfoEnabled()
Are info messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

Specified by:
isInfoEnabled in interface Log

isLevelEnabled

protected boolean isLevelEnabled(int logLevel)
Is the given log level currently enabled?
Parameters:
logLevel - is this level enabled?

isTraceEnabled

public final boolean isTraceEnabled()
Are trace messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

Specified by:
isTraceEnabled in interface Log

isWarnEnabled

public final boolean isWarnEnabled()
Are warn messages currently enabled?

This allows expensive operations such as String concatenation to be avoided when the message will be ignored by the logger.

Specified by:
isWarnEnabled in interface Log

log

protected void log(int type,
                   Object message,
                   Throwable t)
Do the actual logging. This method assembles the message and then calls write() to cause it to be written.
Parameters:
type - One of the LOG_LEVEL_XXX constants defining the log level
message - The message itself (typically a String)
t - The exception whose stack trace should be logged

setLevel

public void setLevel(int currentLogLevel)
Set logging level.
Parameters:
currentLogLevel - new logging level

trace

public final void trace(Object message)
Log a message with trace log level.
Specified by:
trace in interface Log

trace

public final void trace(Object message,
                        Throwable t)
Log an error with trace log level.
Specified by:
trace in interface Log

warn

public final void warn(Object message)
Log a message with warn log level.
Specified by:
warn in interface Log

warn

public final void warn(Object message,
                       Throwable t)
Log an error with warn log level.
Specified by:
warn in interface Log

write

protected void write(StringBuffer buffer)
Write the content of the message accumulated in the specified StringBuffer to the appropriate output destination. The default implementation writes to System.err.
Parameters:
buffer - A StringBuffer containing the accumulated text to be logged

Copyright 2002-2004 The Apache Software Foundation.