Write to log file from Java library?

I'm calling an external java library from one of my scripts, and I would like to do a little debugging with it. Is it possible to write to my current session's log file from that library? Maybe if I could pass the session object to the library? Any help is appreciated.

Like shadders has pointed

Like shadders has pointed out, the key is of course in the session object, where you can publicly access the methods "log", "logInfo", "logWarn", and "logError", passing the text to be written.

The only simple way to get a handle on the session is to pass it as a variable into your class method/constructor. Avoiding this would mean that you would have to have the sever mode running, and you'd have to connect to it remotely, and try to get the session object from it. ... but you probably won't be in such a situation if you're debugging :)

Tim

Thanks!

Perfect, thanks! I wasn't sure if the session object was included in that jar file.

I'm doing something quite

I'm doing something quite similar. Yes you can pass the session object. Here's part of a screen-scraper helper class I use a fair bit. I like to write a lot of my code in netbeans then move it to screen-scraper. This allows me to log either using screen-scraper logging or with System.out.print if I'm running from outside screen-scraper but with a single method so I don't have alter the code when I move it. It basically mirror's the 5 log methods available to the session object. It can either be instantiated with 'session' as an input or without.

so basically from within screenscraper you would use:
import etc...
ssTools tools = new ssTools(session);

then you can see from the class code how it's using the session object in the logCommon method which pretty much mirrors what you are wanting to do...


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package csvtools;

/**
*
* @author git
*/
import com.screenscraper.scraper.ScrapingSession;
import com.screenscraper.scraper.ScrapingSessionManager;
import com.screenscraper.common.DataSet;
import com.screenscraper.common.DataRecord;
import java.util.*;

public class ssTools {

public static final int LOG_LEVEL_SYSTEM = 0;
public static final int LOG_LEVEL_DEBUG = 1;
public static final int LOG_LEVEL_INFO = 2;
public static final int LOG_LEVEL_WARN = 3;
public static final int LOG_LEVEL_ERROR = 4;
public static final int LOG_LEVEL_TIME = 5;
public static final int LOG_LEVEL_DATETIME = 6;
public static final int LOG_LEVEL_RUNTIME = 7;
public ScrapingSession session = null;
public Boolean insideScraper = false;
public Integer defaultLogLevel = LOG_LEVEL_SYSTEM;
public boolean printLevelDefault = true;

private void ssTools(ScrapingSession currSession, int defaultLogLevel) {
this.session = currSession;
this.insideScraper = true;
this.defaultLogLevel = defaultLogLevel;
}

public ssTools(ScrapingSession currSession) {
ssTools(currSession,LOG_LEVEL_DEBUG);
}

public ssTools() {
}

private void logCommon(String logText,Integer logLevel,boolean printLevel) {

switch (logLevel.intValue()) {
case ssTools.LOG_LEVEL_SYSTEM: System.out.println(logText); break;
case ssTools.LOG_LEVEL_DEBUG: {
if (insideScraper)
this.session.logDebug(logText);
else
System.out.println("DEBUG: " + logText);
} break;
case ssTools.LOG_LEVEL_INFO: {
if (insideScraper)
this.session.logInfo(logText);
else
System.out.println("INFO: " + logText);
} break;
case ssTools.LOG_LEVEL_WARN: {
if (insideScraper)
this.session.logWarn(logText);
else
System.out.println("WARN: " + logText);
} break;
case ssTools.LOG_LEVEL_ERROR: {
if (insideScraper)
this.session.logError(logText);
else
System.out.println("ERROR: " + logText);
} break;
case ssTools.LOG_LEVEL_TIME: {
if (insideScraper)
this.session.logCurrentTime();
else
System.out.println("current time");
} break;
case ssTools.LOG_LEVEL_DATETIME: {
if (insideScraper)
this.session.logCurrentDateAndTime();
else
System.out.println("current date and time");
} break;
case ssTools.LOG_LEVEL_RUNTIME: {
if (insideScraper)
this.session.logElapsedRunningTime();
else
System.out.println("current running time");
} break;
default: System.out.println(logText);
}
}
private void logCommon(String logText,Integer logLevel) {
this.logCommon(logText,logLevel,printLevelDefault);
}
public void log(Object logText) {
if (logText == null)
logText = "";
if (insideScraper)
logCommon(logText.toString(),defaultLogLevel);
else
logCommon(logText.toString(),defaultLogLevel);
}
public void logDebug(Object logText) {
if (insideScraper)
logCommon(logText.toString(),LOG_LEVEL_DEBUG);
else
logCommon("DEBUG: " + logText,LOG_LEVEL_SYSTEM);
}
public void logInfo(Object logText) {
if (insideScraper)
logCommon(logText.toString(),LOG_LEVEL_INFO);
else
logCommon("INFO: " + logText,LOG_LEVEL_SYSTEM);
}
public void logWarn(Object logText) {
if (insideScraper)
logCommon(logText.toString(),LOG_LEVEL_WARN);
else
logCommon("WARN: " + logText,LOG_LEVEL_SYSTEM);
}
public void logError(Object logText) {
if (insideScraper)
logCommon(logText.toString(),LOG_LEVEL_ERROR);
else
logCommon("ERROR: " + logText,LOG_LEVEL_SYSTEM);
}
}