@sphereon/ssi-types 0.30.1-unstable.4 → 0.30.1

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.
@@ -1,224 +1,224 @@
1
- import { debug } from 'debug'
2
- import { EventEmitter } from 'events'
3
-
4
- export enum LogLevel {
5
- TRACE = 0,
6
- DEBUG,
7
- INFO,
8
- WARNING,
9
- ERROR,
10
- }
11
-
12
- export enum LoggingEventType {
13
- AUDIT = 'audit',
14
- GENERAL = 'general',
15
- }
16
-
17
- export interface SimpleLogEvent {
18
- type: LoggingEventType.GENERAL
19
- level: LogLevel
20
- correlationId?: string
21
- timestamp: Date
22
- data: string
23
- diagnosticData?: any
24
- }
25
-
26
- export enum LogMethod {
27
- DEBUG_PKG,
28
- CONSOLE,
29
- EVENT,
30
- }
31
-
32
- export interface SimpleLogOptions {
33
- namespace?: string
34
- eventName?: string
35
- defaultLogLevel?: LogLevel
36
- methods?: LogMethod[]
37
- }
38
-
39
- export function logOptions(opts?: SimpleLogOptions): Required<SimpleLogOptions> {
40
- return {
41
- namespace: opts?.namespace ?? 'sphereon',
42
- eventName: opts?.eventName ?? 'sphereon:default',
43
- defaultLogLevel: opts?.defaultLogLevel ?? LogLevel.INFO,
44
- methods: opts?.methods ?? [LogMethod.DEBUG_PKG, LogMethod.EVENT],
45
- }
46
- }
47
-
48
- export class Loggers {
49
- private static readonly DEFAULT_KEY = '__DEFAULT__'
50
- public static readonly DEFAULT: Loggers = new Loggers({
51
- defaultLogLevel: LogLevel.INFO,
52
- methods: [LogMethod.DEBUG_PKG, LogMethod.EVENT],
53
- })
54
- private readonly namespaceOptions: Map<string, Required<SimpleLogOptions>> = new Map()
55
- private readonly loggers: WeakMap<Required<SimpleLogOptions>, ISimpleLogger<any>> = new WeakMap()
56
-
57
- constructor(defaultOptions?: Omit<SimpleLogOptions, 'namespace'>) {
58
- this.defaultOptions(logOptions(defaultOptions))
59
- }
60
-
61
- public options(namespace: string, options: Omit<SimpleLogOptions, 'namespace'>): this {
62
- this.namespaceOptions.set(namespace, logOptions({ ...options, namespace }))
63
- return this
64
- }
65
-
66
- public defaultOptions(options: Omit<SimpleLogOptions, 'namespace'>): this {
67
- this.options(Loggers.DEFAULT_KEY, options)
68
- return this
69
- }
70
-
71
- register<T>(namespace: string, logger: ISimpleLogger<T>): ISimpleLogger<T> {
72
- return this.get(namespace, logger)
73
- }
74
-
75
- get<T>(namespace: string, registerLogger?: ISimpleLogger<T>): ISimpleLogger<T> {
76
- const options = this.namespaceOptions.get(namespace) ?? registerLogger?.options ?? this.namespaceOptions.get(Loggers.DEFAULT_KEY)
77
- if (!options) {
78
- throw Error(`No logging options found for namespace ${namespace}`)
79
- }
80
- this.namespaceOptions.set(namespace, options)
81
-
82
- let logger = this.loggers.get(options)
83
- if (!logger) {
84
- logger = registerLogger ?? new SimpleLogger(options)
85
- this.loggers.set(options, logger)
86
- }
87
- return logger
88
- }
89
- }
90
-
91
- export type ISimpleLogger<LogType> = {
92
- options: Required<SimpleLogOptions>
93
- log(value: LogType, ...args: any[]): void
94
- info(value: LogType, ...args: any[]): void
95
- debug(value: LogType, ...args: any[]): void
96
- trace(value: LogType, ...args: any[]): void
97
- warning(value: LogType, ...args: any[]): void
98
- error(value: LogType, ...args: any[]): void
99
- logl(level: LogLevel, value: LogType, ...argsW: any[]): void
100
- }
101
-
102
- export class SimpleLogger implements ISimpleLogger<any> {
103
- private _eventEmitter = new EventEmitter({ captureRejections: true })
104
- private _options: Required<SimpleLogOptions>
105
-
106
- constructor(opts?: SimpleLogOptions) {
107
- this._options = logOptions(opts)
108
- }
109
-
110
- get eventEmitter(): EventEmitter {
111
- return this._eventEmitter
112
- }
113
-
114
- get options(): Required<SimpleLogOptions> {
115
- return this._options
116
- }
117
-
118
- trace(value: any, ...args: any[]) {
119
- this.logImpl(LogLevel.TRACE, value, ...args)
120
- }
121
-
122
- debug(value: any, ...args: any[]) {
123
- this.logImpl(LogLevel.DEBUG, value, ...args)
124
- }
125
-
126
- info(value: any, ...args: any[]) {
127
- this.logImpl(LogLevel.INFO, value, ...args)
128
- }
129
-
130
- warning(value: any, ...args: any[]) {
131
- this.logImpl(LogLevel.WARNING, value, ...args)
132
- }
133
-
134
- error(value: any, ...args: any[]) {
135
- this.logImpl(LogLevel.ERROR, value, ...args)
136
- }
137
-
138
- logl(level: LogLevel, value: any, ...args: any[]) {
139
- this.logImpl(level, value, ...args)
140
- }
141
-
142
- private logImpl(level: LogLevel, value: any, ...args: any[]) {
143
- const date = new Date().toISOString()
144
- const filteredArgs = args?.filter((v) => v!!) ?? []
145
- const arg = filteredArgs.length === 0 || filteredArgs[0] == undefined ? undefined : filteredArgs
146
-
147
- function toLogValue(options: SimpleLogOptions): any {
148
- if (typeof value === 'string') {
149
- return `${date}-(${options.namespace}) ${value}`
150
- } else if (typeof value === 'object') {
151
- value['namespace'] = options.namespace
152
- value['time'] = date
153
- }
154
- return value
155
- }
156
-
157
- const logValue = toLogValue(this.options)
158
- const logArgs = [logValue]
159
- if (arg) {
160
- logArgs.push(args)
161
- }
162
- let debugPkgEnabled = this.options.methods.includes(LogMethod.DEBUG_PKG)
163
- if (debugPkgEnabled) {
164
- const debugPkgDebugger = debug(this._options.namespace)
165
- // It was enabled at the options level in code, but could be disabled at runtime using env vars
166
- debugPkgEnabled = debugPkgDebugger.enabled
167
- if (debugPkgEnabled) {
168
- if (arg) {
169
- debugPkgDebugger(`${date}- ${value},`, ...arg)
170
- } else {
171
- debugPkgDebugger(`${date}- ${value}`)
172
- }
173
- }
174
- }
175
-
176
- // We do not perform console.logs in case the debug package is enabled in code and used at runtime
177
- if (this.options.methods.includes(LogMethod.CONSOLE) && !debugPkgEnabled) {
178
- const [value, args] = logArgs
179
- let logMethod = console.info
180
- switch (level) {
181
- case LogLevel.TRACE:
182
- logMethod = console.trace
183
- break
184
- case LogLevel.DEBUG:
185
- logMethod = console.debug
186
- break
187
- case LogLevel.INFO:
188
- logMethod = console.info
189
- break
190
- case LogLevel.WARNING:
191
- logMethod = console.warn
192
- break
193
- case LogLevel.ERROR:
194
- logMethod = console.error
195
- break
196
- }
197
- if (args) {
198
- logMethod(value + ',', ...args)
199
- } else {
200
- logMethod(value)
201
- }
202
- }
203
-
204
- if (this.options.methods.includes(LogMethod.EVENT)) {
205
- this._eventEmitter.emit(this.options.eventName, {
206
- data: value.toString(),
207
- timestamp: new Date(date),
208
- level,
209
- type: LoggingEventType.GENERAL,
210
- diagnosticData: logArgs,
211
- } satisfies SimpleLogEvent)
212
- }
213
- }
214
-
215
- log(value: any, ...args: any[]) {
216
- this.logImpl(this.options.defaultLogLevel, value, ...args)
217
- }
218
- }
219
-
220
- export class SimpleRecordLogger extends SimpleLogger implements ISimpleLogger<Record<string, any>> {
221
- constructor(opts?: SimpleLogOptions) {
222
- super(opts)
223
- }
224
- }
1
+ import { debug } from 'debug'
2
+ import { EventEmitter } from 'events'
3
+
4
+ export enum LogLevel {
5
+ TRACE = 0,
6
+ DEBUG,
7
+ INFO,
8
+ WARNING,
9
+ ERROR,
10
+ }
11
+
12
+ export enum LoggingEventType {
13
+ AUDIT = 'audit',
14
+ GENERAL = 'general',
15
+ }
16
+
17
+ export interface SimpleLogEvent {
18
+ type: LoggingEventType.GENERAL
19
+ level: LogLevel
20
+ correlationId?: string
21
+ timestamp: Date
22
+ data: string
23
+ diagnosticData?: any
24
+ }
25
+
26
+ export enum LogMethod {
27
+ DEBUG_PKG,
28
+ CONSOLE,
29
+ EVENT,
30
+ }
31
+
32
+ export interface SimpleLogOptions {
33
+ namespace?: string
34
+ eventName?: string
35
+ defaultLogLevel?: LogLevel
36
+ methods?: LogMethod[]
37
+ }
38
+
39
+ export function logOptions(opts?: SimpleLogOptions): Required<SimpleLogOptions> {
40
+ return {
41
+ namespace: opts?.namespace ?? 'sphereon',
42
+ eventName: opts?.eventName ?? 'sphereon:default',
43
+ defaultLogLevel: opts?.defaultLogLevel ?? LogLevel.INFO,
44
+ methods: opts?.methods ?? [LogMethod.DEBUG_PKG, LogMethod.EVENT],
45
+ }
46
+ }
47
+
48
+ export class Loggers {
49
+ private static readonly DEFAULT_KEY = '__DEFAULT__'
50
+ public static readonly DEFAULT: Loggers = new Loggers({
51
+ defaultLogLevel: LogLevel.INFO,
52
+ methods: [LogMethod.DEBUG_PKG, LogMethod.EVENT],
53
+ })
54
+ private readonly namespaceOptions: Map<string, Required<SimpleLogOptions>> = new Map()
55
+ private readonly loggers: WeakMap<Required<SimpleLogOptions>, ISimpleLogger<any>> = new WeakMap()
56
+
57
+ constructor(defaultOptions?: Omit<SimpleLogOptions, 'namespace'>) {
58
+ this.defaultOptions(logOptions(defaultOptions))
59
+ }
60
+
61
+ public options(namespace: string, options: Omit<SimpleLogOptions, 'namespace'>): this {
62
+ this.namespaceOptions.set(namespace, logOptions({ ...options, namespace }))
63
+ return this
64
+ }
65
+
66
+ public defaultOptions(options: Omit<SimpleLogOptions, 'namespace'>): this {
67
+ this.options(Loggers.DEFAULT_KEY, options)
68
+ return this
69
+ }
70
+
71
+ register<T>(namespace: string, logger: ISimpleLogger<T>): ISimpleLogger<T> {
72
+ return this.get(namespace, logger)
73
+ }
74
+
75
+ get<T>(namespace: string, registerLogger?: ISimpleLogger<T>): ISimpleLogger<T> {
76
+ const options = this.namespaceOptions.get(namespace) ?? registerLogger?.options ?? this.namespaceOptions.get(Loggers.DEFAULT_KEY)
77
+ if (!options) {
78
+ throw Error(`No logging options found for namespace ${namespace}`)
79
+ }
80
+ this.namespaceOptions.set(namespace, options)
81
+
82
+ let logger = this.loggers.get(options)
83
+ if (!logger) {
84
+ logger = registerLogger ?? new SimpleLogger(options)
85
+ this.loggers.set(options, logger)
86
+ }
87
+ return logger
88
+ }
89
+ }
90
+
91
+ export type ISimpleLogger<LogType> = {
92
+ options: Required<SimpleLogOptions>
93
+ log(value: LogType, ...args: any[]): void
94
+ info(value: LogType, ...args: any[]): void
95
+ debug(value: LogType, ...args: any[]): void
96
+ trace(value: LogType, ...args: any[]): void
97
+ warning(value: LogType, ...args: any[]): void
98
+ error(value: LogType, ...args: any[]): void
99
+ logl(level: LogLevel, value: LogType, ...argsW: any[]): void
100
+ }
101
+
102
+ export class SimpleLogger implements ISimpleLogger<any> {
103
+ private _eventEmitter = new EventEmitter({ captureRejections: true })
104
+ private _options: Required<SimpleLogOptions>
105
+
106
+ constructor(opts?: SimpleLogOptions) {
107
+ this._options = logOptions(opts)
108
+ }
109
+
110
+ get eventEmitter(): EventEmitter {
111
+ return this._eventEmitter
112
+ }
113
+
114
+ get options(): Required<SimpleLogOptions> {
115
+ return this._options
116
+ }
117
+
118
+ trace(value: any, ...args: any[]) {
119
+ this.logImpl(LogLevel.TRACE, value, ...args)
120
+ }
121
+
122
+ debug(value: any, ...args: any[]) {
123
+ this.logImpl(LogLevel.DEBUG, value, ...args)
124
+ }
125
+
126
+ info(value: any, ...args: any[]) {
127
+ this.logImpl(LogLevel.INFO, value, ...args)
128
+ }
129
+
130
+ warning(value: any, ...args: any[]) {
131
+ this.logImpl(LogLevel.WARNING, value, ...args)
132
+ }
133
+
134
+ error(value: any, ...args: any[]) {
135
+ this.logImpl(LogLevel.ERROR, value, ...args)
136
+ }
137
+
138
+ logl(level: LogLevel, value: any, ...args: any[]) {
139
+ this.logImpl(level, value, ...args)
140
+ }
141
+
142
+ private logImpl(level: LogLevel, value: any, ...args: any[]) {
143
+ const date = new Date().toISOString()
144
+ const filteredArgs = args?.filter((v) => v!!) ?? []
145
+ const arg = filteredArgs.length === 0 || filteredArgs[0] == undefined ? undefined : filteredArgs
146
+
147
+ function toLogValue(options: SimpleLogOptions): any {
148
+ if (typeof value === 'string') {
149
+ return `${date}-(${options.namespace}) ${value}`
150
+ } else if (typeof value === 'object') {
151
+ value['namespace'] = options.namespace
152
+ value['time'] = date
153
+ }
154
+ return value
155
+ }
156
+
157
+ const logValue = toLogValue(this.options)
158
+ const logArgs = [logValue]
159
+ if (arg) {
160
+ logArgs.push(args)
161
+ }
162
+ let debugPkgEnabled = this.options.methods.includes(LogMethod.DEBUG_PKG)
163
+ if (debugPkgEnabled) {
164
+ const debugPkgDebugger = debug(this._options.namespace)
165
+ // It was enabled at the options level in code, but could be disabled at runtime using env vars
166
+ debugPkgEnabled = debugPkgDebugger.enabled
167
+ if (debugPkgEnabled) {
168
+ if (arg) {
169
+ debugPkgDebugger(`${date}- ${value},`, ...arg)
170
+ } else {
171
+ debugPkgDebugger(`${date}- ${value}`)
172
+ }
173
+ }
174
+ }
175
+
176
+ // We do not perform console.logs in case the debug package is enabled in code and used at runtime
177
+ if (this.options.methods.includes(LogMethod.CONSOLE) && !debugPkgEnabled) {
178
+ const [value, args] = logArgs
179
+ let logMethod = console.info
180
+ switch (level) {
181
+ case LogLevel.TRACE:
182
+ logMethod = console.trace
183
+ break
184
+ case LogLevel.DEBUG:
185
+ logMethod = console.debug
186
+ break
187
+ case LogLevel.INFO:
188
+ logMethod = console.info
189
+ break
190
+ case LogLevel.WARNING:
191
+ logMethod = console.warn
192
+ break
193
+ case LogLevel.ERROR:
194
+ logMethod = console.error
195
+ break
196
+ }
197
+ if (args) {
198
+ logMethod(value + ',', ...args)
199
+ } else {
200
+ logMethod(value)
201
+ }
202
+ }
203
+
204
+ if (this.options.methods.includes(LogMethod.EVENT)) {
205
+ this._eventEmitter.emit(this.options.eventName, {
206
+ data: value.toString(),
207
+ timestamp: new Date(date),
208
+ level,
209
+ type: LoggingEventType.GENERAL,
210
+ diagnosticData: logArgs,
211
+ } satisfies SimpleLogEvent)
212
+ }
213
+ }
214
+
215
+ log(value: any, ...args: any[]) {
216
+ this.logImpl(this.options.defaultLogLevel, value, ...args)
217
+ }
218
+ }
219
+
220
+ export class SimpleRecordLogger extends SimpleLogger implements ISimpleLogger<Record<string, any>> {
221
+ constructor(opts?: SimpleLogOptions) {
222
+ super(opts)
223
+ }
224
+ }