Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Add Log Prefix for logs in an instance #275

Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 2 deletions nodecg-io-core/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { InstanceManager } from "./instanceManager";
import { Service } from "./service";
import { PersistenceManager } from "./persistenceManager";
import { ServiceProvider } from "./serviceProvider";

import { Logger } from "./utils/logger";
/**
* Main type of NodeCG extension that the core bundle exposes.
* Contains references to all internal modules.
Expand Down Expand Up @@ -88,7 +88,7 @@ function onExit(
if (!service.failed && client) {
nodecg.log.info(`Stopping service ${key} of type ${service.result.serviceType}.`);
try {
service.result.stopClient(client);
service.result.stopClient(client, new Logger("nodecg-io-core-shutdown", nodecg));
} catch (err) {
nodecg.log.info(
`Could not stop service ${key} of type ${service.result.serviceType}: ${String(err)}`,
Expand Down
15 changes: 9 additions & 6 deletions nodecg-io-core/extension/instanceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ServiceManager } from "./serviceManager";
import { BundleManager } from "./bundleManager";
import Ajv from "ajv";
import { EventEmitter } from "events";

import { Logger } from "./utils/logger";
/**
* Manages instances of services and their configs/clients.
*/
Expand Down Expand Up @@ -105,7 +105,7 @@ export class InstanceManager extends EventEmitter {
} else {
this.nodecg.log.info(`Successfully stopped client of service instance "${instanceName}".`);
try {
svc.result.stopClient(instance.client);
svc.result.stopClient(instance.client, new Logger(instanceName, this.nodecg));
} catch (e) {
this.nodecg.log.error(`Couldn't stop service instance: ${e}`);
}
Expand Down Expand Up @@ -172,7 +172,10 @@ export class InstanceManager extends EventEmitter {

// Validation by the service.
try {
const validationRes = await service.result.validateConfig(config);
const validationRes = await service.result.validateConfig(
config,
new Logger(instanceName, this.nodecg),
);
if (validationRes.failed) {
throw validationRes.errorMessage;
}
Expand Down Expand Up @@ -220,7 +223,7 @@ export class InstanceManager extends EventEmitter {
// If the service does not require a config we can safely ignore the undefined error because in that case
// passing undefined is the intended behavior.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const client = await service.createClient(inst.config!);
const client = await service.createClient(inst.config!, new Logger(instanceName, this.nodecg));

// Check if a error happened while creating the client
if (client.failed) {
Expand All @@ -230,7 +233,7 @@ export class InstanceManager extends EventEmitter {
inst.client = client.result;
}
} catch (err) {
const msg = `The "${inst.serviceType}" service produced an error while creating a client: ${err}`;
const msg = `The "${inst.serviceType}" service with the name "${instanceName}" produced an error while creating a client: ${err}`;
this.nodecg.log.error(msg);
inst.client = undefined;
return error(msg);
Expand All @@ -244,7 +247,7 @@ export class InstanceManager extends EventEmitter {
if (oldClient !== undefined) {
this.nodecg.log.info(`Stopping old unused ${inst.serviceType} client...`);
try {
service.stopClient(oldClient);
service.stopClient(oldClient, new Logger(instanceName, this.nodecg));
} catch (e) {
this.nodecg.log.error(`Couldn't stop service instance: ${e}`);
}
Expand Down
10 changes: 7 additions & 3 deletions nodecg-io-core/extension/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Result } from "./utils/result";
import { ServiceProvider } from "./serviceProvider";
import { Logger } from "./utils/logger";

/**
* Models a map using a object, instead of a iterator like the javascript es6 map.
Expand Down Expand Up @@ -48,26 +49,29 @@ export interface Service<R, C> {
* This function validates the passed config after it has been validated against the json schema (if applicable).
* Should make deeper checks like checking validity of auth tokens.
* @param config the config which should be validated.
* @param logger the logger which logs with the instance.name as prefix
* @return void if the config passes validation and an error string describing the issue if not.
*/
validateConfig(config: R): Promise<Result<void>>;
validateConfig(config: R, logger: Logger): Promise<Result<void>>;

/**
* Creates a client to the service using the validated config.
* The returned result will be passed to bundles and they should be able to use the service with this returned client.
*
* @param config the user provided config for the service.
* @param logger the logger which logs with the instance.name as prefix
* @return the client if everything went well and an error string describing the issue if a error occured.
*/
createClient(config: R): Promise<Result<C>>;
createClient(config: R, logger: Logger): Promise<Result<C>>;

/**
* Stops a client of this service that is not needed anymore.
* Services should close any connections that might exist here.
*
* @param client the client that needs to be stopped.
* @param logger the logger which logs with the instance.name as prefix
*/
stopClient(client: C): void;
stopClient(client: C, logger: Logger): void;

/**
* Removes all handlers from a service client.
Expand Down
11 changes: 7 additions & 4 deletions nodecg-io-core/extension/serviceBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Result } from "./utils/result";

import * as fs from "fs";
import * as path from "path";
import { Logger } from "./utils/logger";

/**
* Class helping to create a nodecg-io service
Expand Down Expand Up @@ -42,7 +43,6 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
this.nodecg = nodecg;
this.serviceType = serviceName;
this.schema = this.readSchema(pathSegments);

this.nodecg.log.info(this.serviceType + " bundle started.");
this.core = this.nodecg.extensions["nodecg-io-core"] as unknown as NodeCGIOCore | undefined;
if (this.core === undefined) {
Expand All @@ -64,26 +64,29 @@ export abstract class ServiceBundle<R, C> implements Service<R, C> {
* This function validates the passed config after it has been validated against the json schema (if applicable).
* Should make deeper checks like checking validity of auth tokens.
* @param config the config which should be validated.
* @param logger the logger which logs with the instance.name as prefix
* @return void if the config passes validation and an error string describing the issue if not.
*/
abstract validateConfig(config: R): Promise<Result<void>>;
abstract validateConfig(config: R, logger: Logger): Promise<Result<void>>;

/**
* Creates a client to the service using the validated config.
* The returned result will be passed to bundles and they should be able to use the service with this returned client.
*
* @param config the user provided config for the service.
* @param logger the logger which logs with the instance.name as prefix
* @return the client if everything went well and an error string describing the issue if a error occured.
*/
abstract createClient(config: R): Promise<Result<C>>;
abstract createClient(config: R, logger: Logger): Promise<Result<C>>;

/**
* Stops a client of this service that is not needed anymore.
* Services should close any connections that might exist here.
*
* @param client the client that needs to be stopped.
* @param logger the logger which logs with the instance.name as prefix
*/
abstract stopClient(client: C): void;
abstract stopClient(client: C, logger: Logger): void;

/**
* Removes all handlers from a service client.
Expand Down
25 changes: 25 additions & 0 deletions nodecg-io-core/extension/utils/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { NodeCG } from "nodecg-types/types/server";

export class Logger {
constructor(private name: string, private nodecg: NodeCG) {}
trace(...args: any[]): void {
this.nodecg.log.trace(`[${this.name}] ${args[0]}`, ...args.slice(1));
}

debug(...args: any[]): void {
this.nodecg.log.debug(`[${this.name}] ${args[0]}`, ...args.slice(1));
}

info(...args: any[]): void {
this.nodecg.log.info(`[${this.name}] ${args[0]}`, ...args.slice(1));
}

warn(...args: any[]): void {
this.nodecg.log.warn(`[${this.name}] ${args[0]}`, ...args.slice(1));
}

error(...args: any[]): void {
this.nodecg.log.error(`[${this.name}] ${args[0]}`, ...args.slice(1));
}
}
1 change: 1 addition & 0 deletions nodecg-io-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export type { ObjectMap, Service, ServiceDependency, ServiceInstance } from "./e
export * from "./extension/utils/result";
export * from "./extension/serviceBundle";
export * from "./extension/serviceProvider";
export * from "./extension/utils/logger";
Loading