How to enable custom logging in Custom Authentication or AuthenticationFilter

How to enable custom logging in Custom Authentication or AuthenticationFilter

book

Article ID: KB0077121

calendar_today

Updated On:

Products Versions
Spotfire Server 7.8 and above

Description

Below are the steps required to add custom logs to custom Custom Authentication or AuthenticationFilter code.

Issue/Introduction

This article provide the steps to add logging to CustomAuthentication or AuthenticationFiter or CustomWebAuthentication code so to add logs to the server.log when the respective code is executed

Resolution

1. Add the "log4j-api.jar" and "log4j-core.jar" to the project. These can be copied from the tomcat\webapps\spotfire\WEB_INF\lib folder

2. In your custom code, import the necessary LogManager and Logger:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
And at the beginning of the class add:
protected static Logger log = LogManager.getLogger(CustomClassName.class);
Resulting in, for example:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CustomClassName {

protected static Logger log = LogManager.getLogger(CustomClassName.class);
...
}

3. Now you can add logging statements in your code using the below statement
log.debug("In CustomClassName");

4. If this class is added to the package name say "com.spotfire" then there is no need of any configuration and you should see the logs being output to the server.log. However if you use another package names say "com.security.customfilter" then you need to add the following to the Log4j2.xml in the tomcat\spotfire-config\ so that the custom logging is output to server.log
<Logger name="com.security.customfilter" level="DEBUG" additivity="false">
      <AppenderRef ref="serverlog"/>
</Logger>