@cepseudo/adonis-audit-log 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ This will:
|
|
|
26
26
|
|
|
27
27
|
1. Publish migrations to `database/migrations/`
|
|
28
28
|
2. Publish config to `config/audit.ts`
|
|
29
|
-
3. Register the provider in `adonisrc.ts`
|
|
29
|
+
3. Register the provider and command in `adonisrc.ts`
|
|
30
30
|
|
|
31
31
|
Then run the migrations:
|
|
32
32
|
|
|
@@ -62,10 +62,11 @@ export default defineConfig({
|
|
|
62
62
|
enabled: true,
|
|
63
63
|
},
|
|
64
64
|
|
|
65
|
+
// Retention in days (0 = unlimited)
|
|
65
66
|
retention: {
|
|
66
67
|
requestLogs: 30,
|
|
67
68
|
errorLogs: 90,
|
|
68
|
-
auditLogs:
|
|
69
|
+
auditLogs: 0, // Keep forever
|
|
69
70
|
},
|
|
70
71
|
})
|
|
71
72
|
```
|
|
@@ -120,6 +121,16 @@ export default class HttpExceptionHandler extends ExceptionHandler {
|
|
|
120
121
|
}
|
|
121
122
|
```
|
|
122
123
|
|
|
124
|
+
### Log Cleanup
|
|
125
|
+
|
|
126
|
+
Delete old logs based on retention configuration:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
node ace audit:cleanup
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Set retention to `0` for unlimited retention (logs won't be deleted).
|
|
133
|
+
|
|
123
134
|
## Database Schema
|
|
124
135
|
|
|
125
136
|
The package creates three tables:
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BaseCommand } from '@adonisjs/core/ace';
|
|
2
|
+
import { CommandOptions } from '@adonisjs/core/types/ace';
|
|
3
|
+
export default class AuditCleanup extends BaseCommand {
|
|
4
|
+
static commandName: string;
|
|
5
|
+
static description: string;
|
|
6
|
+
static options: CommandOptions;
|
|
7
|
+
run(): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|--------------------------------------------------------------------------
|
|
3
|
+
| Audit Cleanup Command
|
|
4
|
+
|--------------------------------------------------------------------------
|
|
5
|
+
|
|
|
6
|
+
| Command to delete old audit logs based on retention configuration.
|
|
7
|
+
| Run with: node ace audit:cleanup
|
|
8
|
+
|
|
|
9
|
+
*/
|
|
10
|
+
import { BaseCommand } from '@adonisjs/core/ace';
|
|
11
|
+
import { DateTime } from 'luxon';
|
|
12
|
+
export default class AuditCleanup extends BaseCommand {
|
|
13
|
+
static commandName = 'audit:cleanup';
|
|
14
|
+
static description = 'Delete old audit logs based on retention configuration';
|
|
15
|
+
static options = {
|
|
16
|
+
startApp: true,
|
|
17
|
+
};
|
|
18
|
+
async run() {
|
|
19
|
+
const { default: AuditLog } = await import('../src/models/audit_log.js');
|
|
20
|
+
const { default: RequestLog } = await import('../src/models/request_log.js');
|
|
21
|
+
const { default: ErrorLog } = await import('../src/models/error_log.js');
|
|
22
|
+
const config = this.app.config.get('audit');
|
|
23
|
+
if (!config) {
|
|
24
|
+
this.logger.error('Audit config not found. Make sure config/audit.ts exists.');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const retention = config.retention;
|
|
28
|
+
let totalDeleted = 0;
|
|
29
|
+
// Cleanup audit logs
|
|
30
|
+
if (retention.auditLogs > 0) {
|
|
31
|
+
const cutoff = DateTime.now().minus({ days: retention.auditLogs });
|
|
32
|
+
const deleted = await AuditLog.query().where('createdAt', '<', cutoff.toJSDate()).delete();
|
|
33
|
+
const count = Array.isArray(deleted) ? deleted.length : deleted;
|
|
34
|
+
this.logger.info(`Deleted ${count} audit logs older than ${retention.auditLogs} days`);
|
|
35
|
+
totalDeleted += count;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.logger.info('Audit logs: retention unlimited (skipped)');
|
|
39
|
+
}
|
|
40
|
+
// Cleanup request logs
|
|
41
|
+
if (retention.requestLogs > 0) {
|
|
42
|
+
const cutoff = DateTime.now().minus({ days: retention.requestLogs });
|
|
43
|
+
const deleted = await RequestLog.query().where('createdAt', '<', cutoff.toJSDate()).delete();
|
|
44
|
+
const count = Array.isArray(deleted) ? deleted.length : deleted;
|
|
45
|
+
this.logger.info(`Deleted ${count} request logs older than ${retention.requestLogs} days`);
|
|
46
|
+
totalDeleted += count;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.logger.info('Request logs: retention unlimited (skipped)');
|
|
50
|
+
}
|
|
51
|
+
// Cleanup error logs
|
|
52
|
+
if (retention.errorLogs > 0) {
|
|
53
|
+
const cutoff = DateTime.now().minus({ days: retention.errorLogs });
|
|
54
|
+
const deleted = await ErrorLog.query().where('createdAt', '<', cutoff.toJSDate()).delete();
|
|
55
|
+
const count = Array.isArray(deleted) ? deleted.length : deleted;
|
|
56
|
+
this.logger.info(`Deleted ${count} error logs older than ${retention.errorLogs} days`);
|
|
57
|
+
totalDeleted += count;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.logger.info('Error logs: retention unlimited (skipped)');
|
|
61
|
+
}
|
|
62
|
+
this.logger.success(`Cleanup complete. Total deleted: ${totalDeleted}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
package/build/configure.js
CHANGED
|
@@ -49,10 +49,12 @@ export async function configure(command) {
|
|
|
49
49
|
fileName: `${timestamp3}_create_error_logs_table.ts`,
|
|
50
50
|
},
|
|
51
51
|
});
|
|
52
|
-
// Register provider in adonisrc.ts
|
|
52
|
+
// Register provider and command in adonisrc.ts
|
|
53
53
|
await codemods.updateRcFile((rcFile) => {
|
|
54
54
|
rcFile.addProvider('@cepseudo/adonis-audit-log/providers/audit_provider');
|
|
55
|
+
rcFile.addCommand('@cepseudo/adonis-audit-log/commands');
|
|
55
56
|
});
|
|
56
57
|
command.logger.success('Audit log package configured successfully!');
|
|
57
58
|
command.logger.info('Run "node ace migration:run" to create the audit tables.');
|
|
59
|
+
command.logger.info('Use "node ace audit:cleanup" to delete old logs based on retention config.');
|
|
58
60
|
}
|
package/build/stubs/config.stub
CHANGED
|
@@ -31,10 +31,11 @@ export default defineConfig({
|
|
|
31
31
|
enabled: true,
|
|
32
32
|
},
|
|
33
33
|
|
|
34
|
-
// Retention
|
|
34
|
+
// Retention in days (0 = unlimited, no cleanup)
|
|
35
|
+
// Run "node ace audit:cleanup" to delete old logs
|
|
35
36
|
retention: {
|
|
36
37
|
requestLogs: 30,
|
|
37
38
|
errorLogs: 90,
|
|
38
|
-
auditLogs:
|
|
39
|
+
auditLogs: 0, // Keep forever
|
|
39
40
|
},
|
|
40
41
|
})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cepseudo/adonis-audit-log",
|
|
3
3
|
"description": "Simple audit logging for AdonisJS 6",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20.6.0"
|
|
7
7
|
},
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"build/stubs",
|
|
15
15
|
"build/services",
|
|
16
16
|
"build/middleware",
|
|
17
|
+
"build/commands",
|
|
17
18
|
"build/index.d.ts",
|
|
18
19
|
"build/index.js",
|
|
19
20
|
"build/configure.d.ts",
|
|
@@ -24,6 +25,7 @@
|
|
|
24
25
|
"./services/main": "./build/services/main.js",
|
|
25
26
|
"./middleware/request_logger": "./build/middleware/request_logger.js",
|
|
26
27
|
"./providers/audit_provider": "./build/providers/audit_provider.js",
|
|
28
|
+
"./commands": "./build/commands/audit_cleanup.js",
|
|
27
29
|
"./types": "./build/src/types.js"
|
|
28
30
|
},
|
|
29
31
|
"scripts": {
|