Skip to content

Commit 3003009

Browse files
committed
feat(@sirutils/wizard): service logger
1 parent 866488f commit 3003009

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/wizard/src/utils/internals/logger.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { BlobType } from '@sirutils/core'
2-
import { type LogLevels, Loggers } from 'moleculer'
1+
import type { BlobType, Fn } from '@sirutils/core'
2+
import { type LogLevels, Loggers } from 'moleculer'
33

44
import { logger } from '../../internal/logger'
55
import { wizardTags } from '../../tag'
@@ -16,3 +16,19 @@ export class WizardLogger extends Loggers.Base {
1616
return (type: LogLevels, args: BlobType[]) => found[type](...args)
1717
}
1818
}
19+
20+
export const createServiceLogger = (name: string) => {
21+
const serviceLogger = logger.create({
22+
defaults: {
23+
tag: `${wizardTags.service}.${name}`
24+
}
25+
})
26+
27+
//Get all level from logger instance
28+
const logLevels = Object.keys(serviceLogger.options.types) as LogLevels[]
29+
30+
//Get logger functions of all levels from the logger instance and create an object with them
31+
return Object.fromEntries(logLevels.map((level) => {
32+
return [level, serviceLogger[level] as Fn<BlobType, void>]
33+
}))
34+
}

packages/wizard/src/utils/internals/service.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { GatewayResponse, IncomingRequest } from 'moleculer-web'
44

55
import { wizardTags } from '../../tag'
66
import { toMethod } from '../toMethod'
7+
import { createServiceLogger } from './logger'
78

89
export const serviceActions = createActions(
910
(context: Sirutils.Wizard.Context): Sirutils.Wizard.ServiceApi => {
@@ -49,7 +50,13 @@ export const serviceActions = createActions(
4950
name: serviceOptions.name,
5051
version: serviceOptions.version,
5152
actions: serviceOptions.actions ?? {},
52-
})
53+
created(this: BlobType) {
54+
//Overwrite logger with service logger if there is
55+
if(this.logger) {
56+
this.logger = createServiceLogger(serviceOptions.name)
57+
}
58+
}
59+
})
5360

5461
await $service.waitForServices([
5562
{

0 commit comments

Comments
 (0)