How to change slf4j log into html link

568 Views Asked by At

I have java code as:

final URI myUri = new URIBuilder()
                            .setScheme(azkabanHostProtocol)
                            .setHost(azkabanHost + ":" + azkabanPort)
                            .setPath("/executor")
                            .setParameter("execid", executionID).build();

                    logger.info(myUri.toString());

I want to display myURI in form of an url/html link into Azkaban's logs so that by clicking on the url it is opened. I am using log4j for logging.

2

There are 2 best solutions below

0
On

How do you view the Azkaban log files? If they are just raw text files being viewed with a vanilla text editor then there is no way to accomplish what you want. If you are viewing them in a smarter UI then you need to format them according to what that UI requires.

In short, the answer to your question is completely driven by whatever tool you are using to view the logs.

2
On

You may create your own Layout class extending HTMLLayout .

Then override the format method to suit your needs.

The actual implementation has the following lines, that you may want to replace :

sbuf.append(Transform.escapeTags(event.getRenderedMessage()));

See that all tags in the message String, will be escaped by default.

Your version could be based on a kind of marker, say String mark = "[LINK]"; for instance

if(event.getRenderedMessage().startsWith(mark)){
  String uri = event.getRenderedMessage().substring(mark.length());
  String link = "<a href=\"" + uri  + "\">" + uri  + "</a>";
  sbuf.append(link);
}
else
  sbuf.append(Transform.escapeTags(event.getRenderedMessage()));

And you would call the logger this way :

logger.info(mark + myUri.toString());

The following topic will help you use a custom HTMLLayout : how to change htmllayout in log4j2

Here is the source code for the default HTMLLayout, as a starter.