Skip to content

Commit

Permalink
Added LogLevel type for better auto completion
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Sep 9, 2023
1 parent daa1b06 commit dab8e4f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 47 deletions.
12 changes: 6 additions & 6 deletions packages/core/src/ExceptionlessClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Configuration } from "./configuration/Configuration.js";
import { SettingsManager } from "./configuration/SettingsManager.js";
import { EventBuilder } from "./EventBuilder.js";
import { Event, KnownEventDataKeys } from "./models/Event.js";
import { Event, KnownEventDataKeys, LogLevel } from "./models/Event.js";
import { UserDescription } from "./models/data/UserDescription.js";
import { EventContext } from "./models/EventContext.js";
import { EventPluginContext } from "./plugins/EventPluginContext.js";
Expand Down Expand Up @@ -133,8 +133,8 @@ export class ExceptionlessClient {

public createLog(message: string): EventBuilder;
public createLog(source: string, message: string): EventBuilder;
public createLog(source: string | undefined, message: string, level: string): EventBuilder;
public createLog(sourceOrMessage: string, message?: string, level?: string): EventBuilder {
public createLog(source: string | undefined, message: string, level: LogLevel): EventBuilder;
public createLog(sourceOrMessage: string, message?: string, level?: LogLevel): EventBuilder {
let builder = this.createEvent().setType("log");

if (level) {
Expand All @@ -161,9 +161,9 @@ export class ExceptionlessClient {

public submitLog(message: string): Promise<EventPluginContext>;
public submitLog(source: string, message: string): Promise<EventPluginContext>;
public submitLog(source: string | undefined, message: string, level: string): Promise<EventPluginContext>;
public submitLog(sourceOrMessage: string, message?: string, level?: string): Promise<EventPluginContext> {
return this.createLog(sourceOrMessage, <string>message, <string>level).submit();
public submitLog(source: string | undefined, message: string, level: LogLevel): Promise<EventPluginContext>;
public submitLog(sourceOrMessage: string, message?: string, level?: LogLevel): Promise<EventPluginContext> {
return this.createLog(sourceOrMessage, <string>message, <LogLevel>level).submit();
}

public createNotFound(resource: string): EventBuilder {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type { ILog } from "./logging/ILog.js";
export { ConsoleLog } from "./logging/ConsoleLog.js";
export { NullLog } from "./logging/NullLog.js";

export type { Event, EventType, IEventData } from "./models/Event.js";
export type { Event, EventType, IEventData, LogLevel } from "./models/Event.js";
export { KnownEventDataKeys } from "./models/Event.js";
export type { EnvironmentInfo } from "./models/data/EnvironmentInfo.js";
export type { ManualStackingInfo } from "./models/data/ManualStackingInfo.js";
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/models/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export enum KnownEventDataKeys {
ManualStackingInfo = "@stack",
}

export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal" | string;

export interface IEventData extends Record<string, unknown> {
"@error"?: ErrorInfo;
"@simple_error"?: SimpleError;
Expand All @@ -52,7 +54,7 @@ export interface IEventData extends Record<string, unknown> {
"@user"?: UserInfo;
"@user_description"?: UserDescription;
"@version"?: string;
"@level"?: string;
"@level"?: LogLevel;
"@submission_method"?: string;
"@stack"?: ManualStackingInfo;
}
4 changes: 2 additions & 2 deletions packages/core/src/plugins/default/EventExclusionPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KnownEventDataKeys } from "../../models/Event.js";
import { KnownEventDataKeys, LogLevel } from "../../models/Event.js";
import { isMatch, startsWith, toBoolean } from "../../Utils.js";
import { EventPluginContext } from "../EventPluginContext.js";
import { IEventPlugin } from "../IEventPlugin.js";
Expand Down Expand Up @@ -38,7 +38,7 @@ export class EventExclusionPlugin implements IEventPlugin {
return Promise.resolve();
}

public getLogLevel(level: string | undefined): number {
public getLogLevel(level: LogLevel | undefined): number {
switch ((level || "").toLowerCase().trim()) {
case "trace":
case "true":
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/ExceptionlessClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ describe("ExceptionlessClient", () => {
const client = new ExceptionlessClient();
client.config.apiKey = "UNIT_TEST_API_KEY";

const builder = client.createLog(undefined, "Unit Test message", "Trace");
const builder = client.createLog(undefined, "Unit Test message", "trace");

expect(builder.target.source).toBeUndefined();
expect(builder.target.message).toBe("Unit Test message");
expect(builder.target.data?.[KnownEventDataKeys.Level]).toBe("Trace");
expect(builder.target.data?.[KnownEventDataKeys.Level]).toBe("trace");
});

test("should accept source and message", () => {
Expand All @@ -63,11 +63,11 @@ describe("ExceptionlessClient", () => {
test("should accept source and message and level", () => {
const client = new ExceptionlessClient();
client.config.apiKey = "UNIT_TEST_API_KEY";
const builder = client.createLog("source", "Unit Test message", "Info");
const builder = client.createLog("source", "Unit Test message", "info");

expect(builder.target.source).toBe("source");
expect(builder.target.message).toBe("Unit Test message");
expect(builder.target.data?.[KnownEventDataKeys.Level]).toBe("Info");
expect(builder.target.data?.[KnownEventDataKeys.Level]).toBe("info");
});

test("should allow construction via a configuration object", () => {
Expand Down
66 changes: 33 additions & 33 deletions packages/core/test/plugins/default/EventExclusionPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { describe, test } from "@jest/globals";
import { expect } from "expect";

import { ExceptionlessClient } from "../../../src/ExceptionlessClient.js";
import { Event, EventType, KnownEventDataKeys } from "../../../src/models/Event.js";
import { Event, EventType, KnownEventDataKeys, LogLevel } from "../../../src/models/Event.js";
import { InnerErrorInfo } from "../../../src/models/data/ErrorInfo.js";
import { EventExclusionPlugin } from "../../../src/plugins/default/EventExclusionPlugin.js";
import { EventPluginContext } from "../../../src/plugins/EventPluginContext.js";
import { EventContext } from "../../../src/models/EventContext.js";

describe("EventExclusionPlugin", () => {
describe("should exclude log levels", () => {
const run = async (source: string | undefined, level: string | null | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const run = async (source: string | undefined, level: LogLevel | null | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const client = new ExceptionlessClient();
if (typeof settingKey == "string") {
client.config.settings[settingKey] = settingValue as string;
Expand All @@ -30,31 +30,31 @@ describe("EventExclusionPlugin", () => {

test("<null>", async () => expect(await run(undefined, null, null, null)).toBe(false));
test("Test", async () => expect(await run("Test", null, null, null)).toBe(false));
test("[Trace] Test", async () => expect(await run("Test", "Trace", null, null)).toBe(false));
test("[trace] Test", async () => expect(await run("Test", "trace", null, null)).toBe(false));
test("[Off] Test", async () => expect(await run("Test", "Off", null, null)).toBe(true));
test("[Abc] Test", async () => expect(await run("Test", "Abc", null, null)).toBe(false));
test("[Trace] <null> (source min level: Off", async () => expect(await run(undefined, "Trace", "@@log:", "Off")).toBe(true));
test("[Trace] <null> (global min level: Off", async () => expect(await run(undefined, "Trace", "@@log:*", "Off")).toBe(true));
test("[Trace] <undefined> (source min level: Off", async () => expect(await run(undefined, "Trace", "@@log:", "Off")).toBe(true));
test("[Trace] <undefined> (global min level: Off", async () => expect(await run(undefined, "Trace", "@@log:*", "Off")).toBe(true));
test("[Trace] <empty> (source min level: Off", async () => expect(await run("", "Trace", "@@log:", "Off")).toBe(true)); // Becomes Global Log Level
test("[Trace] <empty> (global min level: Off", async () => expect(await run("", "Trace", "@@log:*", "Off")).toBe(true));
test("[Trace] Test (source min level: false)", async () => expect(await run("Test", "Trace", "@@log:Test", "false")).toBe(true));
test("[Trace] Test (source min level: no)", async () => expect(await run("Test", "Trace", "@@log:Test", "no")).toBe(true));
test("[Trace] Test (source min level: 0)", async () => expect(await run("Test", "Trace", "@@log:Test", "0")).toBe(true));
test("[Trace] Test (source min level: true)", async () => expect(await run("Test", "Trace", "@@log:Test", "true")).toBe(false));
test("[Trace] Test (source min level: yes)", async () => expect(await run("Test", "Trace", "@@log:Test", "yes")).toBe(false));
test("[Trace] Test (source min level: 1)", async () => expect(await run("Test", "Trace", "@@log:Test", "1")).toBe(false));
test("[Trace] Test (source min level: Debug)", async () => expect(await run("Test", "Trace", "@@log:Test", "Debug")).toBe(true));
test("[Info] Test (source min level: Debug)", async () => expect(await run("Test", "Info", "@@log:Test", "Debug")).toBe(false));
test("[Trace] Test (global min level: Debug)", async () => expect(await run("Test", "Trace", "@@log:*", "Debug")).toBe(true));
test("[Warn] Test (global min level: Debug)", async () => expect(await run("Test", "Warn", "@@log:*", "Debug")).toBe(false));
test("[trace] <null> (source min level: Off", async () => expect(await run(undefined, "trace", "@@log:", "Off")).toBe(true));
test("[trace] <null> (global min level: Off", async () => expect(await run(undefined, "trace", "@@log:*", "Off")).toBe(true));
test("[trace] <undefined> (source min level: Off", async () => expect(await run(undefined, "trace", "@@log:", "Off")).toBe(true));
test("[trace] <undefined> (global min level: Off", async () => expect(await run(undefined, "trace", "@@log:*", "Off")).toBe(true));
test("[trace] <empty> (source min level: Off", async () => expect(await run("", "trace", "@@log:", "Off")).toBe(true)); // Becomes Global Log Level
test("[trace] <empty> (global min level: Off", async () => expect(await run("", "trace", "@@log:*", "Off")).toBe(true));
test("[trace] Test (source min level: false)", async () => expect(await run("Test", "trace", "@@log:Test", "false")).toBe(true));
test("[trace] Test (source min level: no)", async () => expect(await run("Test", "trace", "@@log:Test", "no")).toBe(true));
test("[trace] Test (source min level: 0)", async () => expect(await run("Test", "trace", "@@log:Test", "0")).toBe(true));
test("[trace] Test (source min level: true)", async () => expect(await run("Test", "trace", "@@log:Test", "true")).toBe(false));
test("[trace] Test (source min level: yes)", async () => expect(await run("Test", "trace", "@@log:Test", "yes")).toBe(false));
test("[trace] Test (source min level: 1)", async () => expect(await run("Test", "trace", "@@log:Test", "1")).toBe(false));
test("[trace] Test (source min level: debug)", async () => expect(await run("Test", "trace", "@@log:Test", "debug")).toBe(true));
test("[info] Test (source min level: debug)", async () => expect(await run("Test", "info", "@@log:Test", "debug")).toBe(false));
test("[trace] Test (global min level: debug)", async () => expect(await run("Test", "trace", "@@log:*", "debug")).toBe(true));
test("[warn] Test (global min level: debug)", async () => expect(await run("Test", "warn", "@@log:*", "debug")).toBe(false));
});

describe("should exclude log levels with info default", () => {
const run = async (source: string | undefined, level: string | null | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const run = async (source: string | undefined, level: LogLevel | null | undefined, settingKey: string | null | undefined, settingValue: string | null | undefined): Promise<boolean> => {
const client = new ExceptionlessClient();
client.config.settings["@@log:*"] = "Info";
client.config.settings["@@log:*"] = "info";
if (typeof settingKey === "string") {
client.config.settings[settingKey] = settingValue as string;
}
Expand All @@ -73,15 +73,15 @@ describe("EventExclusionPlugin", () => {

test("<null>", async () => expect(await run(undefined, null, null, null)).toBe(false));
test("Test", async () => expect(await run("Test", null, null, null)).toBe(false));
test("[Trace] Test", async () => expect(await run("Test", "Trace", null, null)).toBe(true));
test("[Warn] Test", async () => expect(await run("Test", "Warn", null, null)).toBe(false));
test("[Error] Test (source min level: Debug)", async () => expect(await run("Test", "Error", "@@log:Test", "Debug")).toBe(false));
test("[Debug] Test (source min level: Debug)", async () => expect(await run("Test", "Debug", "@@log:Test", "Debug")).toBe(false));
test("[trace] Test", async () => expect(await run("Test", "trace", null, null)).toBe(true));
test("[warn] Test", async () => expect(await run("Test", "warn", null, null)).toBe(false));
test("[error] Test (source min level: debug)", async () => expect(await run("Test", "error", "@@log:Test", "debug")).toBe(false));
test("[debug] Test (source min level: debug)", async () => expect(await run("Test", "debug", "@@log:Test", "debug")).toBe(false));
});

describe("should resolve null and undefined log source levels in reverse settings order", () => {
const plugin = new EventExclusionPlugin();
const settings: Record<string, string> = { "@@log:": "Info", "@@log:*": "Debug" };
const settings: Record<string, string> = { "@@log:": "info", "@@log:*": "debug" };

test("<undefined> (global min level: info)", () => expect(plugin.getMinLogLevel(settings, undefined)).toBe(2));
test("<empty> (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "")).toBe(2));
Expand All @@ -90,7 +90,7 @@ describe("EventExclusionPlugin", () => {

describe("should resolve log source levels and respect settings order", () => {
const plugin = new EventExclusionPlugin();
const settings = { "@@log:*": "Debug", "@@log:": "Info" };
const settings = { "@@log:*": "debug", "@@log:": "info" };

test("<empty> (source min level: info)", () => expect(plugin.getMinLogLevel(settings, "")).toBe(2));
test("* (global min level: debug)", () => expect(plugin.getMinLogLevel(settings, "*")).toBe(1));
Expand All @@ -112,11 +112,11 @@ describe("EventExclusionPlugin", () => {
const plugin = new EventExclusionPlugin();
const settings = {
"@@log:*": "Fatal",
"@@log:": "Debug",
"@@log:": "debug",
"@@log:abc*": "Off",
"@@log:abc.de*": "Debug",
"@@log:abc.def*": "Info",
"@@log:abc.def.ghi": "Trace"
"@@log:abc.de*": "debug",
"@@log:abc.def*": "info",
"@@log:abc.def.ghi": "trace"
};

test("<undefined> (source min level: debug)", () => expect(plugin.getMinLogLevel(settings, undefined)).toBe(1));
Expand All @@ -131,8 +131,8 @@ describe("EventExclusionPlugin", () => {
describe("should respect min log levels settings order", () => {
const plugin = new EventExclusionPlugin();
const settings = {
"@@log:abc.def.ghi": "Trace",
"@@log:abc.def*": "Info",
"@@log:abc.def.ghi": "trace",
"@@log:abc.def*": "info",
"@@log:abc*": "Off"
};

Expand Down

0 comments on commit dab8e4f

Please sign in to comment.