vona-module-a-logger 5.0.9 → 5.0.11

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.
@@ -0,0 +1,85 @@
1
+ import type { BeanScopeUtil } from 'vona';
2
+ import type { IDecoratorBroadcastOptions } from 'vona-module-a-broadcast';
3
+ import type { IAopMethodOptionsLog } from '../bean/aopMethod.log.ts';
4
+ /** bean: end */
5
+ /** bean: begin */
6
+ import type { BeanLogger } from '../bean/bean.logger.ts';
7
+ /** broadcast: end */
8
+ /** broadcast: begin */
9
+ import type { BroadcastSetLevel } from '../bean/broadcast.setLevel.ts';
10
+ /** middlewareSystem: end */
11
+ /** aopMethod: begin */
12
+ import type { IMiddlewareSystemOptionsHttpLog } from '../bean/middlewareSystem.httpLog.ts';
13
+ /** broadcast: end */
14
+ /** scope: begin */
15
+ import { BeanScopeBase } from 'vona';
16
+ import 'vona';
17
+ import 'vona';
18
+ import 'vona';
19
+ import 'vona';
20
+ import 'vona';
21
+ export * from '../bean/aopMethod.log.ts';
22
+ declare module 'vona-module-a-aspect' {
23
+ interface IMiddlewareSystemRecord {
24
+ 'a-logger:httpLog': IMiddlewareSystemOptionsHttpLog;
25
+ }
26
+ }
27
+ declare module 'vona-module-a-logger' {
28
+ interface MiddlewareSystemHttpLog {
29
+ }
30
+ }
31
+ /** aopMethod: end */
32
+ /** bean: begin */
33
+ export * from '../bean/bean.logger.ts';
34
+ declare module 'vona-module-a-aspect' {
35
+ interface IAopMethodRecord {
36
+ 'a-logger:log': IAopMethodOptionsLog;
37
+ }
38
+ }
39
+ declare module 'vona-module-a-logger' {
40
+ interface AopMethodLog {
41
+ }
42
+ }
43
+ /** bean: end */
44
+ /** broadcast: begin */
45
+ export * from '../bean/broadcast.setLevel.ts';
46
+ declare module 'vona' {
47
+ }
48
+ declare module 'vona-module-a-logger' {
49
+ interface BeanLogger {
50
+ }
51
+ }
52
+ declare module 'vona' {
53
+ interface IBeanRecordGlobal {
54
+ logger: BeanLogger;
55
+ }
56
+ }
57
+ /** middlewareSystem: begin */
58
+ export * from '../bean/middlewareSystem.httpLog.ts';
59
+ declare module 'vona-module-a-broadcast' {
60
+ interface IBroadcastRecord {
61
+ 'a-logger:setLevel': IDecoratorBroadcastOptions;
62
+ }
63
+ }
64
+ declare module 'vona-module-a-logger' {
65
+ interface BroadcastSetLevel {
66
+ }
67
+ }
68
+ export interface IModuleBroadcast {
69
+ setLevel: BroadcastSetLevel;
70
+ }
71
+ export declare class ScopeModuleALogger extends BeanScopeBase {
72
+ }
73
+ export interface ScopeModuleALogger {
74
+ util: BeanScopeUtil;
75
+ broadcast: IModuleBroadcast;
76
+ }
77
+ declare module 'vona' {
78
+ interface IBeanScopeRecord {
79
+ 'a-logger': ScopeModuleALogger;
80
+ }
81
+ interface IBeanScopeContainer {
82
+ logger: ScopeModuleALogger;
83
+ }
84
+ }
85
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-logger";
2
+ export { ScopeModuleALogger as ScopeModule } from './index.ts';
@@ -0,0 +1,21 @@
1
+ import type { ILoggerClientChildRecord, ILoggerClientRecord, LoggerLevel, Next, NextSync } from 'vona';
2
+ import type { IAopMethodExecute, IAopMethodGet, IAopMethodSet, IDecoratorAopMethodOptions } from 'vona-module-a-aspect';
3
+ import type winston from 'winston';
4
+ import { BeanAopMethodBase } from 'vona';
5
+ export interface IAopMethodOptionsLog extends IDecoratorAopMethodOptions {
6
+ level: LoggerLevel;
7
+ childName?: keyof ILoggerClientChildRecord;
8
+ clientName?: keyof ILoggerClientRecord;
9
+ auto?: boolean;
10
+ args?: boolean;
11
+ result?: boolean;
12
+ context?: Record<string, any>;
13
+ }
14
+ export declare class AopMethodLog extends BeanAopMethodBase implements IAopMethodGet, IAopMethodSet, IAopMethodExecute {
15
+ get(options: IAopMethodOptionsLog, next: NextSync, receiver: any, prop: string): any;
16
+ set(options: IAopMethodOptionsLog, value: any, next: NextSync, receiver: any, prop: string): boolean;
17
+ execute(options: IAopMethodOptionsLog, _args: [], next: Next | NextSync, receiver: any, prop: string): Promise<any> | any;
18
+ _getContext(options: IAopMethodOptionsLog, receiver: any): unknown;
19
+ _logResult(profiler: winston.Profiler, context: any, res: any, options: IAopMethodOptionsLog, message: string): void;
20
+ _logError(profiler: winston.Profiler, context: any, err: Error, _options: IAopMethodOptionsLog, message: string): void;
21
+ }
@@ -0,0 +1,6 @@
1
+ import type { ILoggerClientRecord, LoggerLevel } from 'vona';
2
+ import { BeanBase } from 'vona';
3
+ export declare class BeanLogger extends BeanBase {
4
+ getLevel(clientName?: keyof ILoggerClientRecord): LoggerLevel | undefined;
5
+ setLevel(level: LoggerLevel | boolean, clientName?: keyof ILoggerClientRecord): void;
6
+ }
@@ -0,0 +1,10 @@
1
+ import type { ILoggerClientRecord, LoggerLevel } from 'vona';
2
+ import type { IBroadcastExecute } from 'vona-module-a-broadcast';
3
+ import { BeanBroadcastBase } from 'vona-module-a-broadcast';
4
+ export interface TypeBroadcastSetLevelJobData {
5
+ level: LoggerLevel | boolean;
6
+ clientName?: keyof ILoggerClientRecord;
7
+ }
8
+ export declare class BroadcastSetLevel extends BeanBroadcastBase<TypeBroadcastSetLevelJobData> implements IBroadcastExecute<TypeBroadcastSetLevelJobData> {
9
+ execute(data: TypeBroadcastSetLevelJobData, isEmitter?: boolean): Promise<void>;
10
+ }
@@ -0,0 +1,8 @@
1
+ import type { Next } from 'vona';
2
+ import type { IDecoratorMiddlewareSystemOptions, IMiddlewareSystemExecute } from 'vona-module-a-aspect';
3
+ import { BeanBase } from 'vona';
4
+ export interface IMiddlewareSystemOptionsHttpLog extends IDecoratorMiddlewareSystemOptions {
5
+ }
6
+ export declare class MiddlewareSystemHttpLog extends BeanBase implements IMiddlewareSystemExecute {
7
+ execute(_options: IMiddlewareSystemOptionsHttpLog, next: Next): Promise<void>;
8
+ }
@@ -0,0 +1,3 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './lib/index.ts';
3
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,166 @@
1
+ import { BeanInfo, BeanAopMethodBase, SymbolBeanFullName, BeanBase, BeanScopeBase } from 'vona';
2
+ import { Bean, Scope } from 'vona-module-a-bean';
3
+ import { evaluateExpressions } from '@cabloy/utils';
4
+ import { AopMethod, MiddlewareSystem, Aspect } from 'vona-module-a-aspect';
5
+ import { Broadcast, BeanBroadcastBase } from 'vona-module-a-broadcast';
6
+ import StdSerializers from 'pino-std-serializers';
7
+
8
+ var _dec$4, _dec2$4, _class$4;
9
+ let AopMethodLog = (_dec$4 = AopMethod({
10
+ level: 'info'
11
+ }), _dec2$4 = BeanInfo({
12
+ module: "a-logger"
13
+ }), _dec$4(_class$4 = _dec2$4(_class$4 = class AopMethodLog extends BeanAopMethodBase {
14
+ get(options, next, receiver, prop) {
15
+ const context = this._getContext(options, receiver);
16
+ const message = `${receiver[SymbolBeanFullName]}#${prop}(get)`;
17
+ const logger = this.app.meta.logger.child(options.childName, options.clientName);
18
+ // begin
19
+ !options.auto && logger.log(options.level, message, context ? {
20
+ context
21
+ } : undefined);
22
+ const profiler = logger.startTimer();
23
+ // next
24
+ try {
25
+ const res = next();
26
+ this._logResult(profiler, context, res, options, message);
27
+ return res;
28
+ } catch (err) {
29
+ this._logError(profiler, context, err, options, message);
30
+ throw err;
31
+ }
32
+ }
33
+ set(options, value, next, receiver, prop) {
34
+ const context = this._getContext(options, receiver);
35
+ const message = `${receiver[SymbolBeanFullName]}#${prop}(set)`;
36
+ const logger = this.app.meta.logger.child(options.childName, options.clientName);
37
+ // begin
38
+ logger.log(options.level, `${message} value: ${JSON.stringify(value)}`, context ? {
39
+ context
40
+ } : undefined);
41
+ const profiler = logger.startTimer();
42
+ // next
43
+ try {
44
+ const res = next();
45
+ !options.auto && this._logResult(profiler, context, undefined, options, message);
46
+ return res;
47
+ } catch (err) {
48
+ this._logError(profiler, context, err, options, message);
49
+ throw err;
50
+ }
51
+ }
52
+ execute(options, _args, next, receiver, prop) {
53
+ const context = this._getContext(options, receiver);
54
+ const message = `${receiver[SymbolBeanFullName]}#${prop}`;
55
+ const logger = this.app.meta.logger.child(options.childName, options.clientName);
56
+ // begin
57
+ options.args !== false && logger.log(options.level, `${message} args: ${JSON.stringify(_args)}`, context ? {
58
+ context
59
+ } : undefined);
60
+ const profiler = logger.startTimer();
61
+ // next
62
+ try {
63
+ const res = next();
64
+ if (res?.then) {
65
+ return res.then(res => {
66
+ options.result !== false && this._logResult(profiler, context, res, options, message);
67
+ return res;
68
+ }).catch(err => {
69
+ this._logError(profiler, context, err, options, message);
70
+ throw err;
71
+ });
72
+ }
73
+ options.result !== false && this._logResult(profiler, context, res, options, message);
74
+ return res;
75
+ } catch (err) {
76
+ this._logError(profiler, context, err, options, message);
77
+ throw err;
78
+ }
79
+ }
80
+ _getContext(options, receiver) {
81
+ return evaluateExpressions(options.context, receiver);
82
+ }
83
+ _logResult(profiler, context, res, options, message) {
84
+ const textResult = res !== undefined ? ` result: ${JSON.stringify(res)}` : '';
85
+ const info = {
86
+ level: options.level,
87
+ message: `${message}${textResult}`
88
+ };
89
+ if (context) info.context = context;
90
+ profiler.done(info);
91
+ }
92
+ _logError(profiler, context, err, _options, message) {
93
+ const textError = ` error: ${err.message}`;
94
+ const info = {
95
+ level: 'error',
96
+ message: `${message}${textError}`
97
+ };
98
+ if (context) info.context = context;
99
+ profiler.done(info);
100
+ }
101
+ }) || _class$4) || _class$4);
102
+
103
+ var _dec$3, _dec2$3, _class$3;
104
+ let BeanLogger = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
105
+ module: "a-logger"
106
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = class BeanLogger extends BeanBase {
107
+ getLevel(clientName) {
108
+ return this.app.meta.logger.getLevel(clientName);
109
+ }
110
+ setLevel(level, clientName) {
111
+ this.app.meta.logger.setLevel(level, clientName);
112
+ this.scope.broadcast.setLevel.emit({
113
+ level,
114
+ clientName
115
+ });
116
+ }
117
+ }) || _class$3) || _class$3);
118
+
119
+ var _dec$2, _dec2$2, _class$2;
120
+ let BroadcastSetLevel = (_dec$2 = Broadcast(), _dec2$2 = BeanInfo({
121
+ module: "a-logger"
122
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class BroadcastSetLevel extends BeanBroadcastBase {
123
+ async execute(data, isEmitter) {
124
+ if (!isEmitter) {
125
+ this.app.meta.logger.setLevel(data.level, data.clientName);
126
+ }
127
+ }
128
+ }) || _class$2) || _class$2);
129
+
130
+ var _dec$1, _dec2$1, _class$1;
131
+ let MiddlewareSystemHttpLog = (_dec$1 = MiddlewareSystem(), _dec2$1 = BeanInfo({
132
+ module: "a-logger"
133
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class MiddlewareSystemHttpLog extends BeanBase {
134
+ async execute(_options, next) {
135
+ const ctx = this.ctx;
136
+ // start
137
+ const req = StdSerializers.req(ctx.req);
138
+ this.$loggerChild('req').http(JSON.stringify(req));
139
+ const profiler = this.$loggerChild('res').startTimer();
140
+ // next
141
+ await next();
142
+ // end
143
+ const res = {
144
+ url: ctx.req.url,
145
+ statusCode: ctx.res.statusCode,
146
+ headers: ctx.res.getHeaders()
147
+ };
148
+ profiler.done({
149
+ level: 'http',
150
+ message: JSON.stringify(res)
151
+ });
152
+ }
153
+ }) || _class$1) || _class$1);
154
+
155
+ var _dec, _dec2, _class;
156
+ let ScopeModuleALogger = (_dec = Scope(), _dec2 = BeanInfo({
157
+ module: "a-logger"
158
+ }), _dec(_class = _dec2(_class = class ScopeModuleALogger extends BeanScopeBase {}) || _class) || _class);
159
+
160
+ /** scope: end */
161
+
162
+ function Log(options) {
163
+ return Aspect.aopMethod('a-logger:log', options);
164
+ }
165
+
166
+ export { AopMethodLog, BeanLogger, BroadcastSetLevel, Log, MiddlewareSystemHttpLog, ScopeModuleALogger };
@@ -0,0 +1 @@
1
+ export * from './log.ts';
@@ -0,0 +1,2 @@
1
+ import type { IAopMethodOptionsLog } from '../bean/aopMethod.log.js';
2
+ export declare function Log(options?: Partial<IAopMethodOptionsLog>): MethodDecorator;
@@ -0,0 +1 @@
1
+ export * from './logger.ts';
@@ -0,0 +1,7 @@
1
+ declare module 'vona' {
2
+ interface ILoggerClientChildRecord {
3
+ req: never;
4
+ res: never;
5
+ }
6
+ }
7
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-logger",
3
3
  "type": "module",
4
- "version": "5.0.9",
4
+ "version": "5.0.11",
5
5
  "title": "a-logger",
6
6
  "vonaModule": {
7
7
  "dependencies": {}
@@ -28,8 +28,12 @@
28
28
  "dependencies": {
29
29
  "pino-std-serializers": "^7.0.0"
30
30
  },
31
+ "devDependencies": {
32
+ "clean-package": "^2.2.0",
33
+ "rimraf": "^6.0.1"
34
+ },
31
35
  "scripts": {
32
- "clean": "rimraf dist tsconfig.tsbuildinfo",
33
- "tsc:publish": "npm run clean && tsc"
36
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
37
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
34
38
  }
35
39
  }