Added some logging messages to the client, and disabled them by default

This commit is contained in:
Eric Allam
2023-01-18 18:25:52 -08:00
parent 5de2a1aec2
commit 7f2654884a
4 changed files with 28 additions and 19 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Added some logging messages (and disabled any messages by default)
+1 -10
View File
@@ -11,7 +11,7 @@ const trigger = new Trigger({
name: "My workflow",
apiKey: "trigger_dev_zC25mKNn6c0q",
endpoint: "ws://localhost:8889/ws",
logLevel: "debug",
logLevel: "log",
on: customEvent({ name: "user.created", schema: userCreatedEvent }),
run: async (event, ctx) => {
await ctx.logger.info("Inside the smoke test workflow, received event", {
@@ -34,12 +34,3 @@ const trigger = new Trigger({
});
trigger.listen();
(async () => {
process.env.TRIGGER_API_KEY = "trigger_dev_zC25mKNn6c0q";
process.env.TRIGGER_API_URL = "http://localhost:3000";
await sendEvent(ulid(), {
name: "user.created",
payload: { id: "123" },
});
})();
+16 -9
View File
@@ -1,14 +1,21 @@
// Create a logger class that uses the debug package internally
export type LogLevel = "log" | "error" | "warn" | "info" | "debug";
const logLevels = [
"disabled",
"log",
"error",
"warn",
"info",
"debug",
] as const;
const logLevels: Array<LogLevel> = ["log", "error", "warn", "info", "debug"];
export type LogLevel = (typeof logLevels)[number];
export class Logger {
#name: string;
readonly #level: number;
constructor(name: string, level: LogLevel = "info") {
constructor(name: string, level: LogLevel = "disabled") {
this.#name = name;
this.#level = logLevels.indexOf(
(process.env.TRIGGER_LOG_LEVEL ?? level) as LogLevel
@@ -16,31 +23,31 @@ export class Logger {
}
log(...args: any[]) {
if (this.#level < 0) return;
if (this.#level < 1) return;
console.log(`[${formattedDateTime()}] [${this.#name}] `, ...args);
console.log(`[${this.#name}] `, ...args);
}
error(...args: any[]) {
if (this.#level < 1) return;
if (this.#level < 2) return;
console.error(`[${formattedDateTime()}] [${this.#name}] `, ...args);
}
warn(...args: any[]) {
if (this.#level < 2) return;
if (this.#level < 3) return;
console.warn(`[${formattedDateTime()}] [${this.#name}] `, ...args);
}
info(...args: any[]) {
if (this.#level < 3) return;
if (this.#level < 4) return;
console.info(`[${formattedDateTime()}] [${this.#name}] `, ...args);
}
debug(...args: any[]) {
if (this.#level < 4) return;
if (this.#level < 5) return;
console.debug(`[${formattedDateTime()}] [${this.#name}] `, ...args);
}
+6
View File
@@ -362,6 +362,12 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
return this.#trigger.options
.run(eventData, ctx)
.then((output) => {
this.#logger.log(
`Completed workflow '${this.#options.name}', run ${
data.id
} 🏃`
);
return serverRPC.send("COMPLETE_WORKFLOW_RUN", {
runId: data.id,
output: JSON.stringify(output),