Skip to content

Fix debug level #333

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

Merged
merged 1 commit into from
Apr 23, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/restate-sdk-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"test": "jest --silent --maxWorkers=1",
"verify": "npm run format-check && npm run lint && npm run build",
"release": "",
"example": "RESTATE_DEBUG_LOGGING=OFF ts-node-dev --transpile-only ./src/example.ts",
"workflowexample": "RESTATE_DEBUG_LOGGING=OFF ts-node-dev --transpile-only ./src/workflow_example.ts",
"ingress": "RESTATE_DEBUG_LOGGING=OFF ts-node-dev --transpile-only ./src/ingress.ts"
"example": "RESTATE_JOURNAL_LOGGING=OFF ts-node-dev --transpile-only ./src/example.ts",
"workflowexample": "RESTATE_JOURNAL_LOGGING=OFF ts-node-dev --transpile-only ./src/workflow_example.ts",
"ingress": "RESTATE_JOURNAL_LOGGING=OFF ts-node-dev --transpile-only ./src/ingress.ts"
},
"dependencies": {
"@restatedev/restate-sdk": "^0.8.0",
Expand Down
29 changes: 13 additions & 16 deletions packages/restate-sdk/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
/* eslint-disable no-console */

export enum RestateLogLevel {
UNSET = 0,
DEBUG = 1,
TRACE = 2,
TRACE = 1,
DEBUG = 2,
INFO = 3,
WARN = 4,
ERROR = 5,
}

function logLevelName(level: RestateLogLevel) {
switch (level) {
case RestateLogLevel.UNSET:
return "UNSET";
case RestateLogLevel.DEBUG:
return "DEBUG";
case RestateLogLevel.TRACE:
return "TRACE";
case RestateLogLevel.DEBUG:
return "DEBUG";
case RestateLogLevel.INFO:
return "INFO";
case RestateLogLevel.WARN:
Expand All @@ -38,16 +35,16 @@ function logLevelName(level: RestateLogLevel) {
}
}

function logLevelFromName(name?: string): RestateLogLevel {
function logLevelFromName(name?: string): RestateLogLevel | null {
if (!name) {
return RestateLogLevel.UNSET;
return null;
}
const n = name.toUpperCase();
switch (n) {
case "DEBUG":
return RestateLogLevel.DEBUG;
case "TRACE":
return RestateLogLevel.TRACE;
case "DEBUG":
return RestateLogLevel.DEBUG;
case "INFO":
return RestateLogLevel.INFO;
case "WARN":
Expand All @@ -61,10 +58,10 @@ function logLevelFromName(name?: string): RestateLogLevel {

function logFunction(level: RestateLogLevel) {
switch (level) {
case RestateLogLevel.DEBUG:
return console.debug;
case RestateLogLevel.TRACE:
return console.trace;
case RestateLogLevel.DEBUG:
return console.debug;
case RestateLogLevel.INFO:
return console.info;
case RestateLogLevel.WARN:
Expand All @@ -79,7 +76,7 @@ function logFunction(level: RestateLogLevel) {
function readRestateLogLevel(): RestateLogLevel {
const env = globalThis.process?.env?.RESTATE_LOGGING;
const level = logLevelFromName(env);
if (level != RestateLogLevel.UNSET) {
if (level != null) {
return level;
}
const nodeEnv = globalThis.process?.env?.NODE_ENV;
Expand All @@ -92,7 +89,7 @@ function readRestateLogLevel(): RestateLogLevel {
return RestateLogLevel.INFO;
}

const RESTATE_LOG_LEVEL = readRestateLogLevel();
export const RESTATE_LOG_LEVEL = readRestateLogLevel();

export class LoggerContext {
readonly fqMethodName: string;
Expand Down Expand Up @@ -164,8 +161,8 @@ export function createRestateConsole(
const shouldLog: () => boolean = filter ?? (() => true);

return Object.create(console, {
debug: loggerForLevel(RestateLogLevel.DEBUG, shouldLog, prefix),
trace: loggerForLevel(RestateLogLevel.TRACE, shouldLog, prefix),
debug: loggerForLevel(RestateLogLevel.DEBUG, shouldLog, prefix),
info: loggerForLevel(RestateLogLevel.INFO, shouldLog, prefix),
warn: loggerForLevel(RestateLogLevel.WARN, shouldLog, prefix),
error: loggerForLevel(RestateLogLevel.ERROR, shouldLog, prefix),
Expand Down
8 changes: 4 additions & 4 deletions packages/restate-sdk/src/state_machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ export class StateMachine implements RestateStreamConsumer {
}

if (this.journal.nextEntryWillBeReplayed()) {
this.console.debugInvokeMessage("Resuming (replaying) function.");
this.console.debug("Resuming (replaying) function.");
} else {
this.console.debugInvokeMessage("Invoking function.");
this.console.debug("Invoking function.");
}

this.invocation.handler
Expand Down Expand Up @@ -386,7 +386,7 @@ export class StateMachine implements RestateStreamConsumer {
);
}

this.console.debugInvokeMessage("Function completed successfully.");
this.console.debug("Function completed successfully.");

// Mark the end of the invocation
this.send(new Message(END_MESSAGE_TYPE, new EndMessage()));
Expand Down Expand Up @@ -568,7 +568,7 @@ export class StateMachine implements RestateStreamConsumer {
this.send(msg);
}

this.console.debugInvokeMessage("Suspending function.");
this.console.debug("Suspending function.");

await this.finish();
}
Expand Down
47 changes: 20 additions & 27 deletions packages/restate-sdk/src/utils/message_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,47 @@

import { formatMessageType } from "../types/protocol";
import { formatMessageAsJson } from "./utils";
import { createRestateConsole, LoggerContext } from "../logger";
import {
createRestateConsole,
LoggerContext,
RESTATE_LOG_LEVEL,
RestateLogLevel,
} from "../logger";

/**
* The environment variable which is read to determine the debug log settings.
*/
export const DEBUG_LOGGING_ENV = "RESTATE_DEBUG_LOGGING";
const RESTATE_JOURNAL_LOGGING = "RESTATE_JOURNAL_LOGGING";

/**
* The values for the {@link DEBUG_LOGGING_ENV} variable.
* The values for the {@link RESTATE_JOURNAL_LOGGING} variable.
*/
export enum RestateDebugLogLevel {
enum JournalLoggingLogLevel {
/** No debug logging at all. Good for performance and avoid per-invocation log volume */
OFF,

/** Logs debug information for every Restate function invocation. */
INVOKE,

/** Logs debug information for every Restate effect (=journal event) inside an invocation,
* like RPC, state access, sideEffect, ... */
JOURNAL,
DEBUG,

/** Logs debug information for every Restate effect (=journal event) inside an invocation,
* like RPC, state access, sideEffect, ... Additionally, this adds a JSON representation
* of the journal message to the log. */
JOURNAL_VERBOSE,
TRACE,
}

const DEFAULT_DEBUG_LOG_LEVEL =
globalThis?.process?.env["NODE_ENV"]?.toUpperCase() === "PRODUCTION"
? RestateDebugLogLevel.OFF
: RestateDebugLogLevel.INVOKE;
globalThis?.process?.env["NODE_ENV"]?.toUpperCase() === "PRODUCTION" ||
RESTATE_LOG_LEVEL > RestateLogLevel.DEBUG
? JournalLoggingLogLevel.OFF
: JournalLoggingLogLevel.DEBUG;

function readLogLevel(): RestateDebugLogLevel {
const env = globalThis?.process?.env[DEBUG_LOGGING_ENV]?.toUpperCase();
function readLogLevel(): JournalLoggingLogLevel {
const env = globalThis?.process?.env[RESTATE_JOURNAL_LOGGING]?.toUpperCase();
if (env == undefined) {
return DEFAULT_DEBUG_LOG_LEVEL;
}
const idx = Object.keys(RestateDebugLogLevel)
const idx = Object.keys(JournalLoggingLogLevel)
.filter((t) =>
// Object.keys contains the numbers as well
// https://stackoverflow.com/questions/48768774/how-to-get-all-the-values-of-an-enum-with-typescript
Expand All @@ -68,8 +71,6 @@ function readLogLevel(): RestateDebugLogLevel {
const log_level = readLogLevel();

export type StateMachineConsole = Console & {
debugInvokeMessage: (msg: string) => void;

debugJournalMessage: (
logMessage: string,
messageType?: bigint,
Expand All @@ -83,23 +84,15 @@ export function createStateMachineConsole(
const console = createRestateConsole(context);

Object.defineProperties(console, {
debugInvokeMessage: {
value: (msg: string) => {
if (log_level >= RestateDebugLogLevel.INVOKE) {
console.debug(msg);
}
},
},
debugJournalMessage: {
value: (logMessage: string, messageType?: bigint, message?: any) => {
if (log_level >= RestateDebugLogLevel.JOURNAL) {
if (log_level >= JournalLoggingLogLevel.DEBUG) {
const type =
messageType !== undefined
? " ; " + formatMessageType(messageType)
: "";
const journalEvent =
log_level >= RestateDebugLogLevel.JOURNAL_VERBOSE &&
message !== undefined
log_level >= JournalLoggingLogLevel.TRACE && message !== undefined
? " : " + formatMessageAsJson(message)
: "";
console.debug(`${logMessage}${type}${journalEvent}`);
Expand Down