Logging Module

The Logging module allows XWiki components to receive logs as events and to isolate logs in for a specific task without them appearing in the standard log. The Logging module was designed to work with the Logging Application. The source code of the UI is published on GitHub.

The available logging levels are: TRACE, DEBUG, INFO, WARN and ERROR.

The Logging module does not provide logs for Components. To do do, you need to inject a SLF4J Logger, as explained in the dedicated documentation page.

The source code for the Logging module API is also published on GitHub

Receive Logs as Events

In order for a component to receive logs, it must listen to events of type LogEvent as shown in the below code snippet. More details are available in the documentation page about the Observation Module.

import org.xwiki.logging.LogLevel;
import org.xwiki.logging.event.AbstractLogEventListener;
import org.xwiki.observation.event.Event;

@Component
@Named("CustomLogListener")
@Singleton
public class CustomLogListener extends AbstractLogEventListener
{
  @Override
  public String getName()
  {
      return "CustomLogListener";
  }

  @Override
  public void onEvent(Event event, Object source, Object data)
  {
        LogEvent logEvent = (LogEvent) event;

      if (logEvent.getLevel() == LogLevel.ERROR) {
            System.err.println(logEvent.getFormattedMessage());
      } else if (logEvent.getLevel() == LogLevel.WARN) {
            System.err.println(logEvent.getFormattedMessage());
      } else if (logEvent.getLevel() == LogLevel.INFO) {
            System.out.println(logEvent.getFormattedMessage());
      } else if (logEvent.getLevel() == LogLevel.DEBUG) {
            System.out.println(logEvent.getFormattedMessage());
      } else if (logEvent.getLevel() == LogLevel.TRACE) {
            System.out.println(logEvent.getFormattedMessage());
      }
  }
}

Isolate Error Logs

To isolate specific logs, like for instance error messages, you can use the LoggerManager component.

LogQueue queue = new LogQueue();

loggerManager.pushLogListener(new LogQueueListener("loglistenerid", queue));
logger.error("Some application error happen");
loggerManager.popLogListener();

The pushLogListener() method retrieves the logs generated by the current thread and sends them to the provided listener. The logs will then be received as XWiki events.

The popLogListener() method removes the current listener from the current thread stack and returns the previous log event listener for the current thread. In case several listeners have been pushed, the previous one becomes active.

Modify the Registered Loggers

Retrieve the registered loggers

To get all registered loggers, you could use the code: Collection<Logger> loggers = loggerManager.getLoggers();

Get the log level of a package

To retrieve the log level for the package org.apache.solr.core.SolrCore, you could use: loggerManager.getLoggerLevel("org.apache.solr.core.SolrCore")

Set the log level for a package

To set the DEBUG level for the package org.apache.solr.core.SolrCore, you could use: loggerManager.setLoggerLevel("org.apache.solr.core.SolrCore", LogLevel.DEBUG)

Translate Logs

To be able to provide a translation key for logging, you could use the SLF4J Marker interface which allows logging listeners that receive logging events.  These ones will use a localization framework that will convert the translation key to the translated version.

Example:

// Log producer
logger.error(new TranslationMarker("myapplication.error"), "An error has occurred");
// Log displayer

#set($translatedLogEvent = $services.logging.translate($logEvent))
$translatedLogEvent.formattedMessage

Organize a Set of Logs as a Tree

You can organize a set of logs as a tree by using the following LogEvent markers:

  • public static final Marker MARKER_BEGIN = MarkerFactory.getMarker("xwiki.begin");
  • public static final Marker MARKER_END = MarkerFactory.getMarker("xwiki.end");
logger.info(LogEvent.MARKER_BEGIN, "Start the task");
logger.error("Failed execute part of the task");
logger.info(LogEvent.MARKER_END, "Task is finished but failed");

logger.info(LogEvent.MARKER_BEGIN, "Start a new task");
logger.info(LogEvent.MARKER_END, "Task successfully finished");

You can also use LogTree in order to interpret such a log and get helpers to navigate into the tree.

The logging script service also provide a helper to convert a log list into a tree: #set($logtree = $services.logging.toLogTree($logs))

 

Related Pages

Search this space

 

Most popular tags

Failed to execute the [groovy] macro
  1. access rights
  2. activity stream
  3. annotation
  4. attachment
  5. comment
  6. Document Tree Macro
  7. export
  8. Extension Manager
  9. Flamingo skin
  10. global user
  11. Groovy event listener
  12. group
  13. nested page
  14. search
  15. skin
  16. syntax
  17. user
  18. user profile
  19. velocity macros
  20. wiki
  21. wysiwyg
  22. XWiki Applications
  23. xwikiattachment_archive table
  24. xwikiattachment table
  25. xwikiattrecyclebin table
  26. xwikiproperties table

[Display all tags from this space]