Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Add custom log in replication agents log file using Interface ReplicationTransaction

Avatar

Level 4

Hi Everyone,

 

I have created new replication agent named TestAgent. I have a requirement where after successful replication action I want to print my own error/info log in TestAgent's log file.

 

I am triggering this replication agent via custom workflow process step. Which service do I need to use to print additional logs in TestAgent's log file.

I see we can use Interface ReplicationTransaction, how can we make use of this interface and call getLog() method of this interface. If there are any other ways , please suggest. 

 

Thanks!!

@kautuk_sahni @lukasz-m 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Uppari_Ramesh,

ReplicationTransaction is not exposed as an OSGi service, you can't get the instance of this class via @Reference - so the fact is not working for you is expected.

Nevertheless I do not think you need ReplicationTransaction explicitly.

In terms of agent related logs you should use ReplicationLog class.

It contains methods to access logs and append your own messages into log.

You can get ReplicationLog object, from Agent class via getLog() method. You can combine it with ReplicationListener which will be run once replication process is complete.

Here is an example code:

import com.day.cq.replication.*;

@Component(
        service = WorkflowProcess.class,
        property = "process.label=Custom Process"
)
public class CustomWorkflowProcess implements WorkflowProcess {

    @Reference
    private Replicator replicator;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {

        // set your won agent id
        AgentIdFilter agentIdFilter = new AgentIdFilter("publish");
        ReplicationOptions replicationOptions = new ReplicationOptions();
        replicationOptions.setFilter(agentIdFilter);
        // set this option to use Replication Listener
        replicationOptions.setSynchronous(true);
        replicationOptions.setListener(new CustomReplicationListener());

        try {
            replicator.replicate(null, ReplicationActionType.ACTIVATE, "/content/we-retail/language-masters/en/test2", replicationOptions);
        } catch (ReplicationException e) {
            e.printStackTrace();
        }

    }

    private class CustomReplicationListener implements ReplicationListener {

        @Override
        public void onStart(Agent agent, ReplicationAction replicationAction) {
            // place for your code
        }

        @Override
        public void onMessage(ReplicationLog.Level level, String s) {
            // place for your code
        }

        @Override
        public void onEnd(Agent agent, ReplicationAction replicationAction, ReplicationResult replicationResult) {
            if (replicationResult.isSuccess()) {
                // place for logging after replication is completed
                agent.getLog().info("Replication completed");
            }
        }

        @Override
        public void onError(Agent agent, ReplicationAction replicationAction, Exception e) {
            // place for your code
        }
    }
}

View solution in original post

11 Replies

Avatar

Community Advisor

Hi @Uppari_Ramesh ,

In your custom workflow process step, obtain a reference to the ReplicationTransaction object. You can do this by injecting the ReplicationTransaction service using the @Reference annotation.

@Reference
private ReplicationTransaction replicationTransaction;

After the replication action is successfully executed, you can access the log object using the getLog() method of the ReplicationTransaction interface.

Logger log = replicationTransaction.getLog();

 

Avatar

Level 4

Hi @MayurSatav ,

 

I tried this before and getting unsatisfied reference if we use this interface via @reference annotation 

Uppari_Ramesh_0-1687327921047.png

 

Avatar

Community Advisor

Seems it is not properly registered. Could you pleas check the required bundle or package containing the com.day.cq.replication.ReplicationTransaction interface is correctly installed and active in your AEM instance. You can also try obtaining a reference to the ReplicationTransaction service using the OSGi ServiceTracker instead.

Avatar

Level 4

Hi @MayurSatav ,

 

Actually  com.day.cq.replication pacakge is part of uber jar.

Avatar

Community Advisor

Hello @Uppari_Ramesh  - 

 

Are you doing something similar ?

import com.day.cq.replication.ReplicationTransaction;
import com.day.cq.replication.ReplicationTransactionLog;

public class CustomWorkflowProcessStep {

    public void execute(ReplicationTransaction transaction) {
        ReplicationTransactionLog log = transaction.getLog();
        if (log != null) {
            log.info("Additional info log message");
            log.error("Additional error log message", new Exception("Optional exception"));
        }
    }
}

 

 

Avatar

Level 4

Hi @Tanika02 ,

 

No, in my implementation I am not reading ReplicationTransaction object as method parameter. Instead in my process step I want to create instance of ReplicationTransaction either via @Reference annotation or some other way. I tried via @Reference annotation but getting unsatisfied reference.

 

Avatar

Community Advisor

Also, @Uppari_Ramesh  - 

 

Have customized the replication agent configuration?

 

Meaning in the AEM configuration, locate the configuration for your replication agent (TestAgent) and specify your custom ReplicationTransactionLog implementation.

 

 

 

<TransportHandler>
    <!-- Other configuration properties -->
    <logClass>com.yourpackage.CustomReplicationTransactionLog</logClass>
</TransportHandler>

 

 

 

If you have not, you can do this as stated below : 

 

  1. Locate the configuration file: The replication agent configurations are typically stored in the AEM repository under the /etc/replication/agents path. Look for the configuration file associated with your TestAgent.

  2. Open the configuration file: The file should have an .agent.xml extension. For example, TestAgent.agent.xml.

  3. Find the <TransportHandler> element: Inside the configuration file, locate the <TransportHandler> element. It contains various properties related to the replication agent's transport handler.

  4. Add the <logClass> element: Within the <TransportHandler> element, add a new <logClass> element and specify the fully qualified class name of your custom ReplicationTransactionLog implementation.

Avatar

Correct answer by
Community Advisor

Hi @Uppari_Ramesh,

ReplicationTransaction is not exposed as an OSGi service, you can't get the instance of this class via @Reference - so the fact is not working for you is expected.

Nevertheless I do not think you need ReplicationTransaction explicitly.

In terms of agent related logs you should use ReplicationLog class.

It contains methods to access logs and append your own messages into log.

You can get ReplicationLog object, from Agent class via getLog() method. You can combine it with ReplicationListener which will be run once replication process is complete.

Here is an example code:

import com.day.cq.replication.*;

@Component(
        service = WorkflowProcess.class,
        property = "process.label=Custom Process"
)
public class CustomWorkflowProcess implements WorkflowProcess {

    @Reference
    private Replicator replicator;

    @Override
    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {

        // set your won agent id
        AgentIdFilter agentIdFilter = new AgentIdFilter("publish");
        ReplicationOptions replicationOptions = new ReplicationOptions();
        replicationOptions.setFilter(agentIdFilter);
        // set this option to use Replication Listener
        replicationOptions.setSynchronous(true);
        replicationOptions.setListener(new CustomReplicationListener());

        try {
            replicator.replicate(null, ReplicationActionType.ACTIVATE, "/content/we-retail/language-masters/en/test2", replicationOptions);
        } catch (ReplicationException e) {
            e.printStackTrace();
        }

    }

    private class CustomReplicationListener implements ReplicationListener {

        @Override
        public void onStart(Agent agent, ReplicationAction replicationAction) {
            // place for your code
        }

        @Override
        public void onMessage(ReplicationLog.Level level, String s) {
            // place for your code
        }

        @Override
        public void onEnd(Agent agent, ReplicationAction replicationAction, ReplicationResult replicationResult) {
            if (replicationResult.isSuccess()) {
                // place for logging after replication is completed
                agent.getLog().info("Replication completed");
            }
        }

        @Override
        public void onError(Agent agent, ReplicationAction replicationAction, Exception e) {
            // place for your code
        }
    }
}

Avatar

Level 4

Hi @lukasz-m ,

 

The approach you proposed is working fine for me. Thanks!!

 

Solution:

From the Agent we can get log as below,

agent.getLog().info("your message");

Now we can see the above info log in agent's log file(agent.log.html).

 

Also we can print this log based on ReplicationStatus i.e., if replicationStatus.isDelivered() true then I will print my own success info log.