How to connect to Simple Log Service
Alibaba Cloud Distributed Task Scheduling Platform (SchedulerX) 2.0 provides a log service. You can view business logs for each task schedule, including distributed tasks, in the console without modifying your code. You only need to add a Log4j or Logback configuration. This topic describes how to enable and view the log service in SchedulerX.
Prerequisites
- Create resources.
- Create resources.
-
Upgrade your application to the Professional Edition and enable the log service. For more information, see Upgrade to the Professional Edition.
Note After you enable the log service, logs are retained for a maximum of two weeks. Logs older than two weeks are deleted.
Configuration
Upgrade the SchedulerX client
Upgrade the SchedulerX client to version 1.4.0 or later. For example, if you use Spring Boot Starter, add the SchedulerxWorker dependency to your application's pom.xml file.
<dependency>
<groupId>com.aliyun.schedulerx</groupId>
<artifactId>schedulerx2-spring-boot-starter</artifactId>
<version>1.3.4</version>
</dependency>
Configure a Log Appender to collect logs
Log4j2 Appender
- Method 1: Collect only task scheduling logs
With this method, you can control log collection from your business logic. It collects only task scheduling logs and ignores other business logs. This simplifies troubleshooting.
log4j2as follows:<?xml version="1.0" encoding="UTF-8"?> <Configuration status="off"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n" /> </Console> <SchedulerxLog4j2Appender name="schedulerxLog" timeFormat="yyyy-MM-dd'T'HH:mmZ" timeZone="UTC" ignoreExceptions="true"> <PatternLayout pattern="%d %-5level [%thread] %logger{0}: %msg"/> </SchedulerxLog4j2Appender> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console" /> </Root> <Logger name="schedulerx" level="info" additivity="false"> <AppenderRef ref="schedulerxLog" /> </Logger> </Loggers> </Configuration>- Modify the business code as follows to use the `schedulerx` logger to collect task scheduling logs.
package com.hxm.test.processor; import com.alibaba.schedulerx.worker.domain.JobContext; import com.alibaba.schedulerx.worker.processor.JavaProcessor; import com.alibaba.schedulerx.worker.processor.ProcessResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class HelloWorldJob3 extends JavaProcessor { private static final Logger logger = LoggerFactory.getLogger("schedulerx"); @Override public ProcessResult process(JobContext context) throws Exception { logger.info("hello HelloWorldJob3"); return new ProcessResult(true); } }
- Method 2: Collect all business logs
This method requires you to configure only the Log4j file. No business code modification is needed. If you configure SchedulerxLog in the rootLogger, all logs are collected by SchedulerX. This can slow down searches and mix business logs with task scheduling logs, which makes troubleshooting difficult. The configuration is as follows:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="off"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n" /> </Console> <SchedulerxLog4j2Appender name="schedulerxLog" timeFormat="yyyy-MM-dd'T'HH:mmZ" timeZone="UTC" ignoreExceptions="true"> <PatternLayout pattern="%d %-5level [%thread] %logger{0}: %msg"/> </SchedulerxLog4j2Appender> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console" /> <AppenderRef ref="schedulerxLog" /> </Root> </Loggers> </Configuration>
Log4j Appender
- Method 1: Collect only task scheduling logs
log4j.propertiesfile.log4j.rootLogger = INFO,console log4j.logger.schedulerx=schedulerxLog log4j.appender.schedulerxLog=com.alibaba.schedulerx.worker.log.appender.SchedulerxLog4jAppender- Modify the business code as follows to use the `schedulerx` logger to collect task scheduling logs.
package com.hxm.test.processor; import com.alibaba.schedulerx.worker.domain.JobContext; import com.alibaba.schedulerx.worker.processor.JavaProcessor; import com.alibaba.schedulerx.worker.processor.ProcessResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class HelloWorldJob3 extends JavaProcessor { private static final Logger logger = LoggerFactory.getLogger("schedulerx"); @Override public ProcessResult process(JobContext context) throws Exception { logger.info("hello HelloWorldJob3"); return new ProcessResult(true); } }
- Method 2: Collect all logs
log4j.propertiesfile and configure this appender in the rootLogger:log4j.rootLogger = INFO,console,schedulerxLog log4j.appender.schedulerxLog=com.alibaba.schedulerx.worker.log.appender.SchedulerxLog4jAppender
Logback Appender
- Method 1: Collect only task scheduling logs
logback.xmlfile.<appender name="schedulerxLog" class="com.alibaba.schedulerx.worker.log.appender.SchedulerxLogbackAppender"> <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat> <timeZone>UTC</timeZone> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> </root> <logger name="schedulerx" level="INFO"> <appender-ref ref="schedulerxLog"/> </logger>- Modify the business code as follows to use the `schedulerx` logger to collect task scheduling logs.
package com.hxm.test.processor; import com.alibaba.schedulerx.worker.domain.JobContext; import com.alibaba.schedulerx.worker.processor.JavaProcessor; import com.alibaba.schedulerx.worker.processor.ProcessResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class HelloWorldJob3 extends JavaProcessor { private static final Logger logger = LoggerFactory.getLogger("schedulerx"); @Override public ProcessResult process(JobContext context) throws Exception { logger.info("hello HelloWorldJob3"); return new ProcessResult(true); } }
- Method 2: Collect all logs
logback.xmlfile and configure it in the rootLogger:<appender name="schedulerxLog" class="com.alibaba.schedulerx.worker.log.appender.SchedulerxLogbackAppender"> <timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat> <timeZone>UTC</timeZone> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="schedulerxLog"/> </root>
View output logs
- Log on to the Distributed Task Scheduling Platform.
- In the navigation pane on the left, click Task Management.
- In the task list, find the task that you want to manage and choose in the Actions column.
- On the Task Instance Record page, click Log in the Actions column of the target task.
The keywords in the logs are defined as follows:
ip: The machine that executed the task and printed the log.executionId: The execution ID of the task instance. The format is${jobId}_${jobInstanceId}_${taskId}.level: The log level.log: The log message.throwable: This field is printed when the application throws an exception.time: The time when the log was printed.
Scenarios and demos
Query the cause of a business failure
The SchedulerX 2.0 log service can collect task execution logs and exceptions, including logs from services.
- Create the code.
- Create the task code and configure it to use SchedulerX for log printing.
package com.hxm.test.processor; import com.alibaba.schedulerx.test.service.TestService; import com.alibaba.schedulerx.worker.domain.JobContext; import com.alibaba.schedulerx.worker.processor.JavaProcessor; import com.alibaba.schedulerx.worker.processor.ProcessResult; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class HelloWorldJob extends JavaProcessor { // Use SchedulerX to collect logs private static final Logger LOGGER = LogManager.getLogger("schedulerx"); @Autowired private TestService testService; @Override public ProcessResult process(JobContext context) throws Exception { String parameters = context.getJobParameters(); String tokens[] = parameters.split(" "); int a = Integer.valueOf(tokens[0]); int b = Integer.valueOf(tokens[1]); int c = testService.doDivision(a, b); LOGGER.info("testService.doDivision finished, a={}, b={}, c={}", a, b, c); if (c < 0) { return new ProcessResult(false, "result=" + c); } return new ProcessResult(true); } } - Create the service business code and configure it to use SchedulerX for log printing.
package com.hxm.test.service; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; @Service("testService") public class TestServiceImpl implements TestService { // Use SchedulerX to collect logs. private static final Logger LOGGER = LogManager.getLogger("schedulerx"); @Override public int doDivision(int a, int b) { try { LOGGER.info("start to do division c = " + a + "/" + b); int c = a/b; LOGGER.info("c=" + c); return c; } catch (Exception e) { LOGGER.error("", e); } return -1; } }
- Create the task code and configure it to use SchedulerX for log printing.
- Configure the task in the console.
Based on the code logic, an exception is thrown because of a division by zero.
- After the task runs once, view its logs from the task instance list.
The logs show that the failure was caused by a division-by-zero exception thrown by TestServiceImpl.
Query the cause of a distributed task failure
Distributed tasks in SchedulerX 2.0 are used to execute batch tasks. To find out which sub-task failed when a batch of tasks fails, perform the following steps:
- Create the task code and configure it to use SchedulerX for log printing.
package com.hxm.test.processor; import java.util.ArrayList; import java.util.List; import com.alibaba.schedulerx.worker.domain.JobContext; import com.alibaba.schedulerx.worker.processor.MapJobProcessor; import com.alibaba.schedulerx.worker.processor.ProcessResult; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class TestMapJobProcessor extends MapJobProcessor { private static final Logger LOGGER = LogManager.getLogger("schedulerx"); @Override public ProcessResult process(JobContext context) throws Exception { String taskName = context.getTaskName(); String parameter = context.getJobParameters(); int dispatchNum = Integer.valueOf(parameter); if (isRootTask(context)) { LOGGER.info("start root task"); List<String> msgList = new ArrayList<>(); for (int i = 0; i <= dispatchNum; i++) { msgList.add("msg_" + i); } return map(msgList, "Level1Dispatch"); } else if (taskName.equals("Level1Dispatch")) { String task = (String)context.getTask(); if (task.equals("msg_23")) { LOGGER.error("msg={}, failed", task); return new ProcessResult(false); } else { LOGGER.info("msg={}, success", task); } return new ProcessResult(true); } return new ProcessResult(false); } } - Configure the task in the console and run it once.
- View the logs. For more information, see View output logs.
- Search for keywords in the logs to quickly locate the failed task and its cause.
Query historical logs by keyword
The history of a task instance retains only the last 60 records. To investigate the causes of historical task failures, you can retrieve logs using the Log Query feature in the navigation pane on the left of the console. Log Query supports searches by job ID, keyword, and time range.
Sample Demo
Log4j2 demo download URL: https://schedulerx2.oss-cn-hangzhou.aliyuncs.com/examples/schedulerx-example-log-log4j2.tar.gz
- Logback demo download URL: https://schedulerx2.oss-cn-hangzhou.aliyuncs.com/examples/schedulerx-example-log-logback.tar.gz