Add custom log in replication agents log file using Interface ReplicationTransaction | Community
Skip to main content
Uppari_Ramesh
Level 5
June 21, 2023
Solved

Add custom log in replication agents log file using Interface ReplicationTransaction

  • June 21, 2023
  • 3 replies
  • 3166 views

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 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

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 } } }

3 replies

MayurSatav
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 21, 2023

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();

 

Mayur Satav | www.mayursatav.in
Uppari_Ramesh
Level 5
June 21, 2023

Hi @mayursatav ,

 

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

 

MayurSatav
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 21, 2023

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.

Mayur Satav | www.mayursatav.in
lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
June 21, 2023

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 } } }
Uppari_Ramesh
Level 5
June 21, 2023

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.

 

 

Uppari_Ramesh
Level 5
June 21, 2023

Hi @lukasz-m @tanika02 @mayursatav ,

 

Thanks for the suggestions!!