Logging and tracing
P4Java includes a simple logging callback feature, documented in the
ILogCallback
Javadoc page, that enables consumers to log
P4Java-internal errors, warnings, informational messages, exceptions, and
so on. Logging is enabled or disabled on a P4Java-wide basis, not on a
per-connection or per-server basis.
The logging feature performs no message formatting or packaging. You can put the log message through the surrounding application context’s logger as required. In general, your applications should log all error and exception messages. Informational messages, statistics, and warning messages do not need to be logged unless you are working with Perforce Technical Support to debug an issue.
-
For more information about logging and tracing, see ILogCallback and ILogCallback.LogTraceLevel in the P4Java API Javadoc.
-
To log all commands sent to the Helix Server, see ICommandCallback in the P4Java API Javadoc.
The snippet below is taken from the LogDemo sample app and illustrates a typical usage pattern for ILogCallback:
public static class LogCallback implements ILogCallback {
private LogTraceLevel traceLevel = LogTraceLevel.NONE; // Don't want tracing...
private PrintStream outStream = null;
public LogCallback(PrintStream outStream) {
this.outStream = outStream;
}
public void setTracelLevel(LogTraceLevel traceLevel) {
this.traceLevel = traceLevel;
}
public LogTraceLevel getTraceLevel() {
return this.traceLevel;
}
public void internalError(String errorString) {
if (this.outStream != null) {
}
}
public void internalException(Throwable thr) {
if (thr != null) {
printMessage(thr.getLocalizedMessage(), "EXCEPTION");
thr.printStackTrace(this.outStream);
}
}
public void internalInfo(String infoString) {
printMessage(infoString, "INFO");
}
public void internalStats(String statsString) {
printMessage(statsString, "STATS");
}
public void internalTrace(LogTraceLevel traceLevel, String traceMessage) {
// Note: tracing does not work for normal P4Java GA releases, so you
// should not see any calls to this method unless you have a "special"
// release...
printMessage(traceMessage, "TRACE");
}
public void internalWarn(String warnString) {
printMessage(warnString, "WARNING");
}
private void printMessage(String msg, String pfx) {
if (msg != null) {
this.outStream.println(new Date() + " (" + pfx + "): " + msg);
}
}
}