package com.ruoyi.cron.runner;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.cron.TaskLog;
import com.ruoyi.cron.document.CronTaskLog;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TaskLogImpl implements TaskLog {

  public static final ThreadLocal<CronTaskLog> logHolder = new ThreadLocal<>();

  @Override
  public void log(String msg) {
    if (logHolder.get() == null) {
      return;
    }
    if (StrUtil.isBlank(logHolder.get().getEx())) {
      logHolder.get().setEx(DateUtil.format(new Date(), "HH:mm:ss.SSS ")+ Thread.currentThread().getName()+ "\t : " + msg);
    } else {
      logHolder.get().setEx(logHolder.get().getEx() + DateUtil.format(new Date(), "\nHH:mm:ss.SSS ")+ Thread.currentThread().getName()+ "\t : " + msg);
    }
  }
}
