Parse Java error logs
In big data and high-concurrency environments, parsing Java error logs into a structured format reduces O&M costs. Simple Log Service can collect Java error logs from various Alibaba Cloud services and use the data transformation feature to parse them.
Prerequisites
Java error logs from SLS, OSS, SLB, and ApsaraDB RDS are collected and stored in a Logstore named cloud_product_error_log. For more information, see Use Logtail to collect logs.
Use cases
For example, you have developed a Java application named Application A that uses multiple Alibaba Cloud services such as OSS and SLS. You store Java error logs generated by API calls to these services in a Logstore named cloud_product_error_log in the China (Hangzhou) region. To fix Java errors efficiently, you analyze the error logs at regular intervals by using Simple Log Service.
Parse the log time, error code, status code, service information, error message, request method, and error line number from the collected logs, then store the parsed logs in each service's Logstore for error analysis.
The following is a sample raw log:
__source__:192.0.2.10
__tag__:__client_ip__:203.0.113.10
__tag__:__receive_time__:1591957901
__topic__:
message: 2021-05-15 16:43:35 ParameterInvalid 400
com.aliyun.openservices.log.exception.LogException:The body is not valid json string.
at com.aliyun.openservice.log.Client.ErrorCheck(Client.java:2161)
at com.aliyun.openservice.log.Client.SendData(Client.java:2312)
at com.aliyun.openservice.log.Client.PullLogsk(Client.java:1397)
at com.aliyun.openservice.log.Client.SendData(Client.java:2265)
at com.aliyun.openservice.log.Client.GetCursor(Client.java:1123)
at com.aliyun.openservice.log.Client.PullLogs(Client.java:2161)
at com.aliyun.openservice.log.Client.ErrorCheck(Client.java:2426)
at transformEvent.main(transformEvent.java:2559)
Procedure
Logtail collects error logs from Application A and stores them in a Logstore. An SPL statement processes and routes the logs to separate Logstores for each cloud service. You can then analyze the structured error logs in their respective destination Logstores. The procedure is as follows:
-
Design an SPL statement: Analyze the log data and write a statement to parse it.
-
Create a data transformation job: Distribute logs to different error analysis Logstores based on the cloud service.
-
Query and analyze data: Query the structured logs in the destination Logstores.
Step 1: Design an SPL statement
SPL process
To analyze error logs:
-
Extract the timestamp, error code, status code, service information, error message, request method, and error line number from the
messagefield. -
Store the error logs in each service's Logstore.

SPL logic analysis
Analyze the timestamp, error code, status code, service information, error message, request method, and error line number in the raw logs, then design a regular expression to extract these fields.
Syntax
-
Use the regexp_like function to check if a log entry contains LogException. If a match is found, process the log based on the parsing rule for SLS error logs. If the log entry contains OSSException, process it based on the parsing rule for OSS error logs. For more information, see SPL statements and functions.
-
Use regular expression parsing functions, such as parse-regexp, from SPL statements and functions to extract data from the error logs.
-
Use a function such as project-away from SPL statements and functions to remove the original message field after parsing. The following example shows how to use a regular expression to parse SLS error logs:
.let a = * | where regexp_like(message, '.*LogException.*') | parse-regexp message,'(\S+\s\S+)\s([a-zA-Z]+)\s([0-9]+)\scom\.aliyun\.openservices\.log\.exception\.([a-zA-Z]+)\:([a-zA-Z0-9:,\-\s]+)\.\s+(\S+\s+\S+\s+){6}\S+\s+com\.aliyun\.openservices\.log\.Client\.([a-zA-Z]+)\S+\s+\S+\s+transformEvent\.main\(transformEvent\.java\:([0-9]+)\)' as data_time,error_code,status,product_exception,error_message,temp,method,error_line | project-away message,temp; .let b = * | where regexp_like(message, '.*OSSException.*') | parse-regexp message,'(\S+\s\S+)\s+com\.aliyun\.oss\.([a-zA-Z]+)\:([a-zA-Z0-9,\s]+)\.\s+\[ErrorCode\]\:\s+([a-zA-Z]+)\s+\[RequestId\]\:\s([a-zA-Z0-9]+)\s+\[HostId\]\:\s+(\S+)\s+com\.aliyun\.oss\.OSSClient\.([a-zA-Z]+)\s+transformEvent\.main\(transformEvent\.java:([0-9]+)\)' as data_time,product_exception,error_message,error_code,request_id,host_id,method,error_line| project-away message; $a; $b;Sample output for Result Set a:
__source__:192.*.*.* __tag__:__client_ip__:203.*.*.* __tag__:__receive_time__:1591957901 __topic__: data_time:2021-05-15 16:43:35 error_code:ParameterInvalid error_line:2559 error_message:The body is not valid json string method:ErrorCheck product_exception:LogException status:400Sample output for Result Set b:
__source__:192.*.*.* __tag__:__client_ip__:203.*.*.* __tag__:__receive_time__:1591957988 __topic__: data_time:2020-05-16 18:30:06 error_code:AccessDenied error_line:399 error_message:You are forbidden to list buckets host_id:wwduhdw-cn-hangzhou.aliyuncs.com method:getBucket product_exception:OSSException request_id:YGYLQJLSLNUS2US1131DSQKW
Step 2: Create a data transformation job
Log on to the Simple Log Service console.
In the Projects section, click the one you want.

-
On the tab, click the destination Logstore. Then, hover the pointer over and click
to add a job. -
On the Raw Logs tab, ensure that log data is available, and then click Add Test Data.
-
On the Test Data tab, view the test data that you added.
The test data is a JSON array of two log records. Each record contains fields such as
__source__,__tag__:__client_ip__,__tag__:__receive_time__,__topic__, andmessage. Themessagevalue of the first record is2021-05-15 16:43:35 ParameterInvalid 400 com.aliyun.openservices.log.exception.LogException:Th..., and themessagevalue of the second record is2020-05-16 18:30:06 com.aliyun.oss.OSSException:You are forbidden to list buckets.... -
In the editor, enter the SPL statement. Then, click Debug SPL. On the Transformation Results tab, you can preview the parsed results. Result Set a contains the parsed SLS error log, and Result Set b contains the parsed OSS error log.
.let a = * | where regexp_like(message, '.*LogException.*') | parse-regexp message,'(\S+\s\S+)\s([a-zA-Z]+)\s([0-9]+)\scom\.aliyun\.openservices\.log\.exception\.([a-zA-Z]+)\:([a-zA-Z0-9:,\-\s]+)\.\s+(\S+\s+\S+\s+){6}\S+\s+com\.aliyun\.openservices\.log\.Client\.([a-zA-Z]+)\S+\s+\S+\s+transformEvent\.main\(transformEvent\.java\:([0-9]+)\)' as data_time,error_code,status,product_exception,error_message,temp,method,error_line | project-away message,temp; .let b = * | where regexp_like(message, '.*OSSException.*') | parse-regexp message,'(\S+\s\S+)\s+com\.aliyun\.oss\.([a-zA-Z]+)\:([a-zA-Z0-9,\s]+)\.\s+\[ErrorCode\]\:\s+([a-zA-Z]+)\s+\[RequestId\]\:\s([a-zA-Z0-9]+)\s+\[HostId\]\:\s+(\S+)\s+com\.aliyun\.oss\.OSSClient\.([a-zA-Z]+)\s+transformEvent\.main\(transformEvent\.java:([0-9]+)\)' as data_time,product_exception,error_message,error_code,request_id,host_id,method,error_line| project-away message; $a; $b;The Transformation Results tab displays two parsed records. Result Set a corresponds to the LogException log and contains extracted fields such as
data_time(2021-05-15 16:43:35),error_code(ParameterInvalid),status(400),product_exception(LogException), andmethod(ErrorCheck). Result Set b corresponds to the OSSException log and contains extracted fields such asdata_time(2020-05-16 18:30:06),error_code(AccessDenied),product_exception(OSSException),request_id,host_id, andmethod(getBucket). This confirms that both regular expression rules produce the expected structured output. -
Click Save as Transformation Job (New Version). In the Create Data Transformation Job (New Version) panel, select Result Set a to save the SLS error logs, or select Result Set b to save the OSS error logs. For more information, see Parameters.
In the Create Data Transformation Job (New Version) panel, set a Display Name (for example,
test), configure the storage destination by specifying the Target Project (for example,project-test1) and Target Logstore (for example,l-test), select the result set from the Write to Result Set drop-down list, and then click OK.
Step 3: View the results
Go to the destination Logstore for Result Set a to view the parsed SLS error logs. You can use the query syntax to analyze the logs.
The query results show an error log record with the following fields: error_code is ParameterInvalid, error_message is The body is not valid json string, status is 400, method is ErrorCheck, and product_exception is LogException.
More
Similarly, normal logs can also be processed by matching content using regular expressions. For example, multi-line stack traces can be handled by matching trace IDs. Additionally, based on the processed results can Create an alert ruleto enable data monitoring capabilities.