Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove erroneous prefixes when using loggerInjectable and prefixLoggerInjectable #7745

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/

export type { LogFunction } from "./src/logger.injectable";
export type { LogFunction, Logger } from "./src/logger.injectable";
export {
logDebugInjectionToken,
logErrorInjectionToken,
Expand All @@ -12,7 +12,6 @@ export {
logWarningInjectionToken,
} from "./src/logger.injectable";

export type { Logger } from "./src/logger";
/** @deprecated Use specific injectionToken, eg. logErrorInjectionToken */
export { loggerInjectionToken } from "./src/logger.injectable";
/** @deprecated Use specific injectionToken, eg. logErrorInjectionToken */
Expand Down
48 changes: 27 additions & 21 deletions packages/logger/src/logger.injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ import {
getInjectionToken,
lifecycleEnum,
} from "@ogre-tools/injectable";
import type { Logger } from "./logger";
import { winstonLoggerInjectable } from "./winston-logger.injectable";
import { pipeline } from "@ogre-tools/fp";

export interface Logger {
info: LogFunction;
error: LogFunction;
debug: LogFunction;
warn: LogFunction;
silly: LogFunction;
}

/** @deprecated Use specific injectionToken, eg. logErrorInjectionToken */
export const loggerInjectionToken = getInjectionToken<Logger>({
id: "logger-injection-token",
Expand All @@ -21,18 +28,18 @@ export const loggerInjectionToken = getInjectionToken<Logger>({
export const loggerInjectable = getInjectable({
id: "logger",
instantiate: (di): Logger => ({
debug: di.inject(logDebugInjectionToken),
info: di.inject(logInfoInjectionToken),
warn: di.inject(logWarningInjectionToken),
error: di.inject(logErrorInjectionToken),
silly: di.inject(logSillyInjectionToken),
debug: getLogFunctionFor("debug", undefined)(di),
info: getLogFunctionFor("info", undefined)(di),
warn: getLogFunctionFor("warn", undefined)(di),
error: getLogFunctionFor("error", undefined)(di),
silly: getLogFunctionFor("silly", undefined)(di),
}),

decorable: false,
injectionToken: loggerInjectionToken,
});

export type LogFunction = (message: string, ...data: any[]) => void;
export type LogFunction = (message: string, ...data: unknown[]) => void;

export const logDebugInjectionToken = getInjectionToken<LogFunction>({
id: "log-debug-injection-token",
Expand All @@ -56,24 +63,23 @@ export const logSillyInjectionToken = getInjectionToken<LogFunction>({

const screamingKebabCase = (str: string) => pipeline(str, kebabCase, toUpper);

const getLogFunctionFor =
(scenario: keyof Logger) =>
(di: DiContainerForInjection): LogFunction => {
const getLogFunctionFor = (scenario: keyof Logger, namespace: string | undefined) => {
const prefix = namespace
? `[${screamingKebabCase(namespace.replace(/-feature$/, ""))}]: `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we assume the id or namespace is lowercase?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If somebody called their feature "my-feature"?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I did assume that given that all of them are. I can make a follow-up PR to fix this

: "";

return (di: DiContainerForInjection): LogFunction => {
const winstonLogger = di.inject(winstonLoggerInjectable);

return (message, ...data) => {
winstonLogger[scenario](
di.sourceNamespace
? `[${screamingKebabCase(di.sourceNamespace)}]: ${message}`
: message,
...data
);
winstonLogger[scenario](`${prefix}${message}`, ...data);
};
};
};

export const logDebugInjectable = getInjectable({
id: "log-debug",
instantiate: getLogFunctionFor("debug"),
instantiate: (di) => getLogFunctionFor("debug", di.sourceNamespace)(di),
injectionToken: logDebugInjectionToken,
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di) => di.sourceNamespace,
Expand All @@ -82,7 +88,7 @@ export const logDebugInjectable = getInjectable({

export const logInfoInjectable = getInjectable({
id: "log-info",
instantiate: getLogFunctionFor("info"),
instantiate: (di) => getLogFunctionFor("info", di.sourceNamespace)(di),
injectionToken: logInfoInjectionToken,
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di) => di.sourceNamespace,
Expand All @@ -91,7 +97,7 @@ export const logInfoInjectable = getInjectable({

export const logWarningInjectable = getInjectable({
id: "log-warning",
instantiate: getLogFunctionFor("warn"),
instantiate: (di) => getLogFunctionFor("warn", di.sourceNamespace)(di),
injectionToken: logWarningInjectionToken,
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di) => di.sourceNamespace,
Expand All @@ -100,7 +106,7 @@ export const logWarningInjectable = getInjectable({

export const logErrorInjectable = getInjectable({
id: "log-error",
instantiate: getLogFunctionFor("error"),
instantiate: (di) => getLogFunctionFor("error", di.sourceNamespace)(di),
injectionToken: logErrorInjectionToken,
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di) => di.sourceNamespace,
Expand All @@ -109,7 +115,7 @@ export const logErrorInjectable = getInjectable({

export const logSillyInjectable = getInjectable({
id: "log-silly",
instantiate: getLogFunctionFor("silly"),
instantiate: (di) => getLogFunctionFor("silly", di.sourceNamespace)(di),
injectionToken: logSillyInjectionToken,
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di) => di.sourceNamespace,
Expand Down
75 changes: 68 additions & 7 deletions packages/logger/src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ import { createContainer, getInjectable } from "@ogre-tools/injectable";
import { registerFeature } from "@k8slens/feature-core";
import { loggerFeature } from "./feature";
import { winstonLoggerInjectable } from "./winston-logger.injectable";
import TransportStream from "winston-transport";

import {
logDebugInjectionToken,
logErrorInjectionToken,
loggerInjectable,
logInfoInjectionToken,
logSillyInjectionToken,
logWarningInjectionToken,
} from "./logger.injectable";

import { getFeature } from "@k8slens/feature-core/src/feature";
import { loggerTransportInjectionToken } from "./transports";
import { prefixedLoggerInjectable } from "./prefixed-logger.injectable";

describe("logger", () => {
[
{ scenario: "debug", injectionToken: logDebugInjectionToken },
{ scenario: "info", injectionToken: logInfoInjectionToken },
{ scenario: "warn", injectionToken: logWarningInjectionToken },
{ scenario: "error", injectionToken: logErrorInjectionToken },
{ scenario: "silly", injectionToken: logSillyInjectionToken },
{ scenario: "debug" as const, injectionToken: logDebugInjectionToken },
{ scenario: "info" as const, injectionToken: logInfoInjectionToken },
{ scenario: "warn" as const, injectionToken: logWarningInjectionToken },
{ scenario: "error" as const, injectionToken: logErrorInjectionToken },
{ scenario: "silly" as const, injectionToken: logSillyInjectionToken },
].forEach(({ scenario, injectionToken }) => {
it(`given not inside a Feature, when logging "${scenario}", does so without a prefix`, () => {
const di = createContainer("irrelevant");
Expand All @@ -40,7 +44,45 @@ describe("logger", () => {
);
});

it(`given inside a Feature, when logging "${scenario}", does so with Feature's id as prefix`, () => {
it(`given not inside a Feature, when logging "${scenario}" using legacy logger, does so without a prefix`, () => {
const di = createContainer("irrelevant");

registerFeature(di, loggerFeature);

const winstonLoggerStub = { [scenario]: jest.fn() } as any;

di.override(winstonLoggerInjectable, () => winstonLoggerStub);

const logger = di.inject(loggerInjectable);

logger[scenario]("some-message", "some-data");

expect(winstonLoggerStub[scenario]).toHaveBeenCalledWith(
"some-message",
"some-data"
);
});

it(`given not inside a Feature, when logging "${scenario}" using legacy prefixed logger, does so without a feature prefix`, () => {
const di = createContainer("irrelevant");

registerFeature(di, loggerFeature);

const winstonLoggerStub = { [scenario]: jest.fn() } as any;

di.override(winstonLoggerInjectable, () => winstonLoggerStub);

const logger = di.inject(prefixedLoggerInjectable, "A-PREFIX");

logger[scenario]("some-message", "some-data");

expect(winstonLoggerStub[scenario]).toHaveBeenCalledWith(
"[A-PREFIX]: some-message",
"some-data"
);
});

it(`given inside a Feature, when logging "${scenario}", does so with Feature's id as prefix without trailing '-feature' to avoid redundancy`, () => {
const di = createContainer("irrelevant");

const logScenarioInFeature = getInjectable({
Expand Down Expand Up @@ -70,9 +112,28 @@ describe("logger", () => {
logScenario("some-message", "some-data");

expect(winstonLoggerStub[scenario]).toHaveBeenCalledWith(
"[SOME-FEATURE]: some-message",
"[SOME]: some-message",
"some-data"
);
});
});

it("given a single transport, when logging, does not throw, and does call the transport", () => {
const di = createContainer("irrelevant");
const log = jest.fn().mockImplementation((data, next) => next());

registerFeature(di, loggerFeature);

di.register(getInjectable({
id: "some-transport",
instantiate: () => new TransportStream({ log }),
injectionToken: loggerTransportInjectionToken,
}))

const logger = di.inject(loggerInjectable);

logger.info("some-message", "some-data");

expect(log).toHaveBeenCalled();
});
});
12 changes: 0 additions & 12 deletions packages/logger/src/logger.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/logger/src/prefixed-logger.injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import type { Logger } from "./logger";
import { loggerInjectionToken } from "./logger.injectable";
import { Logger, loggerInjectionToken } from "./logger.injectable";

/** @deprecated Use specific injectionToken, eg. logErrorInjectionToken */
export const prefixedLoggerInjectable = getInjectable({
Expand Down
8 changes: 3 additions & 5 deletions packages/logger/src/transports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import { getInjectionToken } from "@ogre-tools/injectable";
import type TransportStream from "winston-transport";

export const loggerTransportInjectionToken = getInjectionToken<TransportStream>(
{
id: "logger-transport",
}
);
export const loggerTransportInjectionToken = getInjectionToken<TransportStream>({
id: "logger-transport",
});