Skip to content

Conversation

fateh288
Copy link
Contributor

@fateh288 fateh288 commented Jul 31, 2025

What changes were proposed in this pull request?

New REST endpoint to reload logging configuration to avoid restart of service while changing logging level to DEBUG.
Any new logging config as per requirement can be added to logback.xml which is be loaded when REST endpoint is hit and won't require service restart. Alternatively logLevel can be set for a specific package/class via the REST endpoint directly

How was this patch tested?

Manual testing in the docker setup.

  1. Changed log level in /opt/ranger/admin/ews/webapp/WEB-INF/classes/conf/logback.xml to debug
  2. Run curl command curl -u admin:rangerR0cks! -X POST http://localhost:6080/service/loggers/reload
    Run the curl command curl -X POST \ -H "Content-Type: application/json" \ -u "admin:rangerR0cks\!" \ -d '{"loggerName": "org.apache.ranger", "logLevel": "DEBUG"}' \ "http://localhost:6080/service/loggers/set-level"
    Log level for logger 'org.apache.ranger' has been set to 'DEBUG'

Python client testing script

from apache_ranger.client.ranger_client import RangerClient

def test_ranger_client():
    try:
        print("=== Testing Python Ranger Client ===")

        # Create client
        client = RangerClient("http://localhost:6080", ("admin", "rangerR0cks!"))

        # Test reload log configuration
        print("Testing reload log configuration...")
        result1 = client.reload_log_configuration()
        print(f"Reload result: {result1}")

        # Test set log level
        print("Testing set log level...")
        result2 = client.set_log_level("org.apache.ranger", "DEBUG")
        print(f"Set level result: {result2}")

        print("All tests passed!")

    except Exception as e:
        print(f"Test failed: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_ranger_client()

  1. See ranger admin log file which now contains DEBUG logs /var/log/ranger/ranger-admin-ranger.example.com-.log without docker container / ranger admin restart

Attaching screenshot
Screenshot 2025-07-30 at 5 36 44 PM

fateh288 added 2 commits July 30, 2025 17:46
…to dynamically reload log config to avoid service restart
… to avoid compile time dependencies of specific logging frameworks
/**
* REST API for logging-related operations.
*/
@Path("loggers")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this endpoint need authentication and should be allowed only for ranger admin user?
https://github.com/apache/ranger/blob/master/security-admin/src/main/resources/conf.dist/security-applicationContext.xml

@mneethiraj mneethiraj requested a review from Copilot July 31, 2025 17:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new REST endpoint /service/loggers/reload to dynamically reload logging configuration without requiring a service restart. This is particularly useful for changing log levels to DEBUG for troubleshooting purposes.

  • Implements a new REST endpoint that can reload both Log4j2 and Logback configurations
  • Uses reflection to detect and work with different SLF4J logging implementations
  • Provides runtime log configuration reloading capability for Apache Ranger admin service

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
LogLevelREST.java New REST controller providing the /loggers/reload endpoint
RangerLogLevelService.java Service class that handles the actual log configuration reloading using reflection


/**
* An endpoint to dynamically reload the logging configuration from the
* log4j2 properties file on the classpath.
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions 'log4j2 properties file' but the implementation supports both Log4j2 and Logback configurations. The comment should be updated to reflect that it reloads logging configuration from the appropriate configuration file (logback.xml or log4j2 configuration).

Suggested change
* log4j2 properties file on the classpath.
* appropriate configuration file (e.g., logback.xml or a Log4j2 configuration file)
* on the classpath.

Copilot uses AI. Check for mistakes.


// Create a JoranConfigurator instance
Class<?> joranConfiguratorClass = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
Object configurator = joranConfiguratorClass.newInstance();
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newInstance() method is deprecated since Java 9. Consider using joranConfiguratorClass.getDeclaredConstructor().newInstance() instead for better compatibility with newer Java versions.

Copilot uses AI. Check for mistakes.

Comment on lines 96 to 98
java.net.URL configUrl = this.getClass().getClassLoader().getResource("logback.xml");
if (configUrl == null) {
configUrl = this.getClass().getClassLoader().getResource("logback-test.xml");
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded configuration file paths 'logback.xml' and 'logback-test.xml' should be extracted as constants to improve maintainability and make them configurable if needed.

Suggested change
java.net.URL configUrl = this.getClass().getClassLoader().getResource("logback.xml");
if (configUrl == null) {
configUrl = this.getClass().getClassLoader().getResource("logback-test.xml");
java.net.URL configUrl = this.getClass().getClassLoader().getResource(LOGBACK_XML);
if (configUrl == null) {
configUrl = this.getClass().getClassLoader().getResource(LOGBACK_TEST_XML);

Copilot uses AI. Check for mistakes.

Comment on lines 47 to 49
if (loggerFactoryClassName.startsWith("org.apache.logging.slf4j")) {
reloadLog4j2Configuration();
} else if (loggerFactoryClassName.startsWith("ch.qos.logback.classic")) {
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded SLF4J binding class name prefixes should be extracted as constants to improve maintainability and reduce the risk of typos.

Suggested change
if (loggerFactoryClassName.startsWith("org.apache.logging.slf4j")) {
reloadLog4j2Configuration();
} else if (loggerFactoryClassName.startsWith("ch.qos.logback.classic")) {
if (loggerFactoryClassName.startsWith(SLF4J_LOG4J2_PREFIX)) {
reloadLog4j2Configuration();
} else if (loggerFactoryClassName.startsWith(SLF4J_LOGBACK_PREFIX)) {

Copilot uses AI. Check for mistakes.

resetMethod.invoke(context);

// Find the configuration file URL (e.g., logback.xml)
java.net.URL configUrl = this.getClass().getClassLoader().getResource("logback.xml");
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider validating the configuration file path and ensuring it points to an expected location to prevent potential security issues from loading arbitrary configuration files.

Copilot uses AI. Check for mistakes.

…y support logback as logging mechanism for this endpoint
…for a package, client libs for testing log level endpoints
@fateh288 fateh288 changed the title RANGER-5266: [Generated by Gemini AI]: (security-admin) REST endpoint to reload log configuration while service is running RANGER-5266: [Generated by Gemini AI]: (security-admin) REST endpoint to reload log configuration / set log level while service is running Aug 21, 2025
@fateh288 fateh288 changed the title RANGER-5266: [Generated by Gemini AI]: (security-admin) REST endpoint to reload log configuration / set log level while service is running RANGER-5266: [Generated by Gemini AI]: (security-admin) REST endpoints to reload log configuration / set log level while service is running Aug 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants