Skip to content

Commit

Permalink
Merge pull request #12561 from micalevisk/fix/logger-print-classes
Browse files Browse the repository at this point in the history
fix(common): when passing class functions to logger methods
  • Loading branch information
kamilmysliwiec authored Oct 23, 2023
2 parents ecdd86f + a26de50 commit 55c6a9a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 26 deletions.
16 changes: 12 additions & 4 deletions packages/common/services/console-logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,18 @@ export class ConsoleLogger implements LoggerService {
}

protected stringifyMessage(message: unknown, logLevel: LogLevel) {
// If the message is a function, call it and re-resolve its value.
return isFunction(message)
? this.stringifyMessage(message(), logLevel)
: isPlainObject(message) || Array.isArray(message)
if (isFunction(message)) {
const messageAsStr = Function.prototype.toString.call(message);
const isClass = messageAsStr.startsWith('class ');
if (isClass) {
// If the message is a class, we will display the class name.
return this.stringifyMessage(message.name, logLevel);
}
// If the message is a non-class function, call it and re-resolve its value.
return this.stringifyMessage(message(), logLevel);
}

return isPlainObject(message) || Array.isArray(message)
? `${this.colorize('Object:', logLevel)}\n${JSON.stringify(
message,
(key, value) =>
Expand Down
28 changes: 28 additions & 0 deletions packages/common/test/services/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,34 @@ describe('Logger', () => {
);
});
});

describe('classes for message', () => {
let processStdoutWriteSpy: sinon.SinonSpy;

beforeEach(() => {
processStdoutWriteSpy = sinon.spy(process.stdout, 'write');
});
afterEach(() => {
processStdoutWriteSpy.restore();
});

it("should display class's name or empty for anonymous classes", () => {
const logger = new ConsoleLogger();

// in-line anonymous class
logger.log(class {});

// named class
class Test {
#privateField = 'private field';
publicField = 'public field';
}
logger.log(Test);

expect(processStdoutWriteSpy.firstCall.firstArg).to.include('');
expect(processStdoutWriteSpy.secondCall.firstArg).to.include(Test.name);
});
});
});

describe('[instance methods]', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { CONTROLLER_ID_KEY } from './constants';
import { NestContainer } from './container';
import { ContextId, InstanceWrapper } from './instance-wrapper';
import { ModuleRef, ModuleRefGetOrResolveOpts } from './module-ref';
import { Injector } from './injector';

export class Module {
private readonly _id: string;
Expand Down
39 changes: 19 additions & 20 deletions packages/core/middleware/routes-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,27 @@ export class RoutesMapper {
version === VERSION_NEUTRAL ? undefined : version;

const toRouteInfo = (item: RouteDefinition, prefix: string) =>
item.path
?.flatMap(p => {
let endpointPath = modulePath ?? '';
endpointPath += this.normalizeGlobalPath(prefix) + addLeadingSlash(p);

const routeInfo: RouteInfo = {
path: endpointPath,
method: item.requestMethod,
};
const version = item.version ?? controllerVersion;
if (version && versioningConfig) {
if (typeof version !== 'string' && Array.isArray(version)) {
return version.map(v => ({
...routeInfo,
version: toUndefinedIfNeural(v),
}));
}
routeInfo.version = toUndefinedIfNeural(version);
item.path?.flatMap(p => {
let endpointPath = modulePath ?? '';
endpointPath += this.normalizeGlobalPath(prefix) + addLeadingSlash(p);

const routeInfo: RouteInfo = {
path: endpointPath,
method: item.requestMethod,
};
const version = item.version ?? controllerVersion;
if (version && versioningConfig) {
if (typeof version !== 'string' && Array.isArray(version)) {
return version.map(v => ({
...routeInfo,
version: toUndefinedIfNeural(v),
}));
}
routeInfo.version = toUndefinedIfNeural(version);
}

return routeInfo;
});
return routeInfo;
});

return []
.concat(routePath)
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/server/server-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class ServerMqtt extends Server implements CustomTransportStrategy {
if (this.messageHandlers.has(route)) {
return this.messageHandlers.get(route) || null;
}

for (const [key, value] of this.messageHandlers) {
const keyWithoutSharedPrefix = this.removeHandlerKeySharedPrefix(key);
if (this.matchMqttPattern(keyWithoutSharedPrefix, route)) {
Expand Down

0 comments on commit 55c6a9a

Please sign in to comment.