fix: 初始化逻辑修改

This commit is contained in:
niushuai233 2022-10-28 13:24:58 +08:00
parent 3a817c0fc5
commit aef284d685
4 changed files with 51 additions and 37 deletions

View File

@ -17,6 +17,8 @@ import cc.niushuai.project.devcontrol.base.util.Global;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.XLog;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.entity.SysConfig;
import cc.niushuai.project.devcontrol.db.util.DBHelper;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.RuntimeUtil;
@ -30,22 +32,16 @@ public class App {
// 申请权限
requestPermissions(activity);
// 初始化日志相关内容
initLog(activity);
XLog.i(Keys.Tag.APP_INIT, "{}====================================", System.lineSeparator());
XLog.i(Keys.Tag.APP_INIT, "应用{}启动", BuildConfig.APP_NAME);
XLog.i(Keys.Tag.APP_INIT, "====================================");
// 检查root权限
checkRoot(activity);
// 初始化数据库
initDb(activity);
// 初始化日志相关内容
initLogSupport(activity);
// 检查root权限
checkRoot(activity);
// 重建设备信息缓存
Global.initDeviceInfoMap();
// Global.initDeviceInfoMap();
XLog.i(Keys.Tag.APP_INIT, "初始化完毕");
XLog.i(Keys.Tag.APP_INIT, "应用{}初始化完毕", BuildConfig.APP_NAME);
}
/**
@ -115,7 +111,7 @@ public class App {
* @date: 2022/10/26 14:58
*/
private static void initDb(Activity activity) {
XLog.v(Keys.Tag.APP_INIT, "加载SQLite");
XLog.d(Keys.Tag.APP_INIT, "加载SQLite");
DB.getInstance().init(activity);
}
@ -125,10 +121,27 @@ public class App {
* @author niushuai
* @date: 2022/10/26 15:00
*/
private static void initLog(Activity activity) {
private static void initLogSupport(Activity activity) {
SysConfig switchConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_SWITCH);
if (null != switchConfig) {
XLog.SWITCH = Boolean.valueOf(switchConfig.getValue());
XLog.SWITCH_FOR_FILE = Boolean.valueOf(switchConfig.getValue());
}
SysConfig levelConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_LEVEL);
if (null != levelConfig) {
XLog.ROOT_LEVEL = XLog.Level.transform(levelConfig.getValue());
XLog.ROOT_LEVEL_NAME = XLog.Level.transform(XLog.ROOT_LEVEL);
}
SysConfig keepDayConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_KEEP_DAY);
if (null != keepDayConfig) {
XLog.KEEP_DAY = Integer.valueOf(keepDayConfig.getValue());
}
// 初始化日志路径
String rootPath = getRootPath();
XLog.v(Keys.Tag.APP_INIT, "获取日志文件根目录: {}", rootPath);
if (!FileUtil.exist(rootPath)) {
// 不存在的时候创建文件
@ -136,10 +149,12 @@ public class App {
XLog.v(Keys.Tag.APP_INIT, "日志根目录不存在, 创建: {}", mkdirs);
}
// 内存存储日志根目录
Global.LOG_ROOT_PATH = rootPath;
XLog.v(Keys.Tag.APP_INIT, "日志存储位置: {}", Global.LOG_ROOT_PATH);
XLog.d(Keys.Tag.APP_INIT, "日志开关: {}", XLog.SWITCH);
XLog.d(Keys.Tag.APP_INIT, "日志存储位置: {}", Global.LOG_ROOT_PATH);
XLog.d(Keys.Tag.APP_INIT, "日志级别: {}", XLog.ROOT_LEVEL_NAME);
XLog.d(Keys.Tag.APP_INIT, "日志保留天数: {}", XLog.KEEP_DAY);
}
private static String getRootPath() {
@ -149,11 +164,9 @@ public class App {
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
// 如果存在外置存储
rootPath = StrUtil.join(StrPool.SLASH, Environment.getExternalStorageDirectory().getAbsolutePath(), BuildConfig.APP_NAME);
XLog.v(Keys.Tag.APP_INIT, "外置存储已挂载");
} else {
// 否则就写到/data/data里
rootPath = StrUtil.join(StrPool.SLASH, Keys.LOG_ROOT_FILE_PATH, BuildConfig.APPLICATION_ID);
XLog.v(Keys.Tag.APP_INIT, "外置存储未挂载");
}
return StrUtil.join(StrPool.SLASH, rootPath, Keys.LOG_LOG_FOLDER);

View File

@ -25,23 +25,23 @@ public class XLog {
/**
* 日志总开关
*/
public static Boolean LOG_SWITCH = true;
public static Boolean SWITCH = true;
/**
* 是否写入到文件
*/
public static Boolean LOG_SWITCH_TO_FILE = true;
public static Boolean SWITCH_FOR_FILE = true;
/**
* 日志级别 默认info
*/
public static Integer SET_ROOT_LEVEL = Level.INFO;
public static String SET_ROOT_LEVEL_NAME = Level.INFO_NAME;
public static Integer ROOT_LEVEL = Level.INFO;
public static String ROOT_LEVEL_NAME = Level.INFO_NAME;
/**
* 日志保存最长时间
*/
public static Integer LOG_KEEP_DAY = 7;
public static Integer KEEP_DAY = 7;
/**
* <pre>
@ -49,7 +49,7 @@ public class XLog {
* 2022-10-26 11:11:11:111 - [DEBUG] - packageName#methodName:lineNumber: message
* </pre>
*/
public static final String LOG_TEMPLATE = "{} - [{}] - {}: {}";
public static final String PRINT_TEMPLATE = "{} - [{}] - {}: {}";
public static void v(String tag, String message, Object... params) {
log(tag, filterThr(message, params), Level.VERBOSE);
@ -107,7 +107,7 @@ public class XLog {
* @date: 2022/10/27 9:07
*/
private static void log(String tag, String message, int level) {
if (!LOG_SWITCH) {
if (!SWITCH) {
// 不输出日志 直接返回
return;
}
@ -116,7 +116,7 @@ public class XLog {
// 设置级别为 info 4 打印级别为 debug 3 可打印
// 要求当前输出的级别大于等于设置的级别
if (level >= SET_ROOT_LEVEL) {
if (level >= ROOT_LEVEL) {
if (Level.ERROR == level) {
Log.e(tag, message);
@ -155,7 +155,7 @@ public class XLog {
*/
public static void write(String tag, String message, int level) {
if (LOG_SWITCH_TO_FILE) {
if (SWITCH_FOR_FILE) {
String logContent = concatFinalLog(tag, level, message);
FileUtil.appendUtf8String(logContent + System.lineSeparator(), Global.logAbsolutePath());
}
@ -176,7 +176,7 @@ public class XLog {
// 时间 级别 线程信息 | 日志内容
// 2022-10-26 11:11:11:111 - [DEBUG] - packageName#methodName:lineNumber: message
return StrUtil.format(LOG_TEMPLATE,
return StrUtil.format(PRINT_TEMPLATE,
DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_FORMAT),
Level.transform(level),
threadInfo(),
@ -205,7 +205,7 @@ public class XLog {
return null;
}
static class Level {
public static class Level {
/**
* 详细

View File

@ -14,7 +14,6 @@ import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.XLog;
import cc.niushuai.project.devcontrol.databinding.LayoutLogKeepDayBinding;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.db.util.DBHelper;
/**
@ -45,7 +44,9 @@ public class KeepDayDialogFragment extends DialogFragment {
private void addListener() {
binding.layoutLogKeepDayConfirm.setOnClickListener(v -> {
String keepDayPickerValue = binding.keepDayPicker.getValue() + "";
int value = binding.keepDayPicker.getValue();
XLog.KEEP_DAY = value;
String keepDayPickerValue = value + "";
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, keepDayPickerValue);
XLog.v(Keys.Tag.KEEP_DAY_SELECT, "更新日志存储天数为: {}", keepDayPickerValue);
@ -60,7 +61,7 @@ public class KeepDayDialogFragment extends DialogFragment {
binding.keepDayPicker.setMinValue(1);
binding.keepDayPicker.setMaxValue(90);
binding.keepDayPicker.setValue(XLog.LOG_KEEP_DAY);
binding.keepDayPicker.setValue(XLog.KEEP_DAY);
binding.keepDayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
binding.keepDayPicker.setFormatter(value -> value + "");
binding.keepDayPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {

View File

@ -38,14 +38,14 @@ public class NavSetUpFragment extends BaseFragment {
SysConfig switchConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_SWITCH);
if (null == switchConfig) {
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, XLog.LOG_SWITCH.toString());
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, XLog.SWITCH.toString());
} else {
binding.setupLogSwitchSwitch.setChecked(Boolean.parseBoolean(switchConfig.getValue()));
}
SysConfig levelConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_LEVEL);
if (null == levelConfig) {
DBHelper.configDeal(Keys.SETUP_LOG_LEVEL, XLog.SET_ROOT_LEVEL_NAME);
DBHelper.configDeal(Keys.SETUP_LOG_LEVEL, XLog.ROOT_LEVEL_NAME);
} else {
String[] logLevel = getResources().getStringArray(R.array.logLevel);
for (int i = 0; i < logLevel.length; i++) {
@ -59,7 +59,7 @@ public class NavSetUpFragment extends BaseFragment {
SysConfig keepDayConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_KEEP_DAY);
if (null == keepDayConfig) {
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, XLog.LOG_KEEP_DAY.toString());
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, XLog.KEEP_DAY.toString());
} else {
binding.setupLogKeepDayDisplay.setText(keepDayConfig.getValue());
}
@ -107,7 +107,7 @@ public class NavSetUpFragment extends BaseFragment {
*/
private void setupLogSwitchClickListener(View view) {
Boolean checked = binding.setupLogSwitchSwitch.isChecked();
XLog.LOG_SWITCH = checked;
XLog.SWITCH = checked;
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, checked.toString());
}