Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/include/log4cxx/jsonlayout.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#ifndef _LOG4CXX_JSON_LAYOUT_H
19
#define _LOG4CXX_JSON_LAYOUT_H
20
21
#include <log4cxx/layout.h>
22
#include <log4cxx/helpers/iso8601dateformat.h>
23
#include <log4cxx/spi/loggingevent.h>
24
25
26
namespace LOG4CXX_NS
27
{
28
/**
29
This layout outputs events in a JSON dictionary.
30
*/
31
class LOG4CXX_EXPORT JSONLayout : public Layout
32
{
33
  private:
34
    LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(JSONLayoutPrivate, m_priv)
35
36
  protected:
37
    void appendQuotedEscapedString(LogString& buf, const LogString& input) const;
38
    void appendSerializedMDC(LogString& buf,
39
      const spi::LoggingEventPtr& event) const;
40
    void appendSerializedNDC(LogString& buf,
41
      const spi::LoggingEventPtr& event) const;
42
    void appendSerializedLocationInfo(LogString& buf,
43
      const spi::LoggingEventPtr& event, LOG4CXX_NS::helpers::Pool& p) const;
44
45
  public:
46
    static void appendItem(const LogString& item, LogString& toAppendTo);
47
    DECLARE_LOG4CXX_OBJECT(JSONLayout)
48
0
    BEGIN_LOG4CXX_CAST_MAP()
49
0
    LOG4CXX_CAST_ENTRY(JSONLayout)
50
0
    LOG4CXX_CAST_ENTRY_CHAIN(Layout)
51
0
    END_LOG4CXX_CAST_MAP()
52
53
    JSONLayout();
54
55
    ~JSONLayout();
56
57
    /**
58
    The <b>LocationInfo</b> option takes a boolean value. By
59
    default, it is set to false which means there will be no location
60
    information output by this layout. If the the option is set to
61
    true, then the file name and line number of the statement
62
    at the origin of the log statement will be output.
63
    */
64
    void setLocationInfo(bool locationInfoFlag);
65
66
67
    /**
68
    Returns the current value of the <b>LocationInfo</b> option.
69
    */
70
    bool getLocationInfo() const;
71
72
    /**
73
    The <b>PrettyPrint</b> option takes a boolean value. By
74
    default, it is set to false which means output by this layout will
75
    be one line per log event.  If the option is set to true, then
76
    then each log event will produce multiple lines, each indented
77
    for readability.
78
    */
79
    void setPrettyPrint(bool prettyPrintFlag);
80
81
    /**
82
    Returns the current value of the <b>PrettyPrint</b> option.
83
    */
84
    bool getPrettyPrint() const;
85
86
    /**
87
    Set thread info output mode to \c newValue.
88
89
    @param newValue <code>true</code> to include a thread identifier.
90
    */
91
    void setThreadInfo(bool newValue);
92
93
    /**
94
    Is a thread identifier included in the output?
95
    */
96
    bool getThreadInfo() const;
97
98
    /**
99
    Returns the content type output by this layout, i.e "application/json".
100
    */
101
    LogString getContentType() const override;
102
103
    /**
104
    \copybrief spi::OptionHandler::activateOptions()
105
106
    No action is performed in this implementation.
107
    */
108
    void activateOptions(helpers::Pool& /* p */) override;
109
110
    /**
111
    \copybrief spi::OptionHandler::setOption()
112
113
    Supported options | Supported values | Default value
114
    -------------- | ---------------- | ---------------
115
    LocationInfo | True,False | false
116
    ThreadInfo | True,False | false
117
    PrettyPrint | True,False | false
118
    */
119
    void setOption(const LogString& option, const LogString& value) override;
120
121
    void format(LogString& output,
122
      const spi::LoggingEventPtr& event, helpers::Pool& pool) const override;
123
124
    /**
125
    The JSON layout handles the throwable contained in logging
126
    events. Hence, this method return <code>false</code>.  */
127
    bool ignoresThrowable() const override
128
0
    {
129
0
      return false;
130
0
    }
131
132
}; // class JSONLayout
133
LOG4CXX_PTR_DEF(JSONLayout);
134
}  // namespace log4cxx
135
136
#endif // _LOG4CXX_JSON_LAYOUT_H