Skip to content

Androlog configuration

cescoffier edited this page Jan 6, 2011 · 3 revisions

This page explains how to configure Androlog.

Why do I have to configure ?

By default, on a normal phone, Androlog does not log anything... This is perfect for released application which does not pollute the logcat output. Moreover, this is a requirement in the Android release process.

Androlog configuration can be done in two way:

  • with external files (recommended)
  • with the API

Configuring Androlog with external files

Androlog configuration files are just Java properties file (see java.util.Properties). Configurations are placed in the root of the SDCard of the phone or in the assets of the application. This later case is used for production configuration (especially for the reporting configuration). The configuration contained in the assets is ignore if a configuration is present on the SDCard.

The file name are either androlog.properties, a custom name, your application package name according to the init method that you use.

Initializing logging

In all the entry point of your application, you must call Log.init. This will initialize the logging and configure it. There are three init methods:

  • init() : read the /sdcard/androlog.properties file
  • init(name) : read the /sdcard/name.properties file
  • init(context) : read the /sdcard/application_package.properties file. The application package is the package declared in the AndroidManifest file.

Note: To support configuration from the assets, the init method arguments must contain the Android context.

If the read file does not exist, the logging is disabled, which means, on regular phone, it is disabled.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.init(this); // Use the context method

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);
        // ....
    }
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.init(); // Use the androlog.properties file

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);
        // ....
    }
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.init("my-file"); // Use the my-file.properties file

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);
        // ....
    }

All entry points must call the init method. Init methods can be called from Activities, Content Providers, Services ...

Configuring logging

Once initialized, the logging need to be configured. As said previously, the configuration files are simple java properties. A couple of 'special' keys allows global configuration:

  • androlog.active = true|false : enables or disabled the logging. This is a mandatory property which must be set to true to enable the logging. If set to false, nothing will be logged.
  • androlog.default.level=VERBOSE|DEBUG|INFO|WARN|ERROR : Sets the default log level. If not set, INFO is used.

Others keys (which does nto start with androlog.) are tag names. It allows setting a specific log level for a specific tag:

  • my_tag_1 = VERBOSE
  • my.androlog.logger = INFO

The last configuration key is the configuration of the wtf delegation. On Android 2.2, those methods may cause the application to terminate. To avoid this behavior, you can set androlog.delegate.wtf to false.

Exemple of configurations

The minimum configuration to enable logging

androlog.active=true
The minimum configuration disabling logging
androlog.active=false

Set a default log level

androlog.active=true
androlog.default.level=WARN

Set a default log level and some specific levels

androlog.active=true
androlog.default.level=WARN
my.logger=INFO
de.akquinet.android.MyActivity=DEBUG

Troubleshooting

My logging are never displayed: Check that the init method are really called, and the associated file exist and contains androlog.active=true.

How do I disable the logging before releasing: You don't need, as the configuration file will probably not be present on a regular phone

Configuring Androlog with the API

Despite not recommended, you can configure Androlog from your code. However, this requires that you disable the logging yourself before releasing. Androlog provides methods to enable/disable the logging, set the default log level, configure the level of tags:

  • activateLogging(): activate the logging
  • deactivateLogging(): deactivate the logging
  • reset(): reset the configuration (disables the logging, clear the tag configurations, set the default log level to info)
  • setDefaultLogLevel(int logLevel) : sets the default log level
  • configure(Properties configuration): reads the configuration from the given properties file. This properties obey to the same rules as the the one listed above (file configuration).
  • setWTFDelegation(boolean delegation): enables or disables the delegation to android wtf method. This method returns the status of delegation (boolean). Indeed, on Android 1.6 and 2.0, the delegation cannot be enabled, so this method will always return false.