2 Replies Latest reply on Feb 28, 2007 1:52 AM by anil_jmit

    log4j statements in drools

    anil_jmit

      Hi Guys
      I am very new in drools.
      I wanted to use log4j statements in drools file.
      Please send me code snippet or any advice,how to use log4j

      Its very urgent.

      Thanks in advance
      Anil Aggarwal

        • 1. Re: log4j statements in drools
          srobert

          You can initialize the log4j logger object in your
          class and then add it to the ApplicationData. Now you can use the
          logger from your .drl-file.

          Here the example java class:

          public class MyClass
          {
          private static Logger _logger = Logger.getLogger( MyClass.class );

          public MyClass()
          {
          initRuleEngine();
          }

          public void initRuleEngine( String ruleFile )
          {
          _logger.info( "Initialization of Drools rule engine..." );
          URL ruleURL = this.getClass().getClassLoader().getResource(ruleFile);
          try
          {
          URL ruleURL =
          this.getClass().getClassLoader().getResource( ruleFile );
          if ( ruleURL != null )
          {
          ruleBase = RuleBaseLoader.loadFromUrl( ruleURL );

          if ( ruleBase != null )
          {
          _logger.info( "Rule set loaded from drools config
          file: " + ruleFile );
          workingMemory = ruleBase.newWorkingMemory();
          workingMemory.addEventListener( wmListener );

          //put logger reference to application data
          workingMemory.setApplicationData("logger", _logger);
          }
          else
          {
          _logger.fatal( "Rule set couldn't be loaded!" );
          }
          }
          }
          catch ( DroolsException e )
          {
          _logger.fatal( "Drools exception occured", e );
          // _logger.fatal( e.getCause() );
          // _logger.fatal( e.getMessage() );
          // _logger.fatal( e.getStackTrace() );
          }
          catch ( IOException e )
          {
          _logger.fatal( "I/O Exception occured", e );
          // _logger.fatal( e.getCause() );
          // _logger.fatal( e.getMessage() );
          // _logger.fatal( e.getStackTrace() );
          }
          catch ( SAXParseException e )
          {
          _logger.fatal( "Drools rule definition file seems to be corrupt",
          e );
          }
          catch ( Exception e )
          {
          _logger.fatal( "Exception occured", e );
          // _logger.fatal( e.getCause() );
          // _logger.fatal( e.getMessage() );
          // _logger.fatal( e.getStackTrace() );
          }
          }
          }

          Now the fragment of a .drl-file:

          <?xml version="1.0" encoding="UTF-8"?>
          <rule-set name="MyRules" xmlns="http://drools.org/rules"
          xmlns:java="http://drools.org/semantics/java"
          xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
          xs:schemaLocation="http://drools.org/rules rules.xsd
          http://drools.org/semantics/java java.xsd">

          <!--Required imports-->
          org.drools.examples.state.State
          ...

          <!--Application data variables-->
          <application-data
          identifier="logger">org.apache.log4j.Logger</application-data>
          ...

          <!--check BarBaz attribute-->


          Foo

          <java:condition>foo != null</java:condition>
          <!--Check the barBaz field-->
          <java:condition>foo.getBarBaz()== 0 </java:condition>
          <java:consequence>
          logger.error("BarBaz is null!" );
          ...
          </java:consequence>

          </rule-set>

          code with "Juri Albert"

          • 2. Re: log4j statements in drools
            anil_jmit

            Thanks a lot Albert
            This code is very helpful.

            -Anil Aggarwal