Anvil
How-to guides

Open API: log from a plugin

CATIA Magic runs on Log4j, and a plugin writes into the tool's own log by taking a logger under a category it names itself. Nothing about this reaches the Message Window.

Take a logger

import org.apache.logging.log4j.LogManager

private val log = LogManager.getLogger("ACME")

log.info("export finished: {} diagrams", count)

Name the category for the resource, short and symbolic — ACME rather than a class or a package name. The tool prints it as %c, a reader greps for it, and one configuration entry raises it.

The Developer Guide's own sample imports org.apache.log4j.Logger, which is Log4j 1. The tool ships Log4j 2, configured through <install>/data/log4j2.xml, so bind org.apache.logging.log4j as above.

Build no string nobody reads

The tool's root logger sits at info, so a debug or a trace call costs nothing while nobody is listening — provided the line is not already built. Use a parameterised message, or guard the call:

log.debug("candidates: {}", candidates)
if (log.isTraceEnabled) log.trace("full state: ${expensive()}")

Raise a category

<install>/data/log4j2.xml carries monitorInterval="30", so the tool rereads it within thirty seconds and no restart is needed:

<Logger name="ACME" level="debug"/>

The root logger stays where it is, so raising one category floods nothing else.

What not to reach for

println writes to a console nobody running the tool is watching. GUILog.log(String) opens the notification window on every call — see Open API: show a notification.

Reference

  • Developer Guide — General, "Information logging"
  • Configuration — <install>/data/log4j2.xml
  • API — Log4j 2, org.apache.logging.log4j
  • Local digest — dev-env/ai-docs/reference/guide/information-logging.md

On this page