Forum Discussion

rdebie's avatar
rdebie
Contributor
17 years ago

Export http log to a file with groovy script

Hello,

Can i export the log window 'http log' to a file.
I want to do this with a groovy script.

regards,

Raymond Wiertz

5 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi!

    sure.. you can loop the elements in the model we accessed in the previous script and write them to a file:

    def logArea = com.eviware.soapui.SoapUI.logMonitor
    if( logArea != null )
    {
      def ix = logArea.indexOfTab( "http log" );
      if( ix >= 0 )
      {
          def logPanel = logArea.getComponentAt( ix )
          def model = logPanel.logList.model
          if( model.size > 0 )
          {
            def out = new java.io.PrintWriter( "myfile.log" )
       
            for( c in 0..(model.size-1) )
                out.println( model.getElementAt( c ))

            out.close()
          }
      }
    }


    if you want to make it really fancy you can prompt for the file:


    def logArea = com.eviware.soapui.SoapUI.logMonitor
    if( logArea != null )
    {
      def ix = logArea.indexOfTab( "http log" );
      if( ix >= 0 )
      {
          def logPanel = logArea.getComponentAt( ix )
          def model = logPanel.logList.model
          if( model.size > 0 )
          {
            def file = com.eviware.soapui.support.UISupport.fileDialogs.saveAs(
                null, "Export Log", "*.log", "*.log", null )
            if( file != null )
    {
                def out = new java.io.PrintWriter( file )
       
                for( c in 0..(model.size-1) )
                  out.println( model.getElementAt( c ))

                out.close()
            }
          }
      }
    }


    regards!

    /Ole
  • Oke,

    great , can i do this also for the Test log?

    def ix = logArea.indexOfTab( "test log" );

    does this works also?

    Raymond Wiertz
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    if you mean the log in the groovy-editor window, the answer is no.. there is no way to currently access that log.. but all the other logs at the bottom are available.. actually, you can even add your own from a script;

    def logArea = com.eviware.soapui.SoapUI.logMonitor
    if( logArea != null )
    {
      def ix = logArea.indexOfTab( "my log" );
      def logPanel

      if( ix == -1 )
      {
          logPanel = logArea.addLogArea( "My log", "my.log.category", false )
      }
      else
      {
          logPanel = logArea.getComponentAt( ix )
      }

      logPanel.addLine( "test" )
    }


    Then in subsequent teststeps/testcases you can log to this with

    def mylog = org.apache.log4j.Logger.getLogger( "my.log.category" )
    mylog.info( "testing.." )


    Since this is standard log4j stuff, you could add an SNMP appender for monitoring, JDBC appender for logging to database, etc..

    regards!

    /Ole
    eviware.com
  • Oke,

    Tanks for your support.

    regards,

    Raymond.


    This is really a great functionality   
  • We will be writing a tutorial in the upcoming weeks for what Ole described. Using the SMTPappender (sending mail when a test fails) or NTEventLogappender (Writing to the Windows Event Viewer to be handled by Service Management Software) is really useful.

    /niclas