public class SQLStatementLogger
extends java.lang.Object
directory.xml
at DIR_PATH
location
because, by default, global logging is disabled.
The object itself allows a bit of configuration: with or without a timestamp and the specific one, using
the simple date format.Modifier and Type | Class and Description |
---|---|
private static interface |
SQLStatementLogger.ObjectLogger
Functional interface used for executing special code which appends some output to a
StringBuilder . |
Modifier and Type | Field and Description |
---|---|
private java.util.Map<Database,java.lang.Boolean> |
activeDBs
The set of disabled databases.
|
private java.text.SimpleDateFormat |
dateFormat
The date format to be used.
|
private static java.lang.String |
DIR_PATH
The directory path which contains the global flag for enabling this class.
|
private static SQLStatementLogger |
logger
The static singleton instance.
|
static boolean |
LOGGING_ENABLED
Is this class globally enabled? If not, all API are no-ops and will return quickly.
|
private java.util.Map<Database,java.io.PrintStream> |
streams
The mapped
PrintStream for each Database . |
private boolean |
timestamp
Flag for using the timestamps for each logged event.
|
Modifier | Constructor and Description |
---|---|
private |
SQLStatementLogger()
The private default constructor.
|
Modifier and Type | Method and Description |
---|---|
static SQLStatementLogger |
getLogger()
Static accessor to singleton.
|
void |
log(Database database,
java.sql.Statement statement)
Logs a statement for a database.
|
void |
log(Database database,
java.lang.String statement)
Logs a statement for a database.
|
private void |
log(SQLStatementLogger.ObjectLogger log,
Database database)
Log something using a
Database as reference. |
void |
setEnabled(Database database,
boolean enable)
Enable or disable output for a single database.
|
void |
setStream(Database database,
java.io.PrintStream ps)
Configures that all logs for a
Database to be output to a special PrintStream . |
void |
setTimestamp(boolean timestamp,
java.lang.String dateFormat)
Configures whether logs for a
Database to be output using a timestamp, using an optional date
format. |
private void |
simplify(java.lang.String statement,
java.lang.StringBuilder sb)
Simplifies a statement in order to fit on a single line.
|
private static final SQLStatementLogger logger
public static final boolean LOGGING_ENABLED
private java.text.SimpleDateFormat dateFormat
"yyyy-MM-dd HH:mm:ss.SSS"
.private final java.util.Map<Database,java.io.PrintStream> streams
PrintStream
for each Database
.private final java.util.Map<Database,java.lang.Boolean> activeDBs
private boolean timestamp
private static final java.lang.String DIR_PATH
public static SQLStatementLogger getLogger()
SQLStatementLogger
singleton instance.public void setEnabled(Database database, boolean enable)
database
- The reference database.enable
- Use true
to enable output for this database, or false
to mute it.public void setTimestamp(boolean timestamp, java.lang.String dateFormat)
Database
to be output using a timestamp, using an optional date
format. This configuration affects all databases.timestamp
- A boolean for enabling (true
) or disabling prepending the timestamp to output.dateFormat
- The optional format. If null
, the default of "yyyy-MM-dd HH:mm:ss.SSS"
will be
used.public void setStream(Database database, java.io.PrintStream ps)
Database
to be output to a special PrintStream
.database
- The reference Database
.ps
- The PrintStream
to be used. If null
is passed, the System.out
will be
used.public void log(Database database, java.lang.String statement)
Database
are disabled the method will
return without printing anything.database
- The reference Database
.statement
- The to be described. It will simplified to take just a simple line.public void log(Database database, java.sql.Statement statement)
Database
are disabled the method will
return without printing anything. It will use the toString()
method of the Statement
.database
- The reference Database
.statement
- The Statement
to be described.private void log(SQLStatementLogger.ObjectLogger log, Database database)
Database
as reference. The method use the settings specific to this
database, as previously set. If the LOGGING_ENABLED
is not active, this method will not be
called at all.
When called the first time for a specific database, it will query the registry to see whether that
database has the logging logic enabled. This flag will be saved for further calls. It can be
programmatically altered by using setEnabled()
API.
log
- The special code to be printed.database
- The reference Database
.private void simplify(java.lang.String statement, java.lang.StringBuilder sb)
statement
- The statement to be printed. It eventually takes multiple lines.sb
- The StringBuilder
to append the result to.