Requirements: The application end writes audit logs to a file in a specific format (TXT). Each file must contain complete log information and contain no more than 10000 logs. The application side transfers the audit logs to the directory on the file server specified by the audit system through SFTP/FTP.
1. Generate log files locally
1.1 Creating a Log Object
1.2 Object Assignment
1.3 Converting an Object to JSON
AuditLogEntity au = new AuditLogEntity();
au.setLOG_ID(UUID.randomUUID().toString());
String content = JSON.toJSONString(au);
Copy the code
1.4 Generating Log Files in the Local Path
1.4.1 Obtaining write files
Obtain the latest file under the directory is a written file. The criterion is the last modification time
/** * get the last file name *@pathFile directory address *@returnLast file name, sorted backwards by modification time */
public String getLastFileName(String path) {
File filePath = new File(path);
File[] files = filePath.listFiles();
if (files.length > 0) {
if (files.length > 1) {
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
return (int) (file2.lastModified() - file1.lastModified()); }}); }return files[0].getName();
} else {
return null; }}Copy the code
1.4.2 Obtaining Files and Contents
Gets the file and its encapsulated content based on the file name
- Each log is encapsulated in one line, and a newline must be added at the beginning
- Each log file cannot exceed 10000 lines
- The log name is in 20210601_001.txt format
- If the number of lines exceeds, a new file,20210601_002.txt, is generated according to the specification, and so on
/** * Get the file from the file name assembly path *@return path
*/
public Map getFilePath(String content, String lastFileName) {
String prefix = "d:/ftpTest/";
String filepath;
String uploadName = new SimpleDateFormat("yyyyMMdd").format(new Date());
if (null! = lastFileName && lastFileName.startsWith(uploadName)) { filepath = prefix + lastFileName; }else {
filepath = prefix + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "_001.txt";
}
File file = new File(filepath);
// Get the number of existing file lines
long lineNumber = getLineNumber(file);
logger.info("The number of existing lines of file is --" + lineNumber);
// If there are more than the specified number of rows
if (lineNumber > 0) {
// A new file is generated when the specified number of lines is exceeded
if (lineNumber > 10000) {
int num = Integer.parseInt(lastFileName.substring(10.12));
num++;
String suffix = String.format("_%03d", num) + ".txt";
String fileName = new SimpleDateFormat("yyyyMMdd").format(new Date());
filepath = prefix + fileName + suffix;
} else {
/ / a newline
content = "\n" + content;
}
}
Map map = new HashMap(2);
map.put("filepath", filepath);
map.put("content", content);
return map;
}
/** * get the number of file lines **@paramThe file file *@returnThe number of rows * /
public long getLineNumber(File file) {
if (file.exists()) {
try {
FileReader fileReader = new FileReader(file);
LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
lineNumberReader.skip(Long.MAX_VALUE);
long lines = lineNumberReader.getLineNumber() + 1;
fileReader.close();
lineNumberReader.close();
return lines;
} catch (IOException e) {
logger.error(Error reading file lines!!); e.printStackTrace(); }}return 0;
}
Copy the code
1.4.3 Writing files
try {
String lastFileName = getLastFileName("D:/ftpTest/");
// Get the file and its encapsulated contents according to the file name
Map m = getFilePath(content, lastFileName);
File file = new File(m.get("filepath").toString());
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(m.get("content").toString());
bw.close();
fw.close();
logger.info("Log write success!");
} catch (Exception e) {
logger.error("Log write failed!!");
e.printStackTrace();
}
Copy the code
2. Upload files
2.1 Connecting to the FTP Server
2.2 Obtaining all files in the Directory
2.3 A Scheduled Task Uploads only the log files of the last day
public void uploadFile(a) {
FTPClient ftp = new FTPClient();
try {
String prefix = "d:/ftpTest/";
File filePath = new File(prefix);
File[] files = filePath.listFiles();
if (files.length < 0) {
logger.error("Unable to upload file found!!");
} else {
int reply;
// Connect to the FTP server
ftp.connect("192.168.1.211".21);
/ / login
ftp.login("test"."test");
reply = ftp.getReplyCode();
logger.error("The reply," " + reply);
if(! FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); }// Set to passive mode
ftp.enterLocalPassiveMode();
// Set the encoding format to UTF-8
ftp.setControlEncoding("UTF-8");
// Set the upload file type to binary
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// Set the folder to store
ftp.changeWorkingDirectory("/home/test/data");
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
String uploadName = new SimpleDateFormat("yyyyMMdd").format(new Date().getTime() - 24 * 60 * 60 * 1000);
// Upload only the logs of the previous day
if (fileName.startsWith(uploadName)) {
String path = prefix + fileName;
FileInputStream localFile = new FileInputStream(path);
// Upload the file
boolean b = ftp.storeFile(fileName, localFile);
logger.info("{} Upload result ---- {}", fileName, b); localFile.close(); } } ftp.logout(); }}catch (IOException e) {
logger.info("FTP upload failed!");
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) { ioe.printStackTrace(); }}}}Copy the code
3. Configuration file
# FTP server address
FTP. Host = 192.168.1.211
# FTP server port number
ftp.port=21
# FTP username
ftp.username=test
# FTP password
ftp.password=test
# FTP upload root directory
ftp.remotePath=/home/test/data
# log generation directory
ftp.localFilepath=d:/ftpTest/
Copy the code