如果字符串没有缩进,可以使用 Golang 的 split 函数进行分割。

huangapple go评论90阅读模式
英文:

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$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)

我想根据下一行是否不是制表符字符来拆分和分组。在 JavaScript 中,我可以在 split 方法中使用正则表达式,但是 Golang 中的 strings.Split 函数只接受字符串作为分隔符。我尝试过的是:

strings.SplitAfter(log, ")")

但是上面的代码没有完全捕获到 SEVERE 状态。我正在考虑循环遍历每个字符,并查找字母字符并将其附加到切片中,但我认为这样做会太慢。有没有其他方法可以实现?

英文:

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?

答案1

得分: 2

你可以使用正则表达式来实现这个功能,以下是一个示例代码 playground

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

(请注意,上述代码中使用的是空格而不是制表符,因为我从你的问题中复制的数据是空格)。

另外,你也可以编写自己的代码;标准库的源代码是一个很好的起点 good starting point

英文:

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.

huangapple
  • 本文由 发表于 2022年4月5日 05:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/71744102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定