Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log4j Settings package lookup failing for a lot of packages #25

Open
billerby opened this issue May 20, 2016 · 4 comments
Open

Log4j Settings package lookup failing for a lot of packages #25

billerby opened this issue May 20, 2016 · 4 comments

Comments

@billerby
Copy link

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

@AntonioSoler
Copy link
Contributor

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
Copy link
Contributor

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.

@billerby
Copy link
Author

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)

@AntonioSoler
Copy link
Contributor

AntonioSoler commented May 31, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants