Skip to content

用函数的思维解决这个日志转换过滤的问题 #19

@zhongl

Description

@zhongl

假定, tail -f log 会不断输出类似下面日志内容

2013-12-03 10:20:26,889 [nioEventLoopGroup-2-3] DEBUG i.n.handler.logging.LoggingHandler - [id: 0x456ea7b0, /10.68.199.85:36714 => /10.125.48.164:7769] WRITE(42B)
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 34 30 31 0a 76 69 61 3a 74 63 70 3a 2f 2f 31 30 |401.via:tcp://10|
|00000010| 2e 36 38 2e 31 39 39 2e 38 35 3a 33 36 37 31 34 |.68.199.85:36714|
|00000020| 0a 6d 69 64 3a 32 20 30 0a 0a                   |.mid:2 0..      |
+--------+-------------------------------------------------+----------------+
2013-12-03 10:20:26,889 [nioEventLoopGroup-2-3] DEBUG i.n.handler.logging.LoggingHandler - [id: 0x456ea7b0, /10.68.199.85:36714 => /10.125.48.164:7769] FLUSH
2013-12-03 10:20:27,089 [nioEventLoopGroup-2-3] DEBUG i.n.handler.logging.LoggingHandler - [id: 0x456ea7b0, /10.68.199.85:36714 => /10.125.48.164:7769] RECEIVED(406B)

现, 需要写段 scala 脚本, 使得 tail -f log | scala script.scala 后输出:

2013-12-03 10:20:26,889 /10.68.199.85:36714 => /10.125.48.164:7769 WRITE(42B)

401
via:tcp://10.68.199.85:36714
mid:2 0

2013-12-03 10:20:26,889 /10.68.199.85:36714 => /10.125.48.164:7769 FLUSH
2013-12-03 10:20:27,089 /10.68.199.85:36714 => /10.125.48.164:7769 RECEIVED(406B)

你实现了吗?

嗯, 不错! 然我们提升一点难度, 让你的脚本支持一个 关键字 的参数, 使得执行tail -f log | scala script.scala "401"后输出上面的内容不变, 而执行tail -f log | scala script.scala "400"后则输出:

2013-12-03 10:20:26,889 /10.68.199.85:36714 => /10.125.48.164:7769 WRITE(42B)
2013-12-03 10:20:26,889 /10.68.199.85:36714 => /10.125.48.164:7769 FLUSH
2013-12-03 10:20:27,089 /10.68.199.85:36714 => /10.125.48.164:7769 

请务必用函数的思维来实现你的脚本!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions