如果没有缩进,Golang 拆分字符串

Golang split string if there is no indentation

我有一个这样的日志文件。

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
  [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.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.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.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)

我想根据下一行不是 \t 字符来拆分和分组它。在 javascript 中,我可以在 split 方法中使用正则表达式,但是 golang Split in strings 内置函数只接收字符串作为分隔符。到目前为止我尝试过的是

strings.SplitAfter(log, ")")

但是上面的代码并没有完全捕获 SEVERE 状态。 我正在考虑遍历每个字符并寻找字母字符并将其附加到一个切片,但我认为它会太慢。 有什么办法吗?

如果需要,您可以使用正则表达式来执行此操作;像下面这样的东西应该工作 playground

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

(请注意,上面使用空格而不是制表符,因为这是我从您的问题中复制数据时得到的)。

或者您可以编写自己的代码;标准库的来源是 good starting point.