alfresco-support-tools icon indicating copy to clipboard operation
alfresco-support-tools copied to clipboard

Log4j Settings package lookup failing for a lot of packages

Open billerby opened this issue 8 years ago • 4 comments

Tried changing log level but the package lookup fails for a lot of packages. For example, setting "org.alfresco.repo.web.scripts.activities.feed" to DEBUG gives error message: "08:56:20 - Failed: package org.alfresco.repo.web.scripts.activities.feed not found".

Looked through the source code and it seems to be something in the jmx.queryMBeans function that fails to find the package (line 36 of admin-log-settings.post.js Tried:

var matchingBeans = jmx.queryMBeans("log4j:logger=" + "org.alfresco.repo.web.scripts.activities.feed");

Which returns an empty ScriptMBean.

Addressing a class in the package works in this case:

var matchingBeans = jmx.queryMBeans("log4j:logger=" + org.alfresco.repo.web.scripts.activities.feed.UserFeedRetrieverWebScript");

billerby avatar May 20 '16 07:05 billerby

hi, I'm testing an alternative way to add the packages, Not sure if it would work on all JVMs

could you please test replacing the source code of admin-log-settings.post.js with this:


<import resource="classpath:alfresco/enterprise/webscripts/org/alfresco/enterprise/repository/admin/admin-common.lib.js">

function main(){
  var matchingBeans = jmx.queryMBeans("Alfresco:Name=Log4jHierarchy");
  var packagename = "";
  var priority = "";
  // locate POST fields
  for each (field in formdata.fields)
  {
    if (field.name == "packagename")
    {
      packagename = field.value;
      logger.log("package: " + packagename);
    }
    else if (field.name == "priority")
    {
      priority = field.value;
      logger.log("priority: " + priority);
    }
  }

  //send back logger beans
  model.matchingBeans = jmx.queryMBeans("log4j:logger=*");

  // send tools id for the left hand navigation
  model.tools = Admin.getConsoleTools("admin-log-settings");

  // set the info for the current server
  model.metadata = Admin.getServerMetaData();

  //add the package and priority via JMX
  if (packagename != "" && priority != "")
  {
    var bean = matchingBeans[0];



    //bean.operations.addLoggerMBean(packagename);
    addLogger();



    matchingBeans = jmx.queryMBeans("log4j:logger=" + packagename);
    bean = matchingBeans[0];
    if (bean != null)
    {
      bean.attributes["priority"].value = priority;
      jmx.save(bean);
      model.messageStatus = "";
      model.statusMessage = "Logger " + packagename + " set to level " + priority;
    } else
    {
      model.messageStatus = "error";
      model.statusMessage = "Failed: package " + packagename + " not found";
      return;
    }
    // should check the result is valid
  } else
  {
      model.messageStatus = "error";
      model.statusMessage = "Failed: no package name provided";
  }
}




function addLogger(){

    var LOGGER = Packages.org.apache.log4j.Logger;
    var LEVEL = Packages.org.apache.log4j.Level;
    var LOGMANAGER= Packages.org.apache.log4j.LogManager;

    var LOGGER_OK=0;
    var LOGGER_NOT_SURE=1;
    var LOGGER_KO=2;

    var packageArr = new Array();
    var logLevLArr= new Array();

    packageArr.push(packagename);
    logLevLArr.push(priority);

    for(var i =0; i<packageArr.length;i++){

        if(packageArr[i] != null && packageArr[i] != ""){

            var logName= packageArr[i];
            var logLevel = logLevLArr[i];
            var localLevel = LEVEL.toLevel(logLevel);
            var couple = new Array();
            couple.push(logName);
            couple.push(logLevel);


            var localLogger = LOGMANAGER.exists(logName);

            if(localLogger!=null){

                localLogger.setLevel(localLevel);
                logIt(localLogger);
                result[couple]=LOGGER_OK;
                continue;

            }
            else{

                //check if it's a class -> it means it doesn't have a logger
                try{

                    var classObj = java.lang.Class.forName(logName);
                    result[couple]=LOGGER_KO;
                    continue;
                }
                catch(e){
                }

                var localLogger = LOGGER.getLogger(logName);
                //check if it's a Package
                var packageObj = java.lang.Package.getPackage(logName);

                if(packageObj!=null){
                    localLogger.setLevel(localLevel);
                    logIt(localLogger);
                    result[couple]=LOGGER_OK;
                    continue;
                }

                //if we don't know we set it anyway

                localLogger.setLevel(localLevel);
                logIt(localLogger);
                result[couple]=LOGGER_NOT_SURE;
                continue;


            }
        }
    }
}
main();

AntonioSoler avatar May 25 '16 13:05 AntonioSoler

The problem with the old method is if the package hasn't been defined initially in Log4 there is not an mbean to manipulate it... with this new way we push directly the mbean.

AntonioSoler avatar May 25 '16 13:05 AntonioSoler

Ok, tried it out, first I had to add the packagename and priority to the addLogger function for it to run without exception. Then I got the same failure as before :(

Using the Oracle JVM btw (1.7 on Alfresco 4.2.6)

billerby avatar May 26 '16 09:05 billerby

Ok, I think I got it working now for all occurrences, class names and packages,

replace the three files attached here admin-log-settings.zip in your folder: {alfresco}\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\enterprise\webscripts\org\alfresco\enterprise\repository\admin\support-tools

This is based on the work form my friend and colleague Marco Mancuso: https://github.com/marcomancuso/add-loggers

AntonioSoler avatar May 31 '16 15:05 AntonioSoler