feat: writing logs to the filesystem with rotation support (#1299)
* refactor: move lib/policy/config to lib/config Signed-off-by: Xe Iaso <me@xeiaso.net> * refactor: don't set global loggers anymore Ref #864 You were right @kotx, it is a bad idea to set the global logger instance. Signed-off-by: Xe Iaso <me@xeiaso.net> * feat(config): add log sink support Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: update spelling Signed-off-by: Xe Iaso <me@xeiaso.net> * chore(test): go mod tidy Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: update spelling Signed-off-by: Xe Iaso <me@xeiaso.net> * docs(admin/policies): add logging block documentation Signed-off-by: Xe Iaso <me@xeiaso.net> * docs: update CHANGELOG Signed-off-by: Xe Iaso <me@xeiaso.net> * fix(cmd/anubis): revert this change, it's meant to be its own PR Signed-off-by: Xe Iaso <me@xeiaso.net> * chore: go mod tidy Signed-off-by: Xe Iaso <me@xeiaso.net> * test: add file logging smoke test Assisted-by: GLM 4.6 via Claude Code Signed-off-by: Xe Iaso <me@xeiaso.net> * fix: don't expose the old log file time format string Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
a709a2b2da
commit
f032d5d0ac
118 changed files with 789 additions and 65 deletions
88
test/log-file/test.mjs
Normal file
88
test/log-file/test.mjs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
import { statSync } from "fs";
|
||||
|
||||
async function getPage(path) {
|
||||
return fetch(`http://localhost:8923${path}`, {
|
||||
headers: {
|
||||
'User-Agent': 'CHALLENGE'
|
||||
}
|
||||
})
|
||||
.then(resp => {
|
||||
if (resp.status !== 200) {
|
||||
throw new Error(`wanted status 200, got status: ${resp.status}`);
|
||||
}
|
||||
return resp;
|
||||
})
|
||||
.then(resp => resp.text());
|
||||
}
|
||||
|
||||
async function getFileSize(filePath) {
|
||||
try {
|
||||
return statSync(filePath).size;
|
||||
} catch (error) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const logFilePath = "./var/anubis.log";
|
||||
|
||||
// Get initial log file size
|
||||
const initialSize = await getFileSize(logFilePath);
|
||||
console.log(`Initial log file size: ${initialSize} bytes`);
|
||||
|
||||
// Make 35 requests with different paths
|
||||
const requests = [];
|
||||
for (let i = 0; i < 35; i++) {
|
||||
requests.push(`/test${i}`);
|
||||
}
|
||||
|
||||
const resultSheet = {};
|
||||
let failed = false;
|
||||
|
||||
for (const path of requests) {
|
||||
try {
|
||||
const resp = await getPage(path);
|
||||
resultSheet[path] = {
|
||||
success: true,
|
||||
line: resp.split("\n")[0],
|
||||
};
|
||||
} catch (error) {
|
||||
resultSheet[path] = {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
console.log(`✗ Request to ${path} failed: ${error.message}`);
|
||||
failed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check final log file size
|
||||
const finalSize = await getFileSize(logFilePath);
|
||||
console.log(`Final log file size: ${finalSize} bytes`);
|
||||
console.log(`Size increase: ${finalSize - initialSize} bytes`);
|
||||
|
||||
// Verify that log file size increased
|
||||
if (finalSize <= initialSize) {
|
||||
console.error("ERROR: Log file size did not increase after making requests!");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
let successCount = 0;
|
||||
for (let [k, v] of Object.entries(resultSheet)) {
|
||||
if (!v.success) {
|
||||
console.error({ path: k, error: v.error });
|
||||
} else {
|
||||
successCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Successful requests: ${successCount}/${requests.length}`);
|
||||
|
||||
if (failed) {
|
||||
console.error("Test failed: Some requests failed or log file size did not increase");
|
||||
process.exit(1);
|
||||
} else {
|
||||
console.log("Test passed: All requests succeeded and log file size increased");
|
||||
process.exit(0);
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue