Skip to content

Commit

Permalink
fix: Fix log files take up a lot of space
Browse files Browse the repository at this point in the history
Previously, the anything would keep log files generated in the last 30
days. If a large number of logs were generated in a short period of
time, it would take up a lot of space. Let's modify it to only keep the
10 most recent log files.

Log: Fix log files take up a lot of space.
  • Loading branch information
wangrong1069 committed Jan 8, 2025
1 parent ea09505 commit 848c1a6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
9 changes: 9 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
deepin-anything (6.2.2) unstable; urgency=medium

* New version 6.2.2.
* Fix log files take up a lot of space.
* Add support for s360x, ppc64le and riscv.
* Optimize recoginizing ARM.

-- wangrong <[email protected]> Tue, 07 Jan 2025 20:41:36 +0800

deepin-anything (6.2.1) unstable; urgency=medium

* adapt kernel module for LOONGARCH architecture.
Expand Down
2 changes: 1 addition & 1 deletion src/server/backend/lib/lftmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ QStringList LFTManager::insertFileToLFTBuf(const QByteArray &file)
/* 由于事件合并的原因, 会经常导致报告此类错误. 由于它不属于程序问题, 特此降低日志等级 */
cDebug() << "Failed(Path Exists):" << mount_path;
} else {
cWarning() << "Failed:" << mount_path << ", result:" << r;
cDebug() << "Failed:" << mount_path << ", result:" << r;
}
}

Expand Down
31 changes: 13 additions & 18 deletions src/server/backend/lib/logsaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LogSaverPrivate
static QMutex logMutex; // 同步使用的 mutex

int logLimitSize = 10 * 1024 * 1024; // 10M
int logExpiredDay = -30; // 30天过期
qsizetype logMaxFiles = 10; // 最多保存 10 个最新的备份日志文件
};

QMutex LogSaverPrivate::logMutex;
Expand All @@ -48,12 +48,12 @@ LogSaverPrivate::LogSaverPrivate(LogSaver *qq)
QString logPath = logDir.absoluteFilePath("app.log"); // 获取日志的路径
logFileCreatedDate = QFileInfo(logPath).lastModified().date(); // 若日志文件不存在,返回nullptr

// 10分钟检查一次日志文件创建时间
renameLogFileTimer.setInterval(1000 * 60 * 10);
// 3分钟检查一次日志文件创建时间
renameLogFileTimer.setInterval(1000 * 60 * 3);
QObject::connect(&renameLogFileTimer, &QTimer::timeout, [this] {
QMutexLocker locker(&LogSaverPrivate::logMutex);
backupLog();
autoDeleteLog(); // 自动删除7天前的日志
autoDeleteLog();
});
}

Expand Down Expand Up @@ -129,20 +129,15 @@ void LogSaverPrivate::autoDeleteLog()
if (logDir.isEmpty()) {
return;
}
QDateTime now = QDateTime::currentDateTime();
QDateTime dateTime1 = now.addDays(logExpiredDay);

QFileInfoList fileList = logDir.entryInfoList();
for (QFileInfo f: fileList) {
// "."和".."跳过
if (f.baseName() == "")
continue;

// 从日志文件后缀获取创建时间(例如 app.log.2023-04-14-00-04-12 > 2023-04-14-00-04-12)
QDateTime dateTime2 = QDateTime::fromString(f.suffix(), "yyyy-MM-dd-hh-mm-ss");
if (dateTime2.isValid() && dateTime2 < dateTime1) {
logDir.remove(f.absoluteFilePath());
}

QFileInfoList fileList = logDir.entryInfoList(QStringList() << "app.log.*", QDir::Files, QDir::Time|QDir::Reversed);
int delCount = fileList.count() - logMaxFiles;
if (delCount <= 0) {
return;
}

for (auto it = fileList.begin(); it != fileList.end() && --delCount >= 0; ++it) {
logDir.remove(it->absoluteFilePath());
}
}

Expand Down

0 comments on commit 848c1a6

Please sign in to comment.