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
  • Hi Pat

     

    Not particularly, sorry i probably wasn't specific enough. 

     

    I'm talking about EPI groovy scripts within BSM.  I've got scripts which are logging to opr-scripting-host.log.  But i want to log to a specific file as talked about in the online help:

    Extensibility Guide > Operations Management > Groovy Scripts > Reference Information > The Groovy Console

     

    •To access a custom package you must supply the properties file for your package. The properties files can be found in a sub-directory in the directory conf/core/Tools/log4j. The name of the sub-directory corresponds with the name of the process triggering your script, as detailed under the heading Trigger for each script type described in section Development and Deployment of Scripts.

    I'm not sure what the best approach is to do this, and if overriding the logging properties file with my own will cause any issues. 

     

    Thanks

    Mick

     

  • hi Micky,

     

    If we open opr-scripting-host.properties (attached) from directory ...:\HPBSM\conf\core\Tools\log4j\

     

    You can see the following section :


    ###################################################
    ### define appender opr.scriptinghost.appender ###
    ###################################################
    # opr-epi-server.log is set to be a FileAppender which outputs to opr-epi-server.log
    log4j.appender.opr.scriptinghost.appender=org.apache.log4j.RollingFileAppender
    log4j.appender.opr.scriptinghost.appender.File=${topaz.home}/${log.file.path}/opr-scripting-host.log
    log4j.appender.opr.scriptinghost.appender.MaxFileSize=${def.file.max.size}
    log4j.appender.opr.scriptinghost.appender.MaxBackupIndex=${def.files.backup.count}
    log4j.appender.opr.scriptinghost.appender.layout=org.apache.log4j.PatternLayout
    log4j.appender.opr.scriptinghost.appender.layout.ConversionPattern=%d [%t] %-5p %c{1}.%M(%L) - %m %n

     

    well here is says to what log opr-epi-server should log to, and how.

     

    Check out the following guidelines on the web, to give you an idea or direction how your own Log4j:

     

    http://stackoverflow.com/questions/2716824/where-how-does-log4j-look-for-a-log4j-properties-file

     

     

    http://www.javabeat.net/baisc-steps-to-configure-log4j-using-xml-and-properties-file/#

     

     

     

    Hope this helps,
    PatW

     

  • hi Micky,

     

    If we open opr-scripting-host.properties (attached) from directory ...:\HPBSM\conf\core\Tools\log4j\

     

    You can see the following section :


    ###################################################
    ### define appender opr.scriptinghost.appender ###
    ###################################################
    # opr-epi-server.log is set to be a FileAppender which outputs to opr-epi-server.log
    log4j.appender.opr.scriptinghost.appender=org.apache.log4j.RollingFileAppender
    log4j.appender.opr.scriptinghost.appender.File=${topaz.home}/${log.file.path}/opr-scripting-host.log
    log4j.appender.opr.scriptinghost.appender.MaxFileSize=${def.file.max.size}
    log4j.appender.opr.scriptinghost.appender.MaxBackupIndex=${def.files.backup.count}
    log4j.appender.opr.scriptinghost.appender.layout=org.apache.log4j.PatternLayout
    log4j.appender.opr.scriptinghost.appender.layout.ConversionPattern=%d [%t] %-5p %c{1}.%M(%L) - %m %n

     

    well here is says to what log opr-epi-server should log to, and how.

     

    Check out the following guidelines on the web, to give you an idea or direction how your own Log4j:

     

    http://stackoverflow.com/questions/2716824/where-how-does-log4j-look-for-a-log4j-properties-file

     

     

    http://www.javabeat.net/baisc-steps-to-configure-log4j-using-xml-and-properties-file/#

     

     

     

    Hope this helps,
    PatW

     

  • hi Micky,

     

    If we open opr-scripting-host.properties (attached) from directory ...:\HPBSM\conf\core\Tools\log4j\

     

    You can see the following section :


    ###################################################
    ### define appender opr.scriptinghost.appender ###
    ###################################################
    # opr-epi-server.log is set to be a FileAppender which outputs to opr-epi-server.log
    log4j.appender.opr.scriptinghost.appender=org.apache.log4j.RollingFileAppender
    log4j.appender.opr.scriptinghost.appender.File=${topaz.home}/${log.file.path}/opr-scripting-host.log
    log4j.appender.opr.scriptinghost.appender.MaxFileSize=${def.file.max.size}
    log4j.appender.opr.scriptinghost.appender.MaxBackupIndex=${def.files.backup.count}
    log4j.appender.opr.scriptinghost.appender.layout=org.apache.log4j.PatternLayout
    log4j.appender.opr.scriptinghost.appender.layout.ConversionPattern=%d [%t] %-5p %c{1}.%M(%L) - %m %n

     

    well here is says to what log opr-epi-server should log to, and how.

     

    Check out the following guidelines on the web, to give you an idea or direction how your own Log4j:

     

    http://stackoverflow.com/questions/2716824/where-how-does-log4j-look-for-a-log4j-properties-file

     

     

    http://www.javabeat.net/baisc-steps-to-configure-log4j-using-xml-and-properties-file/#

     

     

     

    Hope this helps,
    PatW

     

  • 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-