This is an automated email from the ASF dual-hosted git repository. swebb2066 pushed a commit to branch add_thread_to_json_layout in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
commit f806a2c949a2e446365151ab9dbca5dded9cec37 Author: Stephen Webb <[email protected]> AuthorDate: Sat Oct 14 16:03:28 2023 +1100 Allow thread information to be included in JSON formatted output --- src/main/cpp/jsonlayout.cpp | 38 ++++++++++++++++++++++++++++++++--- src/main/include/log4cxx/jsonlayout.h | 12 +++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/main/cpp/jsonlayout.cpp b/src/main/cpp/jsonlayout.cpp index d8676122..12e7b800 100644 --- a/src/main/cpp/jsonlayout.cpp +++ b/src/main/cpp/jsonlayout.cpp @@ -40,7 +40,8 @@ struct JSONLayout::JSONLayoutPrivate dateFormat(), ppIndentL1(LOG4CXX_STR(" ")), ppIndentL2(LOG4CXX_STR(" ")), - expectedPatternLength(100) {} + expectedPatternLength(100), + thread(false) {} // Print no location info by default bool locationInfo; //= false @@ -53,6 +54,9 @@ struct JSONLayout::JSONLayoutPrivate // Expected length of a formatted event excluding the message text size_t expectedPatternLength; + + // Thread info is not included by default + bool thread; //= false }; JSONLayout::JSONLayout() : @@ -82,6 +86,16 @@ bool JSONLayout::getPrettyPrint() const return m_priv->prettyPrint; } +void JSONLayout::setThreadInfo(bool newValue) +{ + m_priv->thread = newValue; +} + +bool JSONLayout::getThreadInfo() const +{ + return m_priv->thread; +} + LogString JSONLayout::getContentType() const { return LOG4CXX_STR("application/json"); @@ -99,13 +113,18 @@ void JSONLayout::setOption(const LogString& option, const LogString& value) { setLocationInfo(OptionConverter::toBoolean(value, false)); } - - if (StringHelper::equalsIgnoreCase(option, + else if (StringHelper::equalsIgnoreCase(option, + LOG4CXX_STR("THREADINFO"), LOG4CXX_STR("threadinfo"))) + { + setThreadInfo(OptionConverter::toBoolean(value, false)); + } + else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PRETTYPRINT"), LOG4CXX_STR("prettyprint"))) { setPrettyPrint(OptionConverter::toBoolean(value, false)); } } + void JSONLayout::format(LogString& output, const spi::LoggingEventPtr& event, Pool& p) const @@ -127,6 +146,19 @@ void JSONLayout::format(LogString& output, output.append(LOG4CXX_STR(",")); output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" ")); + if (m_priv->thread) + { + if (m_priv->prettyPrint) + { + output.append(m_priv->ppIndentL1); + } + appendQuotedEscapedString(output, LOG4CXX_STR("thread")); + output.append(LOG4CXX_STR(": ")); + appendQuotedEscapedString(output, event->getThreadName()); + output.append(LOG4CXX_STR(",")); + output.append(m_priv->prettyPrint ? LOG4CXX_EOL : LOG4CXX_STR(" ")); + } + if (m_priv->prettyPrint) { output.append(m_priv->ppIndentL1); diff --git a/src/main/include/log4cxx/jsonlayout.h b/src/main/include/log4cxx/jsonlayout.h index a3aa3cf5..3f4a2a1c 100644 --- a/src/main/include/log4cxx/jsonlayout.h +++ b/src/main/include/log4cxx/jsonlayout.h @@ -83,6 +83,17 @@ class LOG4CXX_EXPORT JSONLayout : public Layout */ bool getPrettyPrint() const; + /** + Set thread info output mode to \c newValue. + + @param newValue <code>true</code> to include a thread identifier. + */ + void setThreadInfo(bool newValue); + + /** + Is a thread identifier included in the output? + */ + bool getThreadInfo() const; /** Returns the content type output by this layout, i.e "application/json". @@ -102,6 +113,7 @@ class LOG4CXX_EXPORT JSONLayout : public Layout Supported options | Supported values | Default value -------------- | ---------------- | --------------- LocationInfo | True,False | false + ThreadInfo | True,False | false PrettyPrint | True,False | false */ void setOption(const LogString& option, const LogString& value) override;
