Skip to content

Logging

Custom server-side code can log information using the API provided by Backendless SDK for Java/Android. The API is modeled after the log4j framework and closely follows it . Consider the following example:

import com.backendless.logging.Logger;
import com.backendless.logging.LogBuffer;

@Asset( "*" ) 
public class GenericTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender <HashMap> 
{ 
  @Override 
  public void beforeCreate( RunnerContext context, HashMap hashmap ) throws Exception 
  {
    // the following line is required to avoid buffering in server code
    LogBuffer.getInstance().setLogReportingPolicy( 1, 0 );

    Logger log = Logger.getLogger( GenericTableEventHandler.class ); 
    log.info( "informational log message" ); 
    log.warn( "warning message" ); 
    log.debug( "debug message" ); 
    log.error( "error message" ); 
  } 
}

It is important to note that using logging in custom server-side code requires a special configuration of the logging policy using the following API. This is required to avoid creation of a log buffer and an additional thread responsible for submission of the log messages to the Backendless servers:

LogBuffer.getInstance().setLogReportingPolicy( 1, 0 );

The sample code above logs four different messages, each for separate logging level. The log levels are organized into a hierarchy:

Log level
Includes messages from log levels
debug
debug, info, warn, error
info
info, warn, error
warn
warn, error
error
error

The log file is saved in the /logging directory of the Backendless File Service storage.

The following logging APIs are available:

Retrieving Logger object:

public static Logger getLogger( Class clazz )

Log a message with the debug level:

public void debug( String message )

Log a message with the info level:

public void info( String message )

Log a message with the warn level:

public void warn( String message )
public void warn( String message, Throwable throwable )

Log a message with the error level:

public void error( String message )
public void error( String message, Throwable throwable )