3.8. Administrative logging

eBox can log every change made through its administrative interface, which is useful to know when something was changed or who changed it. Every eBox module should report every log message to give a comprehensive administrative logging support.

In order to do so, you must include a new package from eBox core EBox::LogAdmin in your module.

Example 3.16. Including LogAdmin package

use EBox::LogAdmin qw ( :all );

A new module attribute named title is used, you must add it to the create call. For instance:

Example 3.17. Creating an object with administrative logging support

my $self = $class->SUPER::_create(name => 'objects',
                    title => __n('Objects'),
                    domain => 'ebox-objects',
                    @_);

Furthermore, a hash is needed in the constructor with the actions your module will report with a nicely formatted message. Following the same example in EBox::Objects module:

Example 3.18. Adding actions to log


$self->{'actions'} = {};
$self->{'actions'}->{'addObject'} = __n('Added object {object}');
$self->{'actions'}->{'addToObject'} =
        __n('Added {name} ({ip}/{mask} [{mac}]) to object {object}');
$self->{'actions'}->{'removeObject'} = __n('Removed object {object}');

	    

For each action you want to log, which should be everything that can be done from web interface, you should follow these steps:

  1. Add an action mark to the function, like:

    sub addObject # (description)
    {
        #action: addObject
    	      
  2. Call logAdminDeferred or logAdminNow. logAdminDeferred puts the action in the database with the field committed set to false, and should be called for actions that don't take place until the changes are saved, that is, anything that gets stored in gconf. When you save changes, the actions are marked as committed, if you revert changes the actions are removed.

    logAdminNow puts the action in the database with the field committed set to true, and should be used for actions that take effect immediately like changing the password or adding a user.

    Both functions have the same signature:

    logAdminDeferred(module,action,params)
    logAdminNow(module,action,params)
    	      

    You should call it only when you're sure that the action has taken place, for example, for addObject:

    Example 3.19. Calling logAdminDeferred method

    $self->set_string("$id/description", $desc);
    logAdminDeferred('objects',"addObject","object=$desc");
    return $id;
                    

    As it is shown, the database stores addObject, object=objname", the displaying code creates the string which should be displayed and translates it to the right language.