根据实例过滤掉异常并路由到文件

Filter out exceptions and routing to a file based on the instance

我有一个场景,我需要通过 grepping 实例名称并将其所有相关异常写入 Instance.txt

来从日志文件中 prepare/write 异常

示例测试-dom-01.txt,测试-dom-01.txt,测试-dom-04.txt ...等等及其相关的错误

out.log

Performing Health Check for Instance : test-dom-01
test-dom-01 Instance is----->  Running
****Existing Exceptions***** on instance: test-dom-01
catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in 
catalina.0.log:SEVERE: Exception when trying to read white list from  probably because file list is empty

--> Health Check for Instance :test-dom-01 is completed

Performing Health Check for Instance : tests-rs-dom2-1
tests-rs-dom2-1 Instance is----->  Running
****Existing Exceptions***** on instance: tests-rs-dom2-1 
catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:23:03.266 [xxxx BACK END TCPLink Reader (Server-555)]
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:28:53 INFO
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]


--> Health Check for Instance :tests-rs-dom2-1 is completed

Performing Health Check for Instance : test-dom-04
test-dom-04 Instance is----->  Running
****Existing Exceptions***** on instance: test-dom-04
catalina.0.log:Caused by: java.net.BindException: Address already in use
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]


--> Health Check for Instance :test-dom-04 is completed

问题是我必须将现有异常移动到每个文件而忽略其余行只是异常必须移动到单独的文件

test-dom-01 文件的预期输出是 test-dom-01.txt 内容为

catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in 
catalina.0.log:SEVERE: Exception when trying to read white list from  probably because file list is empty

tests-rs-dom2-1 的预期输出是 tests-rs-dom2-1.txt

catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:23:03.266 [xxxx BACK END TCPLink Reader (Server-555)]
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:28:53 INFO
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]

尝试过:

awk '/on instance: test-dom-01/,/ /' out.log > test-dom-01.txt

但运气不好

你可以使用这个awk:

awk '/ on instance: /{fn=$NF ".txt"; next} !NF{close(fn); fn=""} fn{print > fn}' out.log