| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.FileVisitResult; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.nio.file.StandardOpenOption; |
| | | import java.nio.file.SimpleFileVisitor; |
| | | import java.nio.file.attribute.BasicFileAttributes; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | import java.util.Map; |
| | | import java.util.concurrent.locks.ReentrantLock; |
| | | import java.util.stream.Stream; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | private static final ReentrantLock FILE_OP_LOCK = new ReentrantLock(); |
| | | |
| | | @Scheduled(cron = "0/3 * * * * ? ") |
| | | public void delDeviceLog() { |
| | | if ("mysql".equals(storageType)) { |
| | | deviceDataLogService.clearLog(expireDays == null ? 1 : expireDays); |
| | | }else if ("file".equals(storageType)) { |
| | | if (!FILE_OP_LOCK.tryLock()) { |
| | | return; |
| | | } |
| | | try { |
| | | clearFileLog(expireDays == null ? 1 : expireDays); |
| | | } finally { |
| | | FILE_OP_LOCK.unlock(); |
| | | } |
| | | }else { |
| | | log.error("未定义的存储类型:{}", storageType); |
| | | } |
| | |
| | | if ("mysql".equals(storageType)) { |
| | | mysqlSave(keys, list); |
| | | }else if ("file".equals(storageType)) { |
| | | if (!FILE_OP_LOCK.tryLock()) { |
| | | return; |
| | | } |
| | | try { |
| | | fileSave(keys, list); |
| | | } finally { |
| | | FILE_OP_LOCK.unlock(); |
| | | } |
| | | }else { |
| | | log.error("未定义的存储类型:{}", storageType); |
| | | } |
| | |
| | | if (size + line.length > max) { |
| | | index++; |
| | | current = dayDir.resolve(prefix + index + ".log"); |
| | | if (!Files.exists(current)) { |
| | | Files.createFile(current); |
| | | } |
| | | size = 0; |
| | | } |
| | | Files.write(current, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND); |
| | |
| | | } |
| | | |
| | | private int findStartIndex(Path baseDir, String prefix) throws Exception { |
| | | List<Path> matched = Files.list(baseDir) |
| | | List<Path> matched; |
| | | try (Stream<Path> stream = Files.list(baseDir)) { |
| | | matched = stream |
| | | .filter(p -> { |
| | | String n = p.getFileName().toString(); |
| | | return n.startsWith(prefix) && n.endsWith(".log"); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | } |
| | | int maxIdx = 0; |
| | | for (Path p : matched) { |
| | | String name = p.getFileName().toString(); |
| | |
| | | } |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); |
| | | long cutoff = System.currentTimeMillis() - (long) days * 24 * 60 * 60 * 1000; |
| | | List<Path> dirs = Files.list(baseDir).filter(Files::isDirectory).collect(Collectors.toList()); |
| | | List<Path> dirs; |
| | | try (Stream<Path> stream = Files.list(baseDir)) { |
| | | dirs = stream.filter(Files::isDirectory).collect(Collectors.toList()); |
| | | } |
| | | for (Path dir : dirs) { |
| | | String name = dir.getFileName().toString(); |
| | | if (name.length() == 8 && name.chars().allMatch(Character::isDigit)) { |
| | | Date d = sdf.parse(name); |
| | | if (d.getTime() < cutoff) { |
| | | List<Path> all = Files.walk(dir).sorted(Comparator.reverseOrder()).collect(Collectors.toList()); |
| | | for (Path p : all) { |
| | | Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
| | | @Override |
| | | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| | | try { |
| | | Files.deleteIfExists(p); |
| | | Files.deleteIfExists(file); |
| | | } catch (Exception ignored) {} |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | |
| | | @Override |
| | | public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) { |
| | | try { |
| | | Files.deleteIfExists(dir); |
| | | } catch (Exception ignored) {} |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | } |