Groovy Script Logging to seperate file

Hi

 

I''ve developed a number of EPI groovy scripts and have configured logging using

private static Log s_log

which is logging to the log/opr-scripting-host/opr-scripting-host.log file.

 

I wanted to create my own log file for each groovy script for better logging/easier troubleshooting.  I'm wondering if anyone could provide an example on how to do this?

 

Thanks in advance!

Mick

Parents Reply Children
  • Verified Answer

    Hi Micky,

     

    as PatWest mentioned, the secret is in file <HPBSM>\conf\core\Tools\log4j\opr-scripting-host.properties
     
    Assume your EPI script is defined as follows (I justed started with it, so it doesn't do anything useful ...):


    import java.util.List;
    import com.hp.opr.api.scripting.Event;
    import org.apache.commons.logging.LogFactory;
    import org.apache.commons.logging.Log;

     

    class SGTest
    {
      private static Log s_log = LogFactory.getLog(SGTest);

      def init()
      {
       s_log.error("SGTest - init" )
       s_log.info("SGTest : Loglevel set to INFO" )
       s_log.warn("SGTest : Loglevel set to WARN" )
       s_log.error("SGTest : Loglevel set to ERROR" )
       s_log.debug("SGTest : Loglevel set to DEBUG" )
      }

      def destroy()
      {
       s_log.error("SGTest - destroyed" )
      }

      def process(List<Event> events)
      {
        s_log.error("SGTest - process" )
        // Example: Title of all events are prefixed with "Modified by CA/EPI: "
        //
        events.each {
        event -> event.setTitle("Modified by CA/EPI: " event.getTitle())
        }


     if (s_log.isInfoEnabled())
      {
        s_log.error("SGTest: INFO is enabled")
      }

     if (s_log.isErrorEnabled())
      {
        s_log.error("SGTest : ERROR is enabled")
      }

     if (s_log.isDebugEnabled())
      {
        s_log.error("SGTest : DEBUG is enabled")
      }

      Integer i = 5
      s_log.error("SGTest : the result is " i )

      }
    }

     

    Now you need to add to file <HPBSM>\conf\core\Tools\log4j\opr-scripting-host.properties

     

    log4j.category.SGTest=${loglevel}, SGTest.appender
    log4j.additivity.SGTest=false

     

    log4j.appender.SGTest.appender=org.apache.log4j.RollingFileAppender
    log4j.appender.SGTest.appender.File=${topaz.home}/${log.file.path}/opr-SGTest.log
    log4j.appender.SGTest.appender.MaxFileSize=${def.file.max.size}
    log4j.appender.SGTest.appender.MaxBackupIndex=${def.files.backup.count}
    log4j.appender.SGTest.appender.layout=org.apache.log4j.PatternLayout
    log4j.appender.SGTest.appender.layout.ConversionPattern=%d [%t] %-5p %c{1}.%M(%L) - %m  %n

     

    save the file, wait 1-2 minutes, then force the execution of your script (by sending a test event, for example):

     

    then you'll see:

     

    E:\HPBSM\log\opr-scripting-host>dir /b
    opr-event-sync-adapter.log
    opr-scripting-host.log
    opr-scripting-host.log.1
    opr-scripting-host.log.2
    opr-scripting-host.log.3
    opr-sg.log
    scripts.log
    spring-framework.log

     

    and opr-sg.log contains - my log statements:


    2013-12-11 15:23:31,874 [pool-5-thread-1] ERROR SGTest.invoke(?) - SGTest - process 
    2013-12-11 15:23:31,875 [pool-5-thread-1] ERROR SGTest.invoke(?) - SGTest: INFO is enabled 
    2013-12-11 15:23:31,875 [pool-5-thread-1] ERROR SGTest.invoke(?) - SGTest : ERROR is enabled 
    2013-12-11 15:23:31,875 [pool-5-thread-1] ERROR SGTest.invoke(?) - SGTest : the result is 5 
    ..
    2013-12-11 15:25:12,850 [pool-5-thread-1] ERROR SGTest.invoke0(?) - SGTest - process 
    2013-12-11 15:25:12,881 [pool-5-thread-1] ERROR SGTest.invoke0(?) - SGTest: INFO is enabled 
    2013-12-11 15:25:12,882 [pool-5-thread-1] ERROR SGTest.invoke0(?) - SGTest : ERROR is enabled 
    2013-12-11 15:25:12,883 [pool-5-thread-1] ERROR SGTest.invoke0(?) - SGTest : the result is 5 

     

    The connection is

     

    script:
    ..
    class SGTest
    {
      private static Log s_log = LogFactory.getLog(SGTest);
    ..

     

    .properties file
    ..
    log4j.category.SGTest=${loglevel}, SGTest.appender
    ..

     

    For a second script it works the similar way:

     

    class SGTest234
    {
      private static Log s_log = LogFactory.getLog(SGTest234);

     

    log4j.category.SGTest234=${loglevel}, SGTest234.appender
    log4j.additivity.SGTest234=false

     

    log4j.appender.SGTest234.appender=org.apache.log4j.RollingFileAppender
    log4j.appender.SGTest234.appender.File=${topaz.home}/${log.file.path}/opr-SGTest234.log
    log4j.appender.SGTest234.appender.MaxFileSize=${def.file.max.size}
    log4j.appender.SGTest234.appender.MaxBackupIndex=${def.files.backup.count}
    log4j.appender.SGTest234.appender.layout=org.apache.log4j.PatternLayout
    log4j.appender.SGTest234.appender.layout.ConversionPattern=%d [%t] %-5p %c{1}.%M(%L) - %m  %n

     

    Note: after modifying the .properties file that way, you no longer will see script output being logged to

     opr-scripting-host.log

    while script complication errors are still logged to

     scripts.log

     

    Greetings

    Siggi 

  • Hi Siggy

     

    that works perfectly

     

    Thanks for the sample!

    Mick

  • Hi Pat

     

    Thanks for the pointer, that all makes sense.

     

    Mick

  • we're glad to help, and remember ...
    "If you find that this or any post resolves your issue, please be sure to mark it as an accepted solution."
  • @Siggi Gladitsch - Thanks for such a wonderfull response. i have further question.

    what happenes if one upgraded the OMi/BSM? am i going to loose the entries from this property file or they remains?

    ThanK You.

    -KAKA-

  • @Siggi Gladitsch - Thanks for such a wonderfull response. i have further question.

    what happenes if one upgraded the OMi/BSM? am i going to loose the entries from this property file or they remains?

    ThanK You.

    -KAKA-

  • @Siggi Gladitsch - Thanks for such a wonderfull response. i have further question.

    what happenes if one upgraded the OMi/BSM? am i going to loose the entries from this property file or they remains?

    ThanK You.

    -KAKA-

  • @Asaf Shechter @Siggi Gladitsch - Another question i have is how can i write info, warn and error type of message altogether? for example: i want to write the normal Informations to check the flow of my script and for error handling i want to write the Warn or Error which may be i can monitor later using the agent.

    pronblem i see here is that although the log has all type of statements i.e s_log.info ("it is info"), s_log.error("X was unexpected at this time") and so on

    log output is showing only what is set in the loglevel.

    is there any solution for this?

    -KAKA-