|
|
@@ -0,0 +1,52 @@
|
|
|
+package cn.seecoder.devmanage.log;
|
|
|
+
|
|
|
+import cn.seecoder.devmanage.model.po.LocalLog;
|
|
|
+import cn.seecoder.devmanage.repository.LocalLogRepository;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+@Component
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class LocalLogger {
|
|
|
+ private final LocalLogRepository localLogRepository;
|
|
|
+
|
|
|
+ @Value("${locallogger.timezone.offset}")
|
|
|
+ private String timezoneOffset;
|
|
|
+ private static SimpleDateFormat dateFormat;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ TimeZone timeZone = TimeZone.getTimeZone(ZoneId.of(timezoneOffset));
|
|
|
+ dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ dateFormat.setTimeZone(timeZone);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void record(String message) {
|
|
|
+ String currentUser = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
+ LocalLog localLog = new LocalLog(currentUser, message, System.currentTimeMillis());
|
|
|
+ localLogRepository.save(localLog);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String convertTime(Long timestamp) {
|
|
|
+ Date date = new Date(timestamp);
|
|
|
+ return dateFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long convertTime(String time) throws ParseException {
|
|
|
+ Date parse = dateFormat.parse(time);
|
|
|
+ return parse.getTime();
|
|
|
+ }
|
|
|
+}
|