public class Log extends Object
get
method.
In this way, any portion of the application can use logging without
having to pass around a reference to the primary Log.
Note: Applications can use the constructor if they require another Log instance.
The following sample code provides an example of how applications can use the Log class:
// get logfile from app properties String logfile = "myLogFile"; Log.get().setOutput(new FileOutputStream(logfile)); Log.get().setLoggingLevel(Log.ADMIN_LEVEL); // setup on-screen tracing in addition to logging Log.get().setTeeOutput(System.out); try { // create a user session } catch (IfsException ifse) { Log.get().println("Invalid user credential", Log.USER_LEVEL); Log.get().printStackTrace(ifse, Log.DEBUG_LEVEL); }
Modifier and Type | Field and Description |
---|---|
static int |
ADMIN_LEVEL
ADMIN logging level: displays messages from the USER logging level
as well as warning messages.
|
static int |
DEBUG_LEVEL
DEBUG logging level: displays messages from the ADMIN logging level
as well as informational and debugging messages.
|
static int |
USER_LEVEL
USER logging level: displays critical messages and errors.
|
Constructor and Description |
---|
Log()
Creates a new Log instance.
|
Modifier and Type | Method and Description |
---|---|
void |
dispose() |
static Log |
get()
Returns the shared Log instance.
|
int |
getLoggingLevel()
Returns the logging level for this Log.
|
PrintWriter |
getOutput()
Returns this Log's primary output.
|
PrintWriter |
getTeeOutput()
Returns this Log's secondary output.
|
boolean |
isLogged(int level) |
void |
log(int level,
String prefix,
String message) |
void |
log(int level,
String prefix,
String message,
Throwable t) |
void |
log(int level,
String message,
Throwable t) |
void |
log(Object message)
Calls println(Object).
|
void |
log(Object message,
boolean tagTimestamp)
Calls println(Object, boolean).
|
void |
log(Object message,
boolean tagTimestamp,
int level)
Calls println(Object, boolean, int).
|
void |
log(Object message,
int level)
Calls println(Object, booelan int).
|
void |
logTimestamp()
Calls printlnTimestamp.
|
void |
print(Object object)
Prints the string representation of the given object without a newline.
|
void |
print(Object message,
boolean tagTimestamp)
Prints an optionally timestamped message to the log
without printing a newline.
|
void |
print(Object message,
boolean tagTimestamp,
int level)
Prints an optionally timestamped message to the log
without printing a newline.
|
void |
println()
Prints a newline to the log.
|
void |
println(Object object)
Prints the string representation of the given object and a newline.
|
void |
println(Object message,
boolean tagTimestamp)
Prints an optionally timestamped message and newline to the log.
|
void |
println(Object message,
boolean tagTimestamp,
int level)
Prints an optionally timestamped message and newline to the log.
|
void |
printlnTimestamp()
Prints a timestamp and newline to the log.
|
void |
printStackTrace(Throwable t)
Prints a stack trace to the log.
|
void |
printStackTrace(Throwable t,
int level)
Prints a stack trace to the log.
|
void |
printTimestamp()
Prints a timestamp to the log without printing a newline.
|
void |
processLog(LogProcesser logProcesser)
Perform a "long or complex" operation on the log file
|
void |
setDateFormat(DateFormat format)
Sets the DateFormat used for timestamps.
|
void |
setLoggingLevel(int level)
Sets the logging level for this Log.
|
void |
setLogLevel(int level) |
void |
setOutput(PrintStream out)
Sets this Log's primary output.
|
void |
setOutput(PrintWriter writer)
Sets this Log's primary output.
|
void |
setTeeOutput(PrintStream out)
Sets this Log's secondary output.
|
void |
setTeeOutput(PrintWriter writer)
Sets this Log's secondary output.
|
void |
swapLog(String logFileName,
String tempName,
String backupName)
Swap the log file so we can start using a new one
|
public static final int USER_LEVEL
public static final int ADMIN_LEVEL
public static final int DEBUG_LEVEL
public static Log get()
public void setDateFormat(DateFormat format)
public PrintWriter getOutput()
public void setOutput(PrintWriter writer)
writer
- the PrintWriter where this Log's output will be sent.public void setOutput(PrintStream out)
out
- the PrintStream where this Log's output will be sent
(usually System.out).public PrintWriter getTeeOutput()
public void setTeeOutput(PrintWriter writer)
writer
- the PrintWriter where this Log's output will be sent.public void setTeeOutput(PrintStream out)
out
- the PrintStream where this Log's output will be sent
(usually System.out).public int getLoggingLevel()
public void setLoggingLevel(int level)
level
- a logging level constant:
USER_LEVEL, ADMIN_LEVEL, or DEBUG_LEVEL.public void log(Object message)
public void log(Object message, boolean tagTimestamp)
public void log(Object message, int level)
public void log(Object message, boolean tagTimestamp, int level)
public void logTimestamp()
public void printTimestamp()
public void printlnTimestamp()
public void printStackTrace(Throwable t)
t
- the Throwable whose stack trace to print.public void printStackTrace(Throwable t, int level)
t
- the Throwable whose stack trace to print.level
- the logging level for this message.public void print(Object object)
public void println()
public void println(Object object)
public void print(Object message, boolean tagTimestamp)
message
- the message to print.tagTimestamp
- true if a timestamp should be printed.public void println(Object message, boolean tagTimestamp)
message
- the message to print.tagTimestamp
- true if a timestamp should be printed.public void print(Object message, boolean tagTimestamp, int level)
message
- the message to print.tagTimestamp
- true if a timestamp should be printed.level
- the logging level for this message.public void println(Object message, boolean tagTimestamp, int level)
message
- the message to print.tagTimestamp
- true if a timestamp should be printed.level
- the logging level for this message.public void dispose()
public boolean isLogged(int level)
public void setLogLevel(int level)
public void processLog(LogProcesser logProcesser)
logProcesser
- object that implements the LogProcesser interfacepublic void swapLog(String logFileName, String tempName, String backupName) throws IOException
logFileName
- name of the current log filetempName
- file that contains the modified contents (new file)backupName
- name for the current log file after rotationIOException
Copyright © 2023. All rights reserved.