-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogService.java
More file actions
79 lines (49 loc) · 2.05 KB
/
Copy pathLogService.java
File metadata and controls
79 lines (49 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package sample;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author liuxf
* @version 1.0
* @date 2020/12/9 16:49
*/
public class LogService {
public static void logInfo(String log) {
String data = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
String pathStr = System.getProperty("user.dir") + File.separator + "logs" + File.separator + data + File.separator;
File path = new File(pathStr);
if (!path.exists()) {
path.mkdirs();
}
String filePath = pathStr + data + "-info.log";
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
randomAccessFile.seek(randomAccessFile.length());
log = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+"---"+log+System.lineSeparator();
randomAccessFile.write(log.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void logError(String log) {
String errorData = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
String pathStr = System.getProperty("user.dir") + File.separator + "logs" + File.separator + errorData + File.separator;
File path = new File(pathStr);
if (!path.exists()) {
path.mkdirs();
}
String filePath = pathStr + errorData + "-error.log";
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
randomAccessFile.seek(randomAccessFile.length());
log = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))+"---"+log+System.lineSeparator();
randomAccessFile.write(log.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
}