Commit 25997146 by pye52

增加日志文件夹监测(每4小时进行一次),当检测到空间不足时,删除旧的日志文件

parent ad95c6e6
...@@ -108,6 +108,8 @@ dependencies { ...@@ -108,6 +108,8 @@ dependencies {
def lifecycle_version = "2.2.0" def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
def work_version = "2.3.1"
implementation "androidx.work:work-runtime:$work_version"
implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13' testImplementation 'junit:junit:4.13'
......
...@@ -12,8 +12,6 @@ import java.io.File; ...@@ -12,8 +12,6 @@ import java.io.File;
public class RootApp extends Application { public class RootApp extends Application {
private static final String LOG_PREFIX = "app"; private static final String LOG_PREFIX = "app";
private static final String LOG_DIR = "log"; private static final String LOG_DIR = "log";
// 设置日志保存期限,避免日志过多占用内存空间
private static final int LOG_SAVE_DAYS = 7;
@Override @Override
public void onCreate() { public void onCreate() {
...@@ -25,7 +23,6 @@ public class RootApp extends Application { ...@@ -25,7 +23,6 @@ public class RootApp extends Application {
.setDir(logDir) .setDir(logDir)
.setLog2FileSwitch(true) .setLog2FileSwitch(true)
.setBorderSwitch(false) .setBorderSwitch(false)
.setFilePrefix(LOG_PREFIX) .setFilePrefix(LOG_PREFIX);
.setSaveDays(LOG_SAVE_DAYS);
} }
} }
...@@ -35,6 +35,7 @@ import com.bgycc.smartcanteen.state.QRCodeState; ...@@ -35,6 +35,7 @@ import com.bgycc.smartcanteen.state.QRCodeState;
import com.bgycc.smartcanteen.utils.NetworkUtils; import com.bgycc.smartcanteen.utils.NetworkUtils;
import com.bgycc.smartcanteen.utils.SmartCanteenUtils; import com.bgycc.smartcanteen.utils.SmartCanteenUtils;
import com.bgycc.smartcanteen.utils.TTSHelper; import com.bgycc.smartcanteen.utils.TTSHelper;
import com.bgycc.smartcanteen.utils.WorkerUtils;
import com.bgycc.smartcanteen.viewModel.CommandViewModel; import com.bgycc.smartcanteen.viewModel.CommandViewModel;
import com.bgycc.smartcanteen.viewModel.PayOfflineViewModel; import com.bgycc.smartcanteen.viewModel.PayOfflineViewModel;
import com.bgycc.smartcanteen.viewModel.PayOnlineViewModel; import com.bgycc.smartcanteen.viewModel.PayOnlineViewModel;
...@@ -269,6 +270,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -269,6 +270,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
commandViewModel.initialize(); commandViewModel.initialize();
handler.post(updateTimeRunnable); handler.post(updateTimeRunnable);
WorkerUtils.startLogFilesMonitor(this);
SCWebSocketClient.getInstance().tryConnect(); SCWebSocketClient.getInstance().tryConnect();
} }
...@@ -332,6 +334,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -332,6 +334,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
TTSHelper.release(); TTSHelper.release();
SCWebSocketClient.getInstance().realClose(); SCWebSocketClient.getInstance().realClose();
SCTaskExecutor.getInstance().quit(); SCTaskExecutor.getInstance().quit();
WorkerUtils.stopLogFilesMonitor(this);
} }
private void initViews() { private void initViews() {
......
package com.bgycc.smartcanteen.utils;
import android.content.Context;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import com.bgycc.smartcanteen.worker.LogFileMonitor;
import java.util.concurrent.TimeUnit;
public class WorkerUtils {
private static final String LOG_MONITOR_WORKER = "worker_log_monitor";
// 每隔一定时间检查日志文件夹并进行清理
public static void startLogFilesMonitor(Context context) {
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(LogFileMonitor.class, 4, TimeUnit.HOURS)
.addTag(LOG_MONITOR_WORKER)
.build();
WorkManager.getInstance(context)
.enqueue(request);
}
public static void stopLogFilesMonitor(Context context) {
WorkManager.getInstance(context)
.cancelAllWorkByTag(LOG_MONITOR_WORKER);
}
}
package com.bgycc.smartcanteen.worker;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import java.io.File;
import java.util.Collections;
import java.util.List;
import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG;
public class LogFileMonitor extends Worker {
public LogFileMonitor(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
String logDirPath = LogUtils.getConfig().getDir();
long totalSize = FileUtils.getFsTotalSize(logDirPath);
long availableSize = FileUtils.getFsAvailableSize(logDirPath);
LogUtils.d(TAG, "日志文件系统, 可用大小: " + availableSize
+ ", 总大小: " + totalSize);
while (availableSize < (totalSize * 0.1f)) {
// 当可用小于10%时,删除旧日志文件
List<File> logFiles = LogUtils.getLogFiles();
Collections.sort(logFiles, (f1, f2) -> {
if (f1.lastModified() == f2.lastModified()) {
return 0;
}
return (f1.lastModified() > f2.lastModified()) ? 1 : -1;
});
for (File f : logFiles.subList(0, 2)) {
FileUtils.delete(f);
}
totalSize = FileUtils.getFsTotalSize(logDirPath);
availableSize = FileUtils.getFsAvailableSize(logDirPath);
}
return Result.success();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment