Fluent Commerce Logo
Docs
Sign In
Essential knowledge

Author:

Kirill Gaiduk

Changed on:

10 July 2025

Overview

The `LogUtils` class in the `util-core` bundle provides structured logging helpers that remove boilerplate code when using logging actions within your rules. These utilities make it easy to record key actions, errors, and checkpoints to the event log for debugging and auditing purposes.


Key points

  • Standardized Logging: The utilities provide a standard, structured format for log messages, making the event log easier to read and search.
  • Automatic Titling: The log entry's title is automatically generated from the rule's class name, ensuring consistency and saving you from writing boilerplate code.
  • Simple Checkpoints`logOnce` is an easy way to record a specific outcome or checkpoint in your rule's execution with a formatted message.
  • Collections`logAttributeCollection` is a convenient helper for logging a list of attributes.

Key Concepts

The logging utilities are wrappers around the standard `context.action().log()` method. They provide two main benefits:

  • Automatic Titling: The title of the log entry is automatically generated from the `@RuleInfo` name of the rule class you provide. This creates consistent `LogEvents` which can be searched and filtered through the Event API.
  • Structured Formatting: The methods provide a clear structure for the log's title and subtitle (using `String.format()`), making the event log easier to read and query.

Core Methods

`logOnce`

This method logs a single, formatted message to the event log. It is ideal for recording a specific, meaningful outcome or checkpoint in your rule's execution.

The log title is automatically set to "Rule logs for rule YourRuleName". The subtitle is generated from your format string and arguments.

1import com.fluentcommerce.util.core.LogUtils;
2
3// ...
4
5public class ProcessOrderRule implements Rule {
6    @Override
7    public void run(Context context) {
8        String orderRef = context.getEvent().getEntityRef();
9        String orderStatus = context.getEvent().getEntityStatus();
10        
11        // Log successful order processing with order details
12        LogUtils.logOnce(
13            context,
14            ProcessOrderRule.class,
15            "Successfully processed order %s with status '%s'. Order contains %d line items.",
16            orderRef,
17            orderStatus,
18            getLineItemCount(context)
19        );
20        
21        // ... rest of rule logic
22    }
23    
24    private int getLineItemCount(Context context) {
25        // Implementation to get line item count
26        return 5; // Example value
27    }
28}
`logAttributeCollection`

This method is used to log a `List` of `Attribute` objects. The log title is the same as `logOnce`, and the subtitle is formatted as "LogCollection:[size]", where size is the number of attributes in the list. 

1import com.fluentcommerce.util.core.LogUtils;
2import com.fluentretail.api.model.attribute.Attribute;
3import java.util.List;
4
5// ...
6
7public class LogAttributesRule implements Rule {
8    @Override
9    public void run(Context context) {
10        // Assume 'getAttributesFromSomeplace()' returns a list of attributes
11        List<Attribute> attributesToLog = getAttributesFromSomeplace();
12
13        if (!attributesToLog.isEmpty()) {
14            // Log the entire collection of attributes for debugging
15            LogUtils.logAttributeCollection(
16                context,
17                LogAttributesRule.class,
18                attributesToLog
19            );
20        }
21    }
22}