@mostajs/audit 2.1.2 → 2.2.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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { logAudit, getAuditUser } from './lib/audit';
2
+ export { dlog, dwarn, createLogger, isDebugEnabled, type ScopedLogger } from './lib/debug-log';
2
3
  export { AuditLogRepository } from './repositories/audit-log.repository';
3
4
  export { AuditLogSchema } from './schemas/audit-log.schema';
4
5
  export { createAuditHandlers } from './api/route';
package/dist/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
  // Author: Dr Hamid MADANI drmdh@msn.com
3
3
  // Core
4
4
  export { logAudit, getAuditUser } from './lib/audit';
5
+ // Volatile debug logger (complement de logAudit pour la trace pm2/stdout)
6
+ export { dlog, dwarn, createLogger, isDebugEnabled } from './lib/debug-log';
5
7
  // Repository & Schema
6
8
  export { AuditLogRepository } from './repositories/audit-log.repository';
7
9
  export { AuditLogSchema } from './schemas/audit-log.schema';
@@ -0,0 +1,16 @@
1
+ /** Log debug — actif uniquement si MOSTAJS_DEBUG=1 ou hors prod. */
2
+ export declare function dlog(scope: string, data: Record<string, unknown>): void;
3
+ /** Log warning — toujours actif (utile en prod pour les erreurs critiques). */
4
+ export declare function dwarn(scope: string, data: Record<string, unknown>): void;
5
+ export interface ScopedLogger {
6
+ dlog: (scope: string, data: Record<string, unknown>) => void;
7
+ dwarn: (scope: string, data: Record<string, unknown>) => void;
8
+ }
9
+ /**
10
+ * Crée un logger avec un préfixe namespace fixe — utile pour différencier
11
+ * les apps qui consomment le module dans le même environnement
12
+ * (ex: pm2 logs avec plusieurs services partageant la même console).
13
+ */
14
+ export declare function createLogger(namespace: string): ScopedLogger;
15
+ /** Pour les tests ou les apps qui veulent contrôler l'activation. */
16
+ export declare function isDebugEnabled(): boolean;
@@ -0,0 +1,69 @@
1
+ // @mosta/audit — Volatile debug logger
2
+ // Author: Dr Hamid MADANI <drmdh@msn.com>
3
+ //
4
+ // Logger de debug structuré complémentaire de `logAudit` :
5
+ // - `logAudit` persiste en DB → traçabilité long-terme, consultation /admin.
6
+ // - `dlog` / `dwarn` poussent sur stdout/stderr → grep en pm2 logs, désactivable.
7
+ //
8
+ // Activation :
9
+ // - Si `MOSTAJS_DEBUG=1` ou `=true` → activé (toutes envs).
10
+ // - Si `MOSTAJS_DEBUG=0` ou `=false` → désactivé explicitement.
11
+ // - Sinon : activé en non-production (`NODE_ENV !== 'production'`).
12
+ //
13
+ // Usage générique :
14
+ // ```ts
15
+ // import { dlog, dwarn } from '@mostajs/audit'
16
+ // dlog('myapp.scope', { key: 'value' }) // → [myapp.scope] { ... }
17
+ // ```
18
+ //
19
+ // Usage avec namespace dédié à une app :
20
+ // ```ts
21
+ // import { createLogger } from '@mostajs/audit'
22
+ // export const { dlog, dwarn } = createLogger('iquesta')
23
+ // // dlog('magic.enter', { ... }) → [iquesta.magic.enter] { ... }
24
+ // ```
25
+ const ENABLED = (() => {
26
+ const flag = process.env.MOSTAJS_DEBUG;
27
+ if (flag === '1' || flag === 'true')
28
+ return true;
29
+ if (flag === '0' || flag === 'false')
30
+ return false;
31
+ return process.env.NODE_ENV !== 'production';
32
+ })();
33
+ function emit(level, scope, data) {
34
+ // dwarn s'affiche même en prod (utile pour les erreurs critiques).
35
+ // dlog est gated par ENABLED.
36
+ if (level === 'info' && !ENABLED)
37
+ return;
38
+ const payload = { ts: new Date().toISOString(), ...data };
39
+ const target = level === 'warn' ? console.warn : console.info;
40
+ try {
41
+ target(`[${scope}]`, JSON.stringify(payload));
42
+ }
43
+ catch {
44
+ target(`[${scope}]`, payload);
45
+ }
46
+ }
47
+ /** Log debug — actif uniquement si MOSTAJS_DEBUG=1 ou hors prod. */
48
+ export function dlog(scope, data) {
49
+ emit('info', scope, data);
50
+ }
51
+ /** Log warning — toujours actif (utile en prod pour les erreurs critiques). */
52
+ export function dwarn(scope, data) {
53
+ emit('warn', scope, data);
54
+ }
55
+ /**
56
+ * Crée un logger avec un préfixe namespace fixe — utile pour différencier
57
+ * les apps qui consomment le module dans le même environnement
58
+ * (ex: pm2 logs avec plusieurs services partageant la même console).
59
+ */
60
+ export function createLogger(namespace) {
61
+ return {
62
+ dlog: (scope, data) => emit('info', `${namespace}.${scope}`, data),
63
+ dwarn: (scope, data) => emit('warn', `${namespace}.${scope}`, data),
64
+ };
65
+ }
66
+ /** Pour les tests ou les apps qui veulent contrôler l'activation. */
67
+ export function isDebugEnabled() {
68
+ return ENABLED;
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/audit",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "description": "Reusable audit logging module — fire-and-forget logAudit() with paginated consultation",
5
5
  "author": "Dr Hamid MADANI <drmdh@msn.com>",
6
6
  "license": "AGPL-3.0-or-later",