Skip to content

Commit 6ad99bd

Browse files
committed
custom prefixes
1 parent b14a99e commit 6ad99bd

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

src/workerd/io/worker-fs.c++

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,16 +1764,13 @@ void writeStdio(jsg::Lock& js, VirtualFileSystem::Stdio type, kj::ArrayPtr<const
17641764
auto methodFunc = jsg::JsFunction(methodVal.As<v8::Function>());
17651765

17661766
kj::String outputStr;
1767-
auto processStdioPrefixed = Worker::Isolate::from(js).getProcessStdioPrefixed();
1768-
if (processStdioPrefixed) {
1769-
if (endPos == 0) {
1770-
methodFunc.call(js, console,
1771-
js.str(type == VirtualFileSystem::Stdio::OUT ? "stdout:"_kj : "stderr:"_kj));
1772-
} else {
1773-
methodFunc.call(js, console,
1774-
js.str(type == VirtualFileSystem::Stdio::OUT ? "stdout:"_kj : "stderr:"_kj),
1775-
js.str(chars.first(endPos)));
1776-
}
1767+
auto isolate = &Worker::Isolate::from(js);
1768+
auto prefix = type == VirtualFileSystem::Stdio::OUT ? isolate->getStdoutPrefix()
1769+
: isolate->getStderrPrefix();
1770+
if (endPos == 0) {
1771+
methodFunc.call(js, console, js.str(prefix));
1772+
} else if (prefix.size() > 0) {
1773+
methodFunc.call(js, console, js.str(kj::str(prefix, " "_kj, chars.first(endPos))));
17771774
} else {
17781775
methodFunc.call(js, console, js.str(chars.first(endPos)));
17791776
}

src/workerd/io/worker.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ class Worker: public kj::AtomicRefcounted {
123123
ConsoleMode consoleMode = Worker::ConsoleMode::STDOUT;
124124
StructuredLogging structuredLogging = StructuredLogging::NO;
125125
ProcessStdioPrefixed processStdioPrefixed = ProcessStdioPrefixed::YES;
126+
kj::String stdoutPrefix = kj::str("stdout:"_kj);
127+
kj::String stderrPrefix = kj::str("stderr:"_kj);
128+
129+
LoggingOptions() = default;
130+
LoggingOptions(LoggingOptions&&) = default;
131+
LoggingOptions& operator=(LoggingOptions&&) = default;
132+
133+
LoggingOptions(const LoggingOptions& other)
134+
: consoleMode(other.consoleMode),
135+
structuredLogging(other.structuredLogging),
136+
processStdioPrefixed(other.processStdioPrefixed),
137+
stdoutPrefix(kj::str(other.stdoutPrefix)),
138+
stderrPrefix(kj::str(other.stderrPrefix)) {}
139+
140+
LoggingOptions& operator=(const LoggingOptions& other) {
141+
consoleMode = other.consoleMode;
142+
structuredLogging = other.structuredLogging;
143+
processStdioPrefixed = other.processStdioPrefixed;
144+
stdoutPrefix = kj::str(other.stdoutPrefix);
145+
stderrPrefix = kj::str(other.stderrPrefix);
146+
return *this;
147+
}
126148
};
127149

128150
explicit Worker(kj::Own<const Script> script,
@@ -453,8 +475,12 @@ class Worker::Isolate: public kj::AtomicRefcounted {
453475
bool isInspectorEnabled() const;
454476

455477
// Get the process stdio prefixed setting from logging options
456-
inline ProcessStdioPrefixed getProcessStdioPrefixed() const {
457-
return loggingOptions.processStdioPrefixed;
478+
inline kj::StringPtr getStdoutPrefix() const {
479+
return loggingOptions.stdoutPrefix;
480+
}
481+
482+
inline kj::StringPtr getStderrPrefix() const {
483+
return loggingOptions.stderrPrefix;
458484
}
459485

460486
// Represents a weak reference back to the isolate that code within the isolate can use as an

src/workerd/server/server.c++

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5114,8 +5114,11 @@ kj::Promise<void> Server::run(
51145114

51155115
// Update logging settings from config
51165116
loggingOptions.structuredLogging = StructuredLogging(config.getStructuredLogging());
5117-
loggingOptions.processStdioPrefixed =
5118-
config.getProcessStdioPrefixed() ? ProcessStdioPrefixed::YES : ProcessStdioPrefixed::NO;
5117+
if (config.hasLogging()) {
5118+
auto logging = config.getLogging();
5119+
loggingOptions.stdoutPrefix = kj::str(logging.getStdoutPrefix());
5120+
loggingOptions.stderrPrefix = kj::str(logging.getStderrPrefix());
5121+
}
51195122

51205123
kj::HttpHeaderTable::Builder headerTableBuilder;
51215124
globalContext = kj::heap<GlobalContext>(*this, v8System, headerTableBuilder);
@@ -5509,8 +5512,11 @@ kj::Promise<bool> Server::test(jsg::V8System& v8System,
55095512
kj::StringPtr entrypointPattern) {
55105513
// Update logging settings from config
55115514
loggingOptions.structuredLogging = StructuredLogging(config.getStructuredLogging());
5512-
loggingOptions.processStdioPrefixed =
5513-
config.getProcessStdioPrefixed() ? ProcessStdioPrefixed::YES : ProcessStdioPrefixed::NO;
5515+
if (config.hasLogging()) {
5516+
auto logging = config.getLogging();
5517+
loggingOptions.stdoutPrefix = kj::str(logging.getStdoutPrefix());
5518+
loggingOptions.stderrPrefix = kj::str(logging.getStderrPrefix());
5519+
}
55145520

55155521
kj::HttpHeaderTable::Builder headerTableBuilder;
55165522
globalContext = kj::heap<GlobalContext>(*this, v8System, headerTableBuilder);

src/workerd/server/workerd.capnp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,17 @@ struct Config {
9191
# This affects the format of logs from KJ_LOG and exception reporting as well as js logs.
9292
# This won't work for logs coming from service worker syntax workers with the old module registry.
9393

94-
processStdioPrefixed @6 :Bool = true;
95-
# Whether to prefix process.stdout and process.stderr output with "stdout: " or "stderr: "
96-
# Default is true.
97-
# Set to false for backwards compat with unprefixed output.
94+
logging @6 : LoggingOptions;
95+
}
96+
97+
struct LoggingOptions {
98+
stdoutPrefix @0 :Text = "stdout:";
99+
# Prefix process.stdout output with "stdout: "
100+
# Can be set to the empty string or a custom prefix to customize.
101+
102+
stderrPrefix @1 :Text = "stderr:";
103+
# Prefix process.stderr output with "stderr: "
104+
# Can be set to the empty string or a custom prefix to customize.
98105
}
99106

100107
# ========================================================================================

0 commit comments

Comments
 (0)