public class Log
extends java.util.logging.Logger
Vona's utils: Logging mechanism.
This is built on top of java.util.logging.Logger
but adds several
conveniences, including
debug(...)
, warning(...)
, and error(...)
APIsDuring app init, setup logging something like this:
Log.setLoggingEnabled(true); Log.setOutputStream(myOutputStream); //optional Log.setGlobalExitOnError(myExitOnError); //optional Log.setUseVT100Attribs(myUseVT100Attribs); //optional
In a major base class like myapp.mypackage.Base include a static Log reference something like this:
protected static Log log = Log.getLog("myapp.mypackage");
Issue log messages in myapp.mypackage.Base and descendants like this:
log.debug("hello world!"); log.warning(myThrowable, "oops!"); log.error(myException);
Copyright (C) 2004 Marsette A. Vona, III
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Modifier and Type | Class and Description |
---|---|
private static class |
Log.LogHandler
Our own handler, includes a nice Formatter, and knows its
OutputStream.
|
protected static class |
Log.LogRec
Internal wrapper around LogRecord archives its own creation point by
consing a new Exception in its constructor.
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
cvsid
CVS id.
|
static java.util.logging.Level |
DEF_LOG_LEVEL
The default log level.
|
static boolean |
DEFAULT_ENABLED
Whether logging is enabled by default.
|
static boolean |
DEFAULT_EXIT_ON_ERROR
Whether to
System.exit(-1) on error(...) calls by
default. |
static java.io.OutputStream |
DEFAULT_OUTPUT_STREAM
The default OutputStream, System.err.
|
static boolean |
DEFAULT_USE_VT100ATTRIBS
Whether to use
VT100Attribs by default. |
private boolean |
enabled
Whether logging is enabled on this logger.
|
private boolean |
exitOnError
Set whether to
System.exit(-1) on error(...) calls on
this logger. |
private static java.lang.ThreadLocal<java.util.Formatter> |
formatterTLS
thread local storage for
printf -style formatters |
private static boolean |
globalExitOnError
Set whether to
System.exit(-1) on error(...) calls
globally. |
private static Log.LogHandler |
handler
Output Handler to console.
|
private java.util.logging.Level |
level
The log Level in use by this Log.
|
static java.util.Map<java.util.logging.Level,VT100Attribs> |
LEVEL_ATTRIBS
VT100Attribs for displaying messages on the console at each log
Level. |
static java.util.logging.Level[] |
LEVELS
all log levels
|
private static java.util.Map<java.lang.String,Log> |
localLogs
In case we don't have privs to add Logs to the LogManager, e.g.
|
static java.util.logging.Level |
LOG_LEVEL_ALL
log level
|
static java.util.logging.Level |
LOG_LEVEL_CONFIG
log level
|
static java.util.logging.Level |
LOG_LEVEL_FINE
log level
|
static java.util.logging.Level |
LOG_LEVEL_FINER
log level
|
static java.util.logging.Level |
LOG_LEVEL_FINEST
log level
|
static java.util.logging.Level |
LOG_LEVEL_INFO
log level
|
static java.util.logging.Level |
LOG_LEVEL_OFF
log level
|
static java.util.logging.Level |
LOG_LEVEL_SEVERE
log level
|
static java.util.logging.Level |
LOG_LEVEL_WARNING
log level
|
private static boolean |
loggingEnabled
Whether logging is enabled globally.
|
static java.util.logging.Level |
MIN_STACK_TRACE
The minimum level at which a stack trace is included in the log.
|
private boolean |
publishDirect
This is set iff there was a problem (e.g.
|
static VT100Attribs |
SEQUENCE_ATTRIBS
VT100Attribs for displaying the sequence counter in messages on
the console. |
static VT100Attribs |
STACKLEVEL_ATTRIBS
VT100Attribs for displaying the stacklevel in messages on the
console. |
static VT100Attribs |
THROWN_ATTRIBS
VT100Attribs for displaying Throwables in messages on the
console. |
private static boolean |
useVT100Attribs
Whether to use
VT100Attribs globally. |
Modifier | Constructor and Description |
---|---|
protected |
Log(java.lang.String name)
Create a Log with the specified name and no resource bundle.
|
Modifier and Type | Method and Description |
---|---|
void |
dbg()
covers
debug() |
void |
dbg(java.lang.String message)
covers
debug(String, Object...) |
void |
dbg(java.lang.String message,
java.lang.Object... args)
covers
debug(String, Object...) |
void |
dbg(java.lang.Throwable t)
covers
debug(Throwable) |
void |
dbg(java.lang.Throwable t,
java.lang.String message,
java.lang.Object... args)
|
void |
debug()
|
void |
debug(java.lang.String message)
|
void |
debug(java.lang.String message,
java.lang.Object... args)
|
void |
debug(java.lang.Throwable t)
|
void |
debug(java.lang.Throwable t,
java.lang.String message,
java.lang.Object... args)
Issue a Level.INFO with the specified Throwable and message.
|
void |
err()
covers
error() |
void |
err(java.lang.String message)
covers
error(String, Object...) |
void |
err(java.lang.String message,
java.lang.Object... args)
covers
error(String, Object...) |
void |
err(java.lang.Throwable t)
covers
error(Throwable) |
void |
err(java.lang.Throwable t,
java.lang.String message,
java.lang.Object... args)
|
void |
error()
|
void |
error(java.lang.String message)
|
void |
error(java.lang.String message,
java.lang.Object... args)
|
void |
error(java.lang.Throwable t)
|
void |
error(java.lang.Throwable t,
java.lang.String message,
java.lang.Object... args)
Like
debug(Throwable, String, Object...) but uses
Level.SEVERE, however, if exitOnError or globalExitOnError is set, this method will call System.exit(-1)
before it returns. |
protected java.lang.String |
formatMessage(java.lang.String message,
java.lang.Object... args)
If args is non-null and not zero-length, then expand
message as a
printf -style format with these args, else
return message unmodified. |
boolean |
getExitOnError()
Get whether to
System.exit(-1) on error(...) calls on
this logger. |
static boolean |
getGlobalExitOnError()
Get whether to
System.exit(-1) on error(...) calls
globally. |
static Log |
getLog(java.lang.String name)
Get the Log with the specified name.
|
static boolean |
getUseVT100Attribs()
Check whether to use
VT100Attribs (globally) to
make the log messages pretty on the OutputStream. |
void |
gothere()
Issue a "gothere"
debug(String) message. |
boolean |
isEnabled()
Check whether logging is enabled on this logger.
|
boolean |
isLoggable(java.util.logging.Level level)
extends super to also check
level |
static boolean |
isLoggingEnabled()
Check whether logging is globally enabled.
|
void |
log(java.util.logging.LogRecord r)
Issue an arbitrary LogRecord.
|
void |
setEnabled(boolean enabled)
Enable or disable logging on this logger.
|
void |
setExitOnError(boolean exitOnError)
Set whether to
System.exit(-1) on error(...) calls on
this logger. |
static void |
setGlobalExitOnError(boolean exitOnError)
Set whether to
System.exit(-1) on error(...) calls
globally. |
void |
setLevel(java.util.logging.Level level)
set level, catching access control exceptions
|
void |
setLevel(java.lang.String levelName)
set log level by name
|
static void |
setLoggingEnabled(boolean enabled)
Enable or disable logging globally.
|
static void |
setOutputStream(java.io.OutputStream os)
Set the OutputStream to which log messages are issued.
|
static void |
setUseVT100Attribs(boolean useVT100Attribs)
Set whether to use
VT100Attribs (globally) to
make the log messages pretty on the OutputStream. |
void |
TBD()
Issues a "TBD"
warning(String) . |
void |
warn()
covers
warning() |
void |
warn(java.lang.String message)
|
void |
warn(java.lang.String message,
java.lang.Object... args)
covers {@link #warning(String, Object...
|
void |
warn(java.lang.Throwable t)
covers
warning(Throwable) |
void |
warn(java.lang.Throwable t,
java.lang.String message,
java.lang.Object... args)
|
void |
warning()
|
void |
warning(java.lang.String message)
|
void |
warning(java.lang.String message,
java.lang.Object... args)
|
void |
warning(java.lang.Throwable t)
|
void |
warning(java.lang.Throwable t,
java.lang.String message,
java.lang.Object... args)
Like
debug(Throwable, String, Object...) but uses
Level.WARNING. |
addHandler, config, entering, entering, entering, exiting, exiting, fine, finer, finest, getAnonymousLogger, getAnonymousLogger, getFilter, getGlobal, getHandlers, getLevel, getLogger, getLogger, getName, getParent, getResourceBundle, getResourceBundleName, getUseParentHandlers, info, log, log, log, log, logp, logp, logp, logp, logrb, logrb, logrb, logrb, removeHandler, setFilter, setParent, setUseParentHandlers, severe, throwing
private static final java.lang.String cvsid
CVS id.
public static final boolean DEFAULT_ENABLED
Whether logging is enabled by default.
public static final boolean DEFAULT_USE_VT100ATTRIBS
Whether to use VT100Attribs
by default.
public static final boolean DEFAULT_EXIT_ON_ERROR
Whether to System.exit(-1)
on error(...)
calls by
default.
public static final java.io.OutputStream DEFAULT_OUTPUT_STREAM
The default OutputStream, System.err.
public static final java.util.logging.Level MIN_STACK_TRACE
The minimum level at which a stack trace is included in the log.
public static final java.util.logging.Level LOG_LEVEL_ALL
public static final java.util.logging.Level LOG_LEVEL_SEVERE
public static final java.util.logging.Level LOG_LEVEL_WARNING
public static final java.util.logging.Level LOG_LEVEL_INFO
public static final java.util.logging.Level LOG_LEVEL_CONFIG
public static final java.util.logging.Level LOG_LEVEL_FINE
public static final java.util.logging.Level LOG_LEVEL_FINER
public static final java.util.logging.Level LOG_LEVEL_FINEST
public static final java.util.logging.Level LOG_LEVEL_OFF
public static final java.util.logging.Level[] LEVELS
public static final java.util.logging.Level DEF_LOG_LEVEL
The default log level.
public static final java.util.Map<java.util.logging.Level,VT100Attribs> LEVEL_ATTRIBS
VT100Attribs
for displaying messages on the console at each log
Level.
public static final VT100Attribs SEQUENCE_ATTRIBS
VT100Attribs
for displaying the sequence counter in messages on
the console.
public static final VT100Attribs STACKLEVEL_ATTRIBS
VT100Attribs
for displaying the stacklevel in messages on the
console.
public static final VT100Attribs THROWN_ATTRIBS
VT100Attribs
for displaying Throwables in messages on the
console.
private volatile boolean enabled
Whether logging is enabled on this logger.
Default is DEFAULT_ENABLED
.
private static volatile boolean loggingEnabled
Whether logging is enabled globally.
Default is DEFAULT_ENABLED
.
private static volatile boolean useVT100Attribs
Whether to use VT100Attribs
globally.
Default is DEFAULT_USE_VT100ATTRIBS
.
private volatile boolean exitOnError
Set whether to System.exit(-1)
on error(...)
calls on
this logger.
private static volatile boolean globalExitOnError
Set whether to System.exit(-1)
on error(...)
calls
globally.
private volatile java.util.logging.Level level
The log Level in use by this Log.
private final boolean publishDirect
This is set iff there was a problem (e.g. AccessControlException)
registering handler
when this Log was created. In that case,
log(LogRecord)
will directly publish.
private static java.util.Map<java.lang.String,Log> localLogs
In case we don't have privs to add Logs to the LogManager, e.g. when running as an applet, we put them here.
private static Log.LogHandler handler
Output Handler to console.
private static java.lang.ThreadLocal<java.util.Formatter> formatterTLS
printf
-style formattersprotected Log(java.lang.String name)
Create a Log with the specified name and no resource bundle.
Protected; Logs should be acquired through getLog(java.lang.String)
.
name
- the name of the Logpublic static Log getLog(java.lang.String name)
Get the Log with the specified name.
name
- the name of the Log to get, not nullpublic void setLevel(java.lang.String levelName)
public void setLevel(java.util.logging.Level level)
setLevel
in class java.util.logging.Logger
public boolean isLoggable(java.util.logging.Level level)
level
isLoggable
in class java.util.logging.Logger
public void setEnabled(boolean enabled)
Enable or disable logging on this logger.
Default enable is DEFAULT_ENABLED
.
enabled
- whether logging should be enabledpublic boolean isEnabled()
Check whether logging is enabled on this logger.
Default enable is DEFAULT_ENABLED
.
public static void setLoggingEnabled(boolean enabled)
Enable or disable logging globally.
Default enable is DEFAULT_ENABLED
.
enabled
- whether logging should be enabledpublic static boolean isLoggingEnabled()
Check whether logging is globally enabled.
Default enable is DEFAULT_ENABLED
.
public static void setUseVT100Attribs(boolean useVT100Attribs)
Set whether to use VT100Attribs
(globally) to
make the log messages pretty on the OutputStream.
Default is DEFAULT_USE_VT100ATTRIBS
.
useVT100Attribs
- whether to use VT100Attribs to make the log
messages pretty on the OutputStreampublic static boolean getUseVT100Attribs()
Check whether to use VT100Attribs
(globally) to
make the log messages pretty on the OutputStream.
Default is DEFAULT_USE_VT100ATTRIBS
.
public void setExitOnError(boolean exitOnError)
Set whether to System.exit(-1)
on error(...)
calls on
this logger.
Default is DEFAULT_EXIT_ON_ERROR
.
public boolean getExitOnError()
Get whether to System.exit(-1)
on error(...)
calls on
this logger.
Default is DEFAULT_EXIT_ON_ERROR
.
public static void setGlobalExitOnError(boolean exitOnError)
Set whether to System.exit(-1)
on error(...)
calls
globally.
Default is DEFAULT_EXIT_ON_ERROR
.
public static boolean getGlobalExitOnError()
Get whether to System.exit(-1)
on error(...)
calls
globally.
Default is DEFAULT_EXIT_ON_ERROR
.
public static void setOutputStream(java.io.OutputStream os)
Set the OutputStream to which log messages are issued.
Default is DEFAULT_OUTPUT_STREAM
.
os
- the OutputStream to which log messages are issued, not nullpublic void debug(java.lang.Throwable t)
Wraps debug(Throwable, String, Object...)
.
message
is set to t.getMessage()
.
t
- the Throwable, may be nullpublic void dbg(java.lang.Throwable t)
debug(Throwable)
public void debug(java.lang.String message, java.lang.Object... args)
message
- the messagepublic void dbg(java.lang.String message, java.lang.Object... args)
debug(String, Object...)
public void debug(java.lang.String message)
message
- the messagepublic void dbg(java.lang.String message)
debug(String, Object...)
public void debug()
Wraps debug(Throwable, String, Object...)
.
No throwable and no message are specified.
public void dbg()
debug()
public void debug(java.lang.Throwable t, java.lang.String message, java.lang.Object... args)
Issue a Level.INFO with the specified Throwable and message.
This method will return and the application will continue.
t
- the Throwable, may be nullmessage
- the messageargs
- if non-null and not zero-length, then message is
expanded as a printf
-style format with these argsprotected java.lang.String formatMessage(java.lang.String message, java.lang.Object... args)
If args is non-null and not zero-length, then expand
message as a printf
-style format with these args, else
return message unmodified.
public void dbg(java.lang.Throwable t, java.lang.String message, java.lang.Object... args)
public void gothere()
Issue a "gothere" debug(String)
message.
public void TBD()
Issues a "TBD" warning(String)
.
public void warning(java.lang.Throwable t)
Wraps warning(Throwable, String, Object...)
.
message
is set to t.getMessage()
.
public void warn(java.lang.Throwable t)
warning(Throwable)
public void warning(java.lang.String message, java.lang.Object... args)
public void warning(java.lang.String message)
warning
in class java.util.logging.Logger
public void warn(java.lang.String message, java.lang.Object... args)
public void warn(java.lang.String message)
public void warning()
Wraps warning(Throwable, String, Object...)
.
No throwable and no message are specified.
public void warn()
warning()
public void warning(java.lang.Throwable t, java.lang.String message, java.lang.Object... args)
Like debug(Throwable, String, Object...)
but uses
Level.WARNING.
public void warn(java.lang.Throwable t, java.lang.String message, java.lang.Object... args)
public void error(java.lang.Throwable t)
Wraps error(Throwable, String, Object...)
.
message
is set to t.getMessage()
.
public void err(java.lang.Throwable t)
error(Throwable)
public void error(java.lang.String message, java.lang.Object... args)
public void err(java.lang.String message, java.lang.Object... args)
error(String, Object...)
public void error(java.lang.String message)
public void err(java.lang.String message)
error(String, Object...)
public void error()
Wraps error(Throwable, String, Object...)
.
No throwable and no message are specified.
public void err()
error()
public void error(java.lang.Throwable t, java.lang.String message, java.lang.Object... args)
Like debug(Throwable, String, Object...)
but uses
Level.SEVERE, however, if exitOnError
or globalExitOnError
is set, this method will call System.exit(-1)
before it returns.
public void err(java.lang.Throwable t, java.lang.String message, java.lang.Object... args)
public void log(java.util.logging.LogRecord r)
Issue an arbitrary LogRecord.
Not normally used, instead call one of the debug()
,
warning()
, or error()
methods.
log
in class java.util.logging.Logger
r
- the LogRecord to issue, must actually be an instance of Log.LogRec