guardian-risk-logger 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Guardian Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # guardian-risk-logger
2
+
3
+ > **Requires:** [`guardian-risk`](https://www.npmjs.com/package/guardian-risk) (core)
4
+
5
+ ```bash
6
+ npm install guardian-risk guardian-risk-logger
7
+ ```
8
+
9
+ > **Stub package** — API may change before `1.0.0`.
10
+
11
+ Audit logging for [guardian-risk](https://www.npmjs.com/package/guardian-risk). Records risk reports, matched rules, and scores for compliance and debugging.
12
+
13
+ ## What gets logged
14
+
15
+ | Field | Source |
16
+ |-------|--------|
17
+ | `score` | `report.score` |
18
+ | `riskLevel` | `report.level` |
19
+ | `matchedRules` | Count and details from `report.matchedRules` |
20
+ | `reasons` | `report.reasons` |
21
+ | `analyzedAt` | `report.analyzedAt` |
22
+
23
+ ## Usage (stub)
24
+
25
+ ```typescript
26
+ import { Guardian } from 'guardian-risk';
27
+ import { loggerPlugin, analyzeAndLog } from 'guardian-risk-logger';
28
+ import { vpnPlugin } from 'guardian-risk-vpn';
29
+
30
+ const guardian = new Guardian()
31
+ .use(loggerPlugin({ level: 'info', minScore: 20 }))
32
+ .use(vpnPlugin());
33
+
34
+ guardian.signal('vpn', true);
35
+
36
+ const report = analyzeAndLog(guardian, { minScore: 0 });
37
+ ```
38
+
39
+ ## Custom sink
40
+
41
+ ```typescript
42
+ import { loggerPlugin, logReport, type LogSink } from 'guardian-risk-logger';
43
+
44
+ const sink: LogSink = {
45
+ write(entry) {
46
+ // Send to Datadog, CloudWatch, file, etc.
47
+ myAuditService.record(entry);
48
+ },
49
+ };
50
+
51
+ const guardian = new Guardian().use(loggerPlugin({ sink }));
52
+ const report = guardian.analyze();
53
+ logReport(report, { sink });
54
+ ```
55
+
56
+ ## Status
57
+
58
+ Not yet published. Implementation in progress.
package/dist/index.cjs ADDED
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ var LEVEL_ORDER = {
5
+ debug: 0,
6
+ info: 1,
7
+ warn: 2,
8
+ error: 3
9
+ };
10
+ var defaultSink = {
11
+ write(entry) {
12
+ const payload = JSON.stringify({
13
+ level: entry.level,
14
+ message: entry.message,
15
+ score: entry.report.score,
16
+ riskLevel: entry.report.level,
17
+ matchedRules: entry.report.matchedRules.length,
18
+ timestamp: entry.timestamp
19
+ });
20
+ console.log(`[guardian-risk] ${payload}`);
21
+ }
22
+ };
23
+ function loggerPlugin(options = {}) {
24
+ const { level = "info", sink = defaultSink, minScore = 0 } = options;
25
+ return {
26
+ name: "guardian-risk-logger",
27
+ install(_guardian) {
28
+ }
29
+ };
30
+ }
31
+ function logReport(report, options = {}) {
32
+ const { level = "info", sink = defaultSink, minScore = 0 } = options;
33
+ if (report.score < minScore) {
34
+ return;
35
+ }
36
+ const entryLevel = resolveLogLevel(report, level);
37
+ sink.write({
38
+ level: entryLevel,
39
+ message: `Risk analysis complete: ${report.level} (${report.score})`,
40
+ report,
41
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
42
+ });
43
+ }
44
+ function analyzeAndLog(guardian, options = {}) {
45
+ const report = guardian.analyze();
46
+ logReport(report, options);
47
+ return report;
48
+ }
49
+ function resolveLogLevel(report, configured) {
50
+ if (report.level === "CRITICAL") {
51
+ return "error";
52
+ }
53
+ if (report.level === "HIGH") {
54
+ return "warn";
55
+ }
56
+ return configured;
57
+ }
58
+ function shouldLog(configured, actual) {
59
+ return LEVEL_ORDER[actual] >= LEVEL_ORDER[configured];
60
+ }
61
+
62
+ exports.analyzeAndLog = analyzeAndLog;
63
+ exports.logReport = logReport;
64
+ exports.loggerPlugin = loggerPlugin;
65
+ exports.shouldLog = shouldLog;
66
+ //# sourceMappingURL=index.cjs.map
67
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AA4BA,IAAM,WAAA,GAAwC;AAAA,EAC5C,KAAA,EAAO,CAAA;AAAA,EACP,IAAA,EAAM,CAAA;AAAA,EACN,IAAA,EAAM,CAAA;AAAA,EACN,KAAA,EAAO;AACT,CAAA;AAEA,IAAM,WAAA,GAAuB;AAAA,EAC3B,MAAM,KAAA,EAAa;AACjB,IAAA,MAAM,OAAA,GAAU,KAAK,SAAA,CAAU;AAAA,MAC7B,OAAO,KAAA,CAAM,KAAA;AAAA,MACb,SAAS,KAAA,CAAM,OAAA;AAAA,MACf,KAAA,EAAO,MAAM,MAAA,CAAO,KAAA;AAAA,MACpB,SAAA,EAAW,MAAM,MAAA,CAAO,KAAA;AAAA,MACxB,YAAA,EAAc,KAAA,CAAM,MAAA,CAAO,YAAA,CAAa,MAAA;AAAA,MACxC,WAAW,KAAA,CAA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/B,IAAA,OAAO,OAAA;AAAA,EACT;AACA,EAAA,IAAI,MAAA,CAAO,UAAU,MAAA,EAAQ;AAC3B,IAAA,OAAO,MAAA;AAAA,EACT;AACA,EAAA,OAAO,UAAA;AACT;AAEA,SAAS,SAAA,CAAU,YAAsB,MAAA,EAA2B;AAClE,EAAA,OAAO,WAAA,CAAY,MAAM,CAAA,IAAK,WAAA,CAAY,UAAU,CAAA;AACtD","file":"index.cjs","sourcesContent":["import type { Plugin, RiskReport } from 'guardian-risk';\n\n/** Log severity level. */\nexport type LogLevel = 'debug' | 'info' | 'warn' | 'error';\n\n/** Sink that receives structured log entries. */\nexport interface LogSink {\n write(entry: LogEntry): void;\n}\n\n/** Structured log entry for a risk analysis. */\nexport interface LogEntry {\n readonly level: LogLevel;\n readonly message: string;\n readonly report: RiskReport;\n readonly timestamp: string;\n}\n\n/** Options for the logger plugin (stub). */\nexport interface LoggerPluginOptions {\n /** Minimum level to emit. */\n readonly level?: LogLevel;\n /** Custom sink. Defaults to console. */\n readonly sink?: LogSink;\n /** Log only when score exceeds this threshold. */\n readonly minScore?: number;\n}\n\nconst LEVEL_ORDER: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\nconst defaultSink: LogSink = {\n write(entry): void {\n const payload = JSON.stringify({\n level: entry.level,\n message: entry.message,\n score: entry.report.score,\n riskLevel: entry.report.level,\n matchedRules: entry.report.matchedRules.length,\n timestamp: entry.timestamp,\n });\n console.log(`[guardian-risk] ${payload}`);\n },\n};\n\n/**\n * Logger plugin for guardian-risk.\n *\n * @stub Future versions will hook into analyze() automatically.\n * For now, use `logReport()` after `guardian.analyze()`.\n */\nexport function loggerPlugin(options: LoggerPluginOptions = {}): Plugin {\n const { level = 'info', sink = defaultSink, minScore = 0 } = options;\n\n return {\n name: 'guardian-risk-logger',\n install(_guardian) {\n void level;\n void sink;\n void minScore;\n // Stub: will wrap analyze() and emit audit logs automatically\n },\n };\n}\n\n/**\n * Log a risk report to the configured sink.\n */\nexport function logReport(\n report: RiskReport,\n options: LoggerPluginOptions = {},\n): void {\n const { level = 'info', sink = defaultSink, minScore = 0 } = options;\n\n if (report.score < minScore) {\n return;\n }\n\n const entryLevel = resolveLogLevel(report, level);\n\n sink.write({\n level: entryLevel,\n message: `Risk analysis complete: ${report.level} (${report.score})`,\n report,\n timestamp: new Date().toISOString(),\n });\n}\n\n/**\n * Analyze and log in one step.\n */\nexport function analyzeAndLog(\n guardian: import('guardian-risk').Guardian,\n options: LoggerPluginOptions = {},\n): RiskReport {\n const report = guardian.analyze();\n logReport(report, options);\n return report;\n}\n\nfunction resolveLogLevel(report: RiskReport, configured: LogLevel): LogLevel {\n if (report.level === 'CRITICAL') {\n return 'error';\n }\n if (report.level === 'HIGH') {\n return 'warn';\n }\n return configured;\n}\n\nfunction shouldLog(configured: LogLevel, actual: LogLevel): boolean {\n return LEVEL_ORDER[actual] >= LEVEL_ORDER[configured];\n}\n\nexport { shouldLog };\n"]}
@@ -0,0 +1,43 @@
1
+ import * as guardian_risk from 'guardian-risk';
2
+ import { RiskReport, Plugin } from 'guardian-risk';
3
+
4
+ /** Log severity level. */
5
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error';
6
+ /** Sink that receives structured log entries. */
7
+ interface LogSink {
8
+ write(entry: LogEntry): void;
9
+ }
10
+ /** Structured log entry for a risk analysis. */
11
+ interface LogEntry {
12
+ readonly level: LogLevel;
13
+ readonly message: string;
14
+ readonly report: RiskReport;
15
+ readonly timestamp: string;
16
+ }
17
+ /** Options for the logger plugin (stub). */
18
+ interface LoggerPluginOptions {
19
+ /** Minimum level to emit. */
20
+ readonly level?: LogLevel;
21
+ /** Custom sink. Defaults to console. */
22
+ readonly sink?: LogSink;
23
+ /** Log only when score exceeds this threshold. */
24
+ readonly minScore?: number;
25
+ }
26
+ /**
27
+ * Logger plugin for guardian-risk.
28
+ *
29
+ * @stub Future versions will hook into analyze() automatically.
30
+ * For now, use `logReport()` after `guardian.analyze()`.
31
+ */
32
+ declare function loggerPlugin(options?: LoggerPluginOptions): Plugin;
33
+ /**
34
+ * Log a risk report to the configured sink.
35
+ */
36
+ declare function logReport(report: RiskReport, options?: LoggerPluginOptions): void;
37
+ /**
38
+ * Analyze and log in one step.
39
+ */
40
+ declare function analyzeAndLog(guardian: guardian_risk.Guardian, options?: LoggerPluginOptions): RiskReport;
41
+ declare function shouldLog(configured: LogLevel, actual: LogLevel): boolean;
42
+
43
+ export { type LogEntry, type LogLevel, type LogSink, type LoggerPluginOptions, analyzeAndLog, logReport, loggerPlugin, shouldLog };
@@ -0,0 +1,43 @@
1
+ import * as guardian_risk from 'guardian-risk';
2
+ import { RiskReport, Plugin } from 'guardian-risk';
3
+
4
+ /** Log severity level. */
5
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error';
6
+ /** Sink that receives structured log entries. */
7
+ interface LogSink {
8
+ write(entry: LogEntry): void;
9
+ }
10
+ /** Structured log entry for a risk analysis. */
11
+ interface LogEntry {
12
+ readonly level: LogLevel;
13
+ readonly message: string;
14
+ readonly report: RiskReport;
15
+ readonly timestamp: string;
16
+ }
17
+ /** Options for the logger plugin (stub). */
18
+ interface LoggerPluginOptions {
19
+ /** Minimum level to emit. */
20
+ readonly level?: LogLevel;
21
+ /** Custom sink. Defaults to console. */
22
+ readonly sink?: LogSink;
23
+ /** Log only when score exceeds this threshold. */
24
+ readonly minScore?: number;
25
+ }
26
+ /**
27
+ * Logger plugin for guardian-risk.
28
+ *
29
+ * @stub Future versions will hook into analyze() automatically.
30
+ * For now, use `logReport()` after `guardian.analyze()`.
31
+ */
32
+ declare function loggerPlugin(options?: LoggerPluginOptions): Plugin;
33
+ /**
34
+ * Log a risk report to the configured sink.
35
+ */
36
+ declare function logReport(report: RiskReport, options?: LoggerPluginOptions): void;
37
+ /**
38
+ * Analyze and log in one step.
39
+ */
40
+ declare function analyzeAndLog(guardian: guardian_risk.Guardian, options?: LoggerPluginOptions): RiskReport;
41
+ declare function shouldLog(configured: LogLevel, actual: LogLevel): boolean;
42
+
43
+ export { type LogEntry, type LogLevel, type LogSink, type LoggerPluginOptions, analyzeAndLog, logReport, loggerPlugin, shouldLog };
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ // src/index.ts
2
+ var LEVEL_ORDER = {
3
+ debug: 0,
4
+ info: 1,
5
+ warn: 2,
6
+ error: 3
7
+ };
8
+ var defaultSink = {
9
+ write(entry) {
10
+ const payload = JSON.stringify({
11
+ level: entry.level,
12
+ message: entry.message,
13
+ score: entry.report.score,
14
+ riskLevel: entry.report.level,
15
+ matchedRules: entry.report.matchedRules.length,
16
+ timestamp: entry.timestamp
17
+ });
18
+ console.log(`[guardian-risk] ${payload}`);
19
+ }
20
+ };
21
+ function loggerPlugin(options = {}) {
22
+ const { level = "info", sink = defaultSink, minScore = 0 } = options;
23
+ return {
24
+ name: "guardian-risk-logger",
25
+ install(_guardian) {
26
+ }
27
+ };
28
+ }
29
+ function logReport(report, options = {}) {
30
+ const { level = "info", sink = defaultSink, minScore = 0 } = options;
31
+ if (report.score < minScore) {
32
+ return;
33
+ }
34
+ const entryLevel = resolveLogLevel(report, level);
35
+ sink.write({
36
+ level: entryLevel,
37
+ message: `Risk analysis complete: ${report.level} (${report.score})`,
38
+ report,
39
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
40
+ });
41
+ }
42
+ function analyzeAndLog(guardian, options = {}) {
43
+ const report = guardian.analyze();
44
+ logReport(report, options);
45
+ return report;
46
+ }
47
+ function resolveLogLevel(report, configured) {
48
+ if (report.level === "CRITICAL") {
49
+ return "error";
50
+ }
51
+ if (report.level === "HIGH") {
52
+ return "warn";
53
+ }
54
+ return configured;
55
+ }
56
+ function shouldLog(configured, actual) {
57
+ return LEVEL_ORDER[actual] >= LEVEL_ORDER[configured];
58
+ }
59
+
60
+ export { analyzeAndLog, logReport, loggerPlugin, shouldLog };
61
+ //# sourceMappingURL=index.js.map
62
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3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hD,EAAA,IAAA,CAAK,KAAA,CAAM;AAAA,IACT,KAAA,EAAO,UAAA;AAAA,IACP,SAAS,CAAA,wBAAA,EAA2B,MAAA,CAAO,KAAK,CAAA,EAAA,EAAK,OAAO,KAAK,CAAA,CAAA,CAAA;AAAA,IACjE,MAAA;AAAA,IACA,SAAA,EAAA,iBAAW,IAAI,IAAA,EAAK,EAAE,WAAA;AAAY,GACnC,CAAA;AACH;AAKO,SAAS,aAAA,CACd,QAAA,EACA,OAAA,GAA+B,EAAC,EACpB;AACZ,EAAA,MAAM,MAAA,GAAS,SAAS,OAAA,EAAQ;AAChC,EAAA,SAAA,CAAU,QAAQ,OAAO,CAAA;AACzB,EAAA,OAAO,MAAA;AACT;AAEA,SAAS,eAAA,CAAgB,QAAoB,UAAA,EAAgC;AAC3E,EAAA,IAAI,MAAA,CAAO,UAAU,UAAA,EAAY;AAC/B,IAAA,OAAO,OAAA;AAAA,EACT;AACA,EAAA,IAAI,MAAA,CAAO,UAAU,MAAA,EAAQ;AAC3B,IAAA,OAAO,MAAA;AAAA,EACT;AACA,EAAA,OAAO,UAAA;AACT;AAEA,SAAS,SAAA,CAAU,YAAsB,MAAA,EAA2B;AAClE,EAAA,OAAO,WAAA,CAAY,MAAM,CAAA,IAAK,WAAA,CAAY,UAAU,CAAA;AACtD","file":"index.js","sourcesContent":["import type { Plugin, RiskReport } from 'guardian-risk';\n\n/** Log severity level. */\nexport type LogLevel = 'debug' | 'info' | 'warn' | 'error';\n\n/** Sink that receives structured log entries. */\nexport interface LogSink {\n write(entry: LogEntry): void;\n}\n\n/** Structured log entry for a risk analysis. */\nexport interface LogEntry {\n readonly level: LogLevel;\n readonly message: string;\n readonly report: RiskReport;\n readonly timestamp: string;\n}\n\n/** Options for the logger plugin (stub). */\nexport interface LoggerPluginOptions {\n /** Minimum level to emit. */\n readonly level?: LogLevel;\n /** Custom sink. Defaults to console. */\n readonly sink?: LogSink;\n /** Log only when score exceeds this threshold. */\n readonly minScore?: number;\n}\n\nconst LEVEL_ORDER: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n};\n\nconst defaultSink: LogSink = {\n write(entry): void {\n const payload = JSON.stringify({\n level: entry.level,\n message: entry.message,\n score: entry.report.score,\n riskLevel: entry.report.level,\n matchedRules: entry.report.matchedRules.length,\n timestamp: entry.timestamp,\n });\n console.log(`[guardian-risk] ${payload}`);\n },\n};\n\n/**\n * Logger plugin for guardian-risk.\n *\n * @stub Future versions will hook into analyze() automatically.\n * For now, use `logReport()` after `guardian.analyze()`.\n */\nexport function loggerPlugin(options: LoggerPluginOptions = {}): Plugin {\n const { level = 'info', sink = defaultSink, minScore = 0 } = options;\n\n return {\n name: 'guardian-risk-logger',\n install(_guardian) {\n void level;\n void sink;\n void minScore;\n // Stub: will wrap analyze() and emit audit logs automatically\n },\n };\n}\n\n/**\n * Log a risk report to the configured sink.\n */\nexport function logReport(\n report: RiskReport,\n options: LoggerPluginOptions = {},\n): void {\n const { level = 'info', sink = defaultSink, minScore = 0 } = options;\n\n if (report.score < minScore) {\n return;\n }\n\n const entryLevel = resolveLogLevel(report, level);\n\n sink.write({\n level: entryLevel,\n message: `Risk analysis complete: ${report.level} (${report.score})`,\n report,\n timestamp: new Date().toISOString(),\n });\n}\n\n/**\n * Analyze and log in one step.\n */\nexport function analyzeAndLog(\n guardian: import('guardian-risk').Guardian,\n options: LoggerPluginOptions = {},\n): RiskReport {\n const report = guardian.analyze();\n logReport(report, options);\n return report;\n}\n\nfunction resolveLogLevel(report: RiskReport, configured: LogLevel): LogLevel {\n if (report.level === 'CRITICAL') {\n return 'error';\n }\n if (report.level === 'HIGH') {\n return 'warn';\n }\n return configured;\n}\n\nfunction shouldLog(configured: LogLevel, actual: LogLevel): boolean {\n return LEVEL_ORDER[actual] >= LEVEL_ORDER[configured];\n}\n\nexport { shouldLog };\n"]}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "guardian-risk-logger",
3
+ "version": "0.1.0",
4
+ "description": "Audit logging plugin for guardian-risk — logs reports and matched rules",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "sideEffects": false,
22
+ "files": [
23
+ "dist",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "engines": {
28
+ "node": ">=20"
29
+ },
30
+ "keywords": [
31
+ "guardian-risk",
32
+ "guardian-risk-logger",
33
+ "logger",
34
+ "audit",
35
+ "logging",
36
+ "risk",
37
+ "security"
38
+ ],
39
+ "license": "MIT",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/himanshu6306singh/guardian-risk.git",
43
+ "directory": "packages/logger"
44
+ },
45
+ "homepage": "https://github.com/himanshu6306singh/guardian-risk/tree/main/packages/logger#readme",
46
+ "bugs": {
47
+ "url": "https://github.com/himanshu6306singh/guardian-risk/issues"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "peerDependencies": {
53
+ "guardian-risk": "^0.2.0"
54
+ },
55
+ "devDependencies": {
56
+ "tsup": "^8.3.5",
57
+ "typescript": "^5.7.2",
58
+ "guardian-risk": "0.2.0"
59
+ },
60
+ "scripts": {
61
+ "build": "tsup",
62
+ "typecheck": "tsc --noEmit"
63
+ }
64
+ }