[Building Sakai] runtime config of log4j

John Bush john.bush at rsmart.com
Tue May 8 22:11:05 PDT 2012


this is bad ass, if you didn't know it you can change log levels at
runtime.  Add this to your SakaiScript.jws

   public String adjustLogLevel(String sessionid, String packageName,
String level) throws AxisFault {
       Session session = establishSession(sessionid);
       if (!securityService.isSuperUser()) {
           LOG.warn("NonSuperUser trying to collect configuration: "
+ session.getUserId());
           throw new AxisFault("NonSuperUser trying to collect
configuration: " + session.getUserId());
       }
       Properties props = new Properties();
       try {

           // ok, yes I know this is fragile and totally tomcat
specific, but it works and avoids having to
           // configure up a new version of the log4j file somehow.
For sure this will break in tomcat 6, as
           // I think classloading is much different there.
           // This finds the log4j file in kernel common:
           // common/lib/sakai-kernel-common-x.x.x.jar!/log4j.properties
           InputStream configStream =
this.getClass().getClassLoader().getParent().getParent().getParent().getResourceAsStream("log4j.properties");
           props.load(configStream);
           configStream.close();
           props.setProperty("log4j.logger." + packageName, level);
           LogManager.resetConfiguration();
           PropertyConfigurator.configure(props);
       } catch (Exception e) {
           e.printStackTrace();
       }

       return "success";
   }

The call the webservice via a browser or however you like to do that
(using an active sessionid of course):

http://localhost:8888/sakai-axis/SakaiScript.jws?method=adjustLogLevel&sessionid=9a17024e-7430-4d69-998e-83a6f7d35cab&packageName=org.springframework&level=DEBUG

and viola you can adjust log level at runtime.   Pretty sweet.

Try it out.

-- 
John Bush
602-490-0470


More information about the sakai-dev mailing list