Adding Logger Calls to Your Code

The One Network SDK gives you access to the One Network Logger. So far we've been looking at how to view and change log message detail levels. It seems fitting to now discuss how to add logging messages to your code, and how to target the different log levels we've been discussing.

You get some logging automatically. For example if you use SqlDefs in your code, you will automatically have access to the SQL in the DEBUG_SQL log level without adding anything to your code.

For the most part, however, it is advisable to add logging messages based on specific log levels, particularly when those messages are meant for development and debugging purposes and would otherwise be out of place in a system administrator's log.

You can add a logger to any class you create. To start, import the logger class.

import com.onenetwork.platform.tools.log.PlatformLogger;

Next create a static instance of the logger as an instance variable within your class.

public static PlatformLogger logger = PlatformLogger.get(YourClass.class);

Replace the YourClass in YourClass.class with the actual name of your class. The rest is very straightforward. You can log messages to any log level using the methods for each level. For example, if you want a message to appear only if the log level is set to DEBUG, you could use something like this:

log.debug("This is a debug message. You must be in DEBUG mode to see it.");