This is an automated email from the ASF dual-hosted git repository.

swebb2066 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git


The following commit(s) were added to refs/heads/master by this push:
     new 09fb1c26 Allow thread information to be included in JSON formatted 
output (#272)
09fb1c26 is described below

commit 09fb1c267373b6f83f1fe4a4fccd561032669c67
Author: Stephen Webb <[email protected]>
AuthorDate: Tue Oct 17 13:14:09 2023 +1100

    Allow thread information to be included in JSON formatted output (#272)
---
 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..2a48224f 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),
+               threadInfo(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 threadInfo; //= false
 };
 
 JSONLayout::JSONLayout() :
@@ -82,6 +86,16 @@ bool JSONLayout::getPrettyPrint() const
        return m_priv->prettyPrint;
 }
 
+void JSONLayout::setThreadInfo(bool newValue)
+{
+       m_priv->threadInfo = newValue;
+}
+
+bool JSONLayout::getThreadInfo() const
+{
+       return m_priv->threadInfo;
+}
+
 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->threadInfo)
+       {
+               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;

Reply via email to