Saturday, July 25, 2015

LibGDX - Logging facilities

In this post I will describe the two logging facilities provided by LibGDX, Logger class and Application logging interface.

Application logging interface

Application class offers several static method to send log messages with different priorities:

  • debug (LOG_DEBUG)
  • error (LOG_ERROR)
  • log  (LOG_INFO)
You can set the log level using the setLogLevel(int logLevel) API; to disable logs, it is sufficient to set LOG_NONE.

debug, error and log methods can accept two or three parameters:
  • a tag (String), to identify the component that it is logging and simplify the filter of the log output;
  • a message (String), with the useful information
  • an exception (Throwable) - opt parameter, this exception will be raised if the after a check among the message log level and the current log level.
In your application you can access log facilities using this syntax:

Gdx.app.debug("LoggApp", "Test message");

You can find more information here:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Application.html

Example

You can add this lines to the create() method of Logging class from the previous example (http://mytechpg.blogspot.ch/2015/07/libgdx-howto-create-your-first-project.html)
Gdx.app.setLogLevel(Gdx.app.LOG_INFO);
Gdx.app.debug("logging", "Debug level log");
Gdx.app.log("logging", "Info level log");
Gdx.app.error("logging", "Error level log");
Gdx.app.debug("logging", "Debug level log + exception", 
new Exception("debug level"));
Gdx.app.error("logging", "Error level log + exception", 
new Exception("error level"));

Since the log level is info, debug messages will be ignored. Here you can see the output:

logging: Error level log
logging: Info level log
logging: Error level log + exception
java.lang.Exception: error level
  at com.blogspot.mytechpg.libgdx.test1.Logging.create(Logging.java:24)
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

As expected, the exception is generated with the error method.

To download the source code repo:
27224b84e463d5ed9099bc085113ef5d8a1ddb43
git clone -b dev https://github.com/mytechpg/libgdx-samples.git

To select the correct release:
git checkout 19af8c7478e8671ac5ebe83a26790c419a9ab686

Logger class

This class uses the Application logging facilities and offers the same methods with a similar syntax.
It is possible configure the tag and the log level using the class constructor.

You can also use setLevel() method to configure the log level. If you use Application.setLogLevel() method you will override the value configured using Logger APIs.
debug, error and info methods accept one or two parameters:
  • a message (String)
  • an exception (Throwable) - opt parameter
You can find more information here:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Logger.html





No comments:

Post a Comment