@mostajs/audit 2.1.1 → 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/LICENSE CHANGED
@@ -1,21 +1,29 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Dr Hamid MADANI <drmdh@msn.com>
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.
1
+ GNU AFFERO GENERAL PUBLIC LICENSE
2
+ Version 3, 19 November 2007
3
+
4
+ Copyright (c) 2026 Dr Hamid MADANI <drmdh@msn.com>
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Affero General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Affero General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Affero General Public License
17
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
18
+
19
+ COMMERCIAL LICENSE
20
+
21
+ For organizations that cannot comply with the AGPL open-source requirements,
22
+ a commercial license is available. Contact: drmdh@msn.com
23
+
24
+ The commercial license allows you to:
25
+ - Use the software in proprietary/closed-source projects
26
+ - Modify without publishing your source code
27
+ - Get priority support and SLA
28
+
29
+ Contact: Dr Hamid MADANI <drmdh@msn.com>
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,9 +1,9 @@
1
1
  {
2
2
  "name": "@mostajs/audit",
3
- "version": "2.1.1",
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
- "license": "MIT",
6
+ "license": "AGPL-3.0-or-later",
7
7
  "type": "module",
8
8
  "main": "dist/index.js",
9
9
  "types": "dist/index.d.ts",