Home > Net >  Golang split string if there is no indentation
Golang split string if there is no indentation

Time:04-05

I have a log file like this.

INFO [com.cplus.sdk.iso.module] 02:55:24.8290 19.02.2022 @ com.cplus.sdk.iso.Module
  [Message] message sent
  [Source] [Protocol:TranzWare-InterConnect (v.2.115-11);Client:[Transport:TCP/IP;To:/127.0.0.1:50274;From:/127.0.0.1:4455;KeepAlive:true;Framer:L2 L1 <###>]]
  [Thread] IA-Pool-Thread-18 (30)
INFO [com.cplus.sdk.net] 02:55:25.7840 19.02.2022 @ com.cplus.sdk.net.Client
  [Message] connection closed
  [Source] [Transport:TCP/IP;Closed;To:/172.30.250.35:57001;From:/10.1.180.54:49851;KeepAlive:true;Framer:(Len 1) L2 L1 <###> ETX]
  [Thread] IA-Pool-Thread-4 (16)
SEVERE [com.cplus.ia] 02:55:25.9530 19.02.2022 @ com.cplus.ia.Instance$InterfaceListener$2
  [Message] interface processing error
  [Source] Instance:[UnitID:11276;External:{ExternalHost:[Protocol:Alto-Rtt (v.1.0);Interface:ISO-8583 Based Host-To-Host Interface]}]
  [Thread] IA-Pool-Thread-4 (16)
  [Exception] com.cplus.sdk.ex.ProcessorException com.cplus.ia.tic.Interface.handle <- com.cplus.sdk.iso.Processor.process <- com.cplus.ia.tic.Interface.process <- com.cplus.sdk.iso.Module.process <- com.cplus.ia.Instance$InterfaceListener$2.run <- java.util.concurrent.Executors$RunnableAdapter.call[Executors.java<511>] <- java.util.concurrent.FutureTask.run[FutureTask.java<266>] <- java.util.concurrent.ThreadPoolExecutor.runWorker[ThreadPoolExecutor.java<1149>] <- java.util.concurrent.ThreadPoolExecutor$Worker.run[ThreadPoolExecutor.java<624>] <- com.cplus.ia.Instance$ApplicationThread.run <- java.lang.Thread.run[Thread.java<748>]
    [Text] unhandled processing error
    [Cause] com.cplus.sdk.ex.NetworkException com.cplus.sdk.iso.Module.send <- com.btpn.alto.rtt.TicInterface.msgHandler[TicInterface.java<329>] <- com.btpn.alto.rtt.TicInterface.onFinancial[TicInterface.java<183>] <- com.cplus.ia.tic.Interface.handle <- com.cplus.sdk.iso.Processor.process <- com.cplus.ia.tic.Interface.process <- com.cplus.sdk.iso.Module.process <- com.cplus.ia.Instance$InterfaceListener$2.run <- java.util.concurrent.Executors$RunnableAdapter.call[Executors.java<511>] <- java.util.concurrent.FutureTask.run[FutureTask.java<266>] <- java.util.concurrent.ThreadPoolExecutor.runWorker[ThreadPoolExecutor.java<1149>] <- java.util.concurrent.ThreadPoolExecutor$Worker.run[ThreadPoolExecutor.java<624>] <- com.cplus.ia.Instance$ApplicationThread.run <- java.lang.Thread.run[Thread.java<748>]
      [Cause] java.nio.channels.InterruptedByTimeoutException com.cplus.sdk.net.FramesHelper.checkDeadline <- com.cplus.sdk.net.FramesHelper$IO.write <- com.cplus.sdk.net.Frames$Frame27.write <- com.cplus.sdk.net.Client.write <- com.cplus.sdk.iso.Module$ModuleTCP.sendMessage <- com.cplus.sdk.iso.Module.send <- com.btpn.alto.rtt.TicInterface.msgHandler[TicInterface.java<329>] <- com.btpn.alto.rtt.TicInterface.onFinancial[TicInterface.java<183>] <- com.cplus.ia.tic.Interface.handle <- com.cplus.sdk.iso.Processor.process <- com.cplus.ia.tic.Interface.process <- com.cplus.sdk.iso.Module.process <- com.cplus.ia.Instance$InterfaceListener$2.run <- java.util.concurrent.Executors$RunnableAdapter.call[Executors.java<511>] <- java.util.concurrent.FutureTask.run[FutureTask.java<266>] <- java.util.concurrent.ThreadPoolExecutor.runWorker[ThreadPoolExecutor.java<1149>] <- java.util.concurrent.ThreadPoolExecutor$Worker.run[ThreadPoolExecutor.java<624>] <- com.cplus.ia.Instance$ApplicationThread.run <- java.lang.Thread.run[Thread.java<748>]
FINE [com.cplus.sdk.net] 05:27:34.0200 27.03.2022 @ com.cplus.sdk.net.Client
  [Message] frame sent: QTRNMTAwMDAwODAwODIyMDAwMDAwMDAwMDAwMDA0MDAwMDAwMDAwMDAwMDAwMzI2MjIyNzM0MzAwNzkzMzAx
  [Source] [Transport:TCP/IP;To:/127.0.0.1:56573;From:/127.0.0.1:2221;KeepAlive:true;Framer:L2 L1 <###>]
  [Thread] IA-Pool-Thread-2 (45)
INFO [com.cplus.sdk.iso.module] 05:27:34.0200 27.03.2022 @ com.cplus.sdk.iso.Module
  [Message] message sent
  [Source] [Protocol:TranzWare-InterConnect (v.2.115-11);Client:[Transport:TCP/IP;To:/127.0.0.1:56573;From:/127.0.0.1:2221;KeepAlive:true;Framer:L2 L1 <###>]]
  [Thread] IA-Pool-Thread-2 (45)

I want to split and group it based on if the next line is not a \t character. In javascript I can use regex in split method, but golang Split in strings built-in function only receive string as separator. What I've tried so far was

strings.SplitAfter(log, ")")

But the code above doesn't capture the SEVERE state completely. I'm thinking to loop through each character and looking for alphabet character and append it to a slice but I think it will be too slow. Is there any way to do it?

CodePudding user response:

You can use a regex to do this if you want; something like the following should work playground

regexp.MustCompile("([^ ].*[\r|\n].(?: .*[\r|\n]*)*)").FindAllString(input, -1)

(Note that the above uses spaces rather than tabs because that's what I got when I copied the data from your question).

Alternatively you can write your own code; the source of the standard library would be a good starting point.

  • Related