Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

1396 add pino #1397

Open
wants to merge 16 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions modules/messaging/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export class MessagingAuthService {
}

this.log = this.config.logger || nullLogger;
this.log.debug(
`Created messaging auth service with config: ${JSON.stringify(config, null, 2)}`,
);
this.log.debug(`Created messaging auth service`, { config });

this.defaultJWTAudience = this.config.messagingUrl as string;
this.auth = new AuthService(
Expand Down
27 changes: 18 additions & 9 deletions modules/messaging/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MessagingService implements IMessagingService {
private readonly getBearerToken: () => Promise<string>,
) {
this.log = config.logger || nullLogger;
this.log.debug(`Created NatsMessagingService with config: ${JSON.stringify(config, null, 2)}`);
this.log.debug(`Created NatsMessagingService`, { config });
this.bearerToken = null;
}

Expand Down Expand Up @@ -60,44 +60,53 @@ export class MessagingService implements IMessagingService {
async onReceive(subject: string, callback: (msg: GenericMessage) => void): Promise<void> {
await this.service!.subscribe(this.prependKey(`${subject}.>`), (msg: any, err?: any): void => {
if (err || !msg || !msg.data) {
this.log.error(`Encountered an error while handling callback for message ${msg}: ${err}`);
this.log.error(`Caught error receiving message`, {
msg,
err: err.message,
stack: err.stack,
});
} else {
const data = typeof msg.data === `string` ? JSON.parse(msg.data) : msg.data;
this.log.debug(`Received message for ${subject}: ${JSON.stringify(data)}`);
this.log.debug(`Received message`, { subject, data });
callback(data as GenericMessage);
}
});
}

async send(to: string, msg: GenericMessage): Promise<void> {
this.log.debug(`Sending message to ${to}: ${JSON.stringify(msg)}`);
this.log.debug(`Sending message`, { to, msg });
return this.service!.publish(this.prependKey(`${to}.${msg.from}`), JSON.stringify(msg));
}

////////////////////////////////////////
// More generic methods

async publish(subject: string, data: any): Promise<void> {
this.log.debug(`Publishing ${subject}: ${JSON.stringify(data)}`);
this.log.debug(`Publishing`, { subject, data });
this.service!.publish(subject, JSON.stringify(data));
}

async request(subject: string, timeout: number, data: object = {}): Promise<any> {
this.log.debug(`Requesting ${subject} with data: ${JSON.stringify(data)}`);
this.log.debug(`Requesting response`, { subject, data });
const response = await this.service!.request(subject, timeout, JSON.stringify(data));
this.log.debug(`Request for ${subject} returned: ${JSON.stringify(response)}`);
this.log.debug(`Request received`, { subject, response });
return response;
}

async subscribe(subject: string, callback: (msg: GenericMessage) => void): Promise<void> {
await this.service!.subscribe(subject, (msg: any, err?: any): void => {
if (err || !msg || !msg.data) {
this.log.error(`Encountered an error while handling callback for message ${msg}: ${err}`);
this.log.error(`Failed to handle subscription`, {
msg,
subject,
error: err?.message,
stack: err?.stack,
});
} else {
const parsedMsg = typeof msg === `string` ? JSON.parse(msg) : msg;
const parsedData = typeof msg.data === `string` ? JSON.parse(msg.data) : msg.data;
parsedMsg.data = parsedData;
this.log.debug(`Subscription for ${subject}: ${JSON.stringify(parsedMsg)}`);
this.log.debug(`Received subscription`, { subject, data: parsedMsg });
callback(parsedMsg as GenericMessage);
}
});
Expand Down
1 change: 1 addition & 0 deletions modules/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"evt": "1.8.4",
"ioredis": "4.17.3",
"lodash": "4.17.19",
"nestjs-pino": "1.2.0",
"nats": "1.4.9",
"p-queue": "6.6.0",
"pg": "8.3.0",
Expand Down
81 changes: 15 additions & 66 deletions modules/node/src/logger/logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,42 @@
import { ILogger } from "@connext/types";
import { Injectable, Scope, Logger as NestLogger } from "@nestjs/common";

// TODO: can we import & use ColorfulLogger from @connext/utils here?

const colors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
};
import { PinoLogger } from "@connext/utils";

@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService extends NestLogger implements ILogger {
private levels: { [key: string]: number } = {
debug: 4,
error: 1,
info: 3,
warn: 2,
};
private defaultLevel = "info";
private color: { [key: string]: string } = {
debug: colors.FgMagenta,
error: colors.FgRed,
info: colors.FgGreen,
warn: colors.FgYellow,
};

public context: string;
export class LoggerService extends NestLogger {
private internal: PinoLogger;
public logLevel: number = parseInt(process.env.INDRA_LOG_LEVEL || "3", 10);

public constructor(context?: string) {
super();
this.context = typeof context !== "undefined" ? context : "UnknownContext";
this.internal = new PinoLogger(context, this.logLevel);
}

public setContext(context: string): void {
this.context = context;
this.internal.setContext(context);
}

public newContext(context: string): LoggerService {
return new LoggerService(context);
}

public error(msg: string, stack?: string): void {
this.print("error", msg);
stack && this.print("error", stack);
public error(details: object, message: string): void {
this.internal.error(message, details);
}

public warn(msg: string): void {
this.print("warn", msg);
public warn(details: object, message: string): void {
this.internal.warn(message, details);
}

// Nest internals expect a method called log
public log(msg: string): void {
this.print(this.defaultLevel, msg);
}

public info(msg: string): void {
this.print("info", msg);
public log(details: object, message: string): void {
this.internal.info(message, details);
}

public debug(msg: string): void {
this.print("debug", msg);
public info(details: object, message: string): void {
this.internal.info(message, details);
}

private print(level: string, msg: any): void {
if (this.levels[level] > this.logLevel) return;
const now = new Date().toISOString();
return (console as any)[level](
`${colors.Reset}${now} ${colors.FgCyan}[${this.context}]${colors.Reset} ${this.color[level]}${msg}${colors.Reset}`,
);
public debug(details: object, message: string): void {
this.internal.debug(message, details);
}
}
4 changes: 2 additions & 2 deletions modules/node/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { LoggerService } from "./logger/logger.service";

(async () => {
const log = new LoggerService("Main");
log.error(`Deploying Indra ${version}`);
log.error({ version }, `Deploying Indra`);
process.on("unhandledRejection", (e: Error) => {
log.error(`Unhandled Promise Rejection: ${e.stack}`);
log.error({ error: e.message, stack: e.stack }, `Unhandled Promise Rejection`);
});
const app = await NestFactory.create(AppModule, { logger: log });
app.enableCors();
Expand Down
Loading