Logging

From EsWiki

Jump to: navigation, search

This article explains how to enable logging in your Java plugins. Using System.out.println can help you trace to the server console, but this does not get saved in the server/logs/ElectroServer4.log file.

Contents

Add jar

Add the following jar to your Java classpath (so your project will compile). You do not need to add it to the lib folder of your extension because ElectroServer uses it already.

    ''installation folder''/server/lib/slf4j-api-1.4.3.jar

Setup and Configure

There are two ways to use a logger. You can use a separate Logger object, or you can use the ES4 logging framework. Either way works if you are in a class that has access to a getApi() method. If you do not have access to getApi() then use a separate Logger object.

Using the ES4 Logger

In your Java code, add this import:

    import org.slf4j.Logger;

Add a global variable for your logger instance. If you make it private, only the current class has access, which may be all you need.

    private Logger logger;

The easy way to determine the name of the logger is to add these three lines to your plugin, then check the console when you test the plugin:

        logger = getApi().getLogger();
        String logName = logger.getName();
        System.out.println("using logger: " + logName);

Generally, the logger's name for a plugin will be Extensions.extension name.Plugins.plugin name. For example: Extensions.MyExtension.Plugins.MyPlugin.

Next, edit installation folder/server/config/log4j.properties with any text editor. Add a line for each logger variable you use. Replace logName by the name of the logger, as described above.

    log4j.logger.''logName''=info

If you want to add both info and debug level logging lines, you can use

    log4j.logger.''logName''=debug

Separate Logger method

In your Java code, add these imports:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

Add a global variable for your logger instance. If you make it private, only the current class has access, which may be all you need. Replace name of the class by the name of the class containing the logger variable.

    private Logger logger = LoggerFactory.getLogger(''name of the class''.class);

Next, edit installation folder/server/config/log4j.properties with any text editor. Add a line for each logger variable you use. Replace path by the full path for the class, such as com.electrotank.electroserver4.examples.pluginfilesystem.FileSystemPlugin.

    log4j.logger.''path''=info

General use

Back in your Java code, anywhere you want to log information, add logger.info() or logger.debug() lines. For example:

    public ChainAction userEnter(UserEnterContext context) {
        String playerName = context.getUserName();
        '''logger.info(playerName + " entered the room.");'''
        boolean ok = onPlayerEnter(playerName);
        if (ok) {
            '''logger.info(playerName + " joined the game.");'''
            return ChainAction.OkAndContinue;
        } else {
            '''logger.info("Game refused to let " + playerName + " join.");'''
            return ChainAction.Fail;
        }
    }

One handy use of logging for development testing is in a plugin's request method.

    @Override
    public void request(String playerName, EsObjectRO requestParameters) {
        EsObject messageIn = new EsObject();
        messageIn.addAll(requestParameters);
        getApi().getLogger().debug(playerName + " requests: " + messageIn.toString());

This logs all requests. Similarly, you can log all outgoing plugin messages using EsObject.toString(). Extremely useful for debugging!

Finding the Log lines

All the logging lines will appear on the server console, which will likely scroll too fast to be read. The lines will also be saved in installation folder/server/logs/ElectroServer4.log. Each logging line will be marked, similar to these:

Examples using ES4 Logger

   2008-Mar-12 18:26:55:656 - INFO  [main]: testing logger info level
   2008-Mar-12 18:26:55:656 - DEBUG [main]: testing logger debug level
   2008-Mar-12 18:26:55:671 - DEBUG [main]: saveCall making connection
   2008-Mar-12 18:26:57:015 - DEBUG [main]: connection = null? false

Examples using Separate Logger

   2008-Feb-09 07:58:01:718 - INFO  [pool-1-thread-10]: u9516 entered the room.
   2008-Feb-09 07:58:01:734 - INFO  [pool-1-thread-10]: Delayed message g to u9516 queued.
   2008-Feb-09 07:58:01:734 - INFO  [pool-1-thread-10]: Delayed message pl to u9516 queued.
   2008-Feb-09 07:58:01:734 - INFO  [pool-1-thread-10]: u9516 joined the game.
Personal tools
download