Forum Discussion

sairamau's avatar
sairamau
New Contributor
7 years ago

Groovy Script To Read Pattern in a logfile, extract data and do date calculation

I have a log file which come as an attachment for my soap response and the file has below content

I am trying to write a groovy script which will :-

Open the log file which is an attachment in my soap response

Read the file Extract "[TIMESTAMP] - 2018-02-08" above the line ending "Audit event Individual_Duplicate_Check_Referred - End"

Once i get the TIMESTAMP, i need to convert it to No.Of Days (SysDate-TIMESTAMP)

 

Below is my logfile content

 

2018-02-26 10:41:21,076 [KilimWorker-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [VARCHAR] - REFER
2018-02-26 10:41:21,077 [KilimWorker-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [7] as [VARCHAR] - PRE_REFER
2018-02-26 10:41:21,077 [KilimWorker-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [8] as [TIMESTAMP] - 2018-02-08 23:00:52
2018-02-26 10:41:21,078 [KilimWorker-4] DEBUG com.vedaadvantage.dp3.manager.actor.Message - Audit event Individual_Duplicate_Check_Referred - End
2018-02-26 10:41:21,078 [KilimWorker-4] DEBUG com.vedaadvantage.dp3.manager.actor.DispatcherActor - execute

 

  • PaulMS's avatar
    PaulMS
    Super Contributor
    def logfile = '''2018-02-26 10:41:21,076 [KilimWorker-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [6] as [VARCHAR] - REFER
    2018-02-26 10:41:21,077 [KilimWorker-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [7] as [VARCHAR] - PRE_REFER
    2018-02-26 10:41:21,077 [KilimWorker-4] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [8] as [TIMESTAMP] - 2018-02-08 23:00:52
    2018-02-26 10:41:21,078 [KilimWorker-4] DEBUG com.vedaadvantage.dp3.manager.actor.Message - Audit event Individual_Duplicate_Check_Referred - End
    2018-02-26 10:41:21,078 [KilimWorker-4] DEBUG com.vedaadvantage.dp3.manager.actor.DispatcherActor - execute
    '''
    
    logfile.eachLine { currentLine, lineNumber ->
        if (currentLine.contains( 'Individual_Duplicate_Check_Referred' )) {
            previousLine = logfile.readLines()[lineNumber - 1]
        }
    }
    
    def timeStamp = previousLine.split(' - ').last();
    def Date ts = new Date().parse("yyyy-MM-dd HH:mm:ss", timeStamp)
    log.info new Date() - ts
    • sairamau's avatar
      sairamau
      New Contributor

      Thanks Paul, i will give a go with this, will update you