Implemented a json log formatter
This commit is contained in:
parent
62d4378109
commit
40890e9dbe
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* XmlTraceLogFormatter.h
|
||||
*
|
||||
* This source file is part of the FoundationDB open source project
|
||||
*
|
||||
* Copyright 2018 Apple Inc. and the FoundationDB project authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "flow/flow.h"
|
||||
#include "JsonTraceLogFormatter.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
void JsonTraceLogFormatter::addref()
|
||||
{
|
||||
ReferenceCounted<JsonTraceLogFormatter>::addref();
|
||||
}
|
||||
|
||||
void JsonTraceLogFormatter::delref()
|
||||
{
|
||||
ReferenceCounted<JsonTraceLogFormatter>::delref();
|
||||
}
|
||||
|
||||
const char* JsonTraceLogFormatter::getExtension()
|
||||
{
|
||||
return "json";
|
||||
}
|
||||
|
||||
const char* JsonTraceLogFormatter::getHeader()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
const char* JsonTraceLogFormatter::getFooter()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
void escapeString(std::stringstream& ss, const std::string source)
|
||||
{
|
||||
for (auto c : source) {
|
||||
if (c == '"') {
|
||||
ss << "\"";
|
||||
} else if (c == '\\') {
|
||||
ss << "\\\\";
|
||||
} else {
|
||||
ss << c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::string JsonTraceLogFormatter::formatEvent(const TraceEventFields& fields)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "{ ";
|
||||
for (auto iter = fields.begin(); iter != fields.end(); ++iter) {
|
||||
if (iter != fields.begin()) {
|
||||
ss << ", ";
|
||||
}
|
||||
ss << "\"";
|
||||
escapeString(ss, iter->first);
|
||||
ss << "\": \"";
|
||||
escapeString(ss, iter->second);
|
||||
ss << "\"";
|
||||
}
|
||||
ss << " }\r\n";
|
||||
return ss.str();
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* XmlTraceLogFormatter.h
|
||||
*
|
||||
* This source file is part of the FoundationDB open source project
|
||||
*
|
||||
* Copyright 2018 Apple Inc. and the FoundationDB project authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "FastRef.h"
|
||||
#include "Trace.h"
|
||||
|
||||
struct JsonTraceLogFormatter : public ITraceLogFormatter, ReferenceCounted<JsonTraceLogFormatter> {
|
||||
const char* getExtension() override;
|
||||
const char* getHeader() override; // Called when starting a new file
|
||||
const char* getFooter() override; // Called when ending a file
|
||||
std::string formatEvent(const TraceEventFields&) override; // Called for each event
|
||||
|
||||
void addref() override;
|
||||
void delref() override;
|
||||
};
|
|
@ -123,6 +123,7 @@ FlowKnobs::FlowKnobs(bool randomize, bool isSimulated) {
|
|||
init( TRACE_EVENT_METRIC_UNITS_PER_SAMPLE, 500 );
|
||||
init( TRACE_EVENT_THROTTLER_SAMPLE_EXPIRY, 1800.0 ); // 30 mins
|
||||
init( TRACE_EVENT_THROTTLER_MSG_LIMIT, 20000 );
|
||||
init( TRACE_FORMAT, "json" );
|
||||
|
||||
//TDMetrics
|
||||
init( MAX_METRICS, 600 );
|
||||
|
|
|
@ -145,6 +145,7 @@ public:
|
|||
int TRACE_EVENT_METRIC_UNITS_PER_SAMPLE;
|
||||
int TRACE_EVENT_THROTTLER_SAMPLE_EXPIRY;
|
||||
int TRACE_EVENT_THROTTLER_MSG_LIMIT;
|
||||
std::string TRACE_FORMAT;
|
||||
|
||||
//TDMetrics
|
||||
int64_t MAX_METRIC_SIZE;
|
||||
|
|
|
@ -22,10 +22,12 @@
|
|||
#include "flow/Trace.h"
|
||||
#include "flow/FileTraceLogWriter.h"
|
||||
#include "flow/XmlTraceLogFormatter.h"
|
||||
#include "flow/JsonTraceLogFormatter.h"
|
||||
#include "flow/flow.h"
|
||||
#include "flow/DeterministicRandom.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <cctype>
|
||||
#include <time.h>
|
||||
|
||||
#include "flow/IThreadPool.h"
|
||||
|
@ -41,6 +43,19 @@
|
|||
#undef min
|
||||
#endif
|
||||
|
||||
Reference<ITraceLogFormatter> createLogFormatter() {
|
||||
auto f = FLOW_KNOBS->TRACE_FORMAT;
|
||||
std::transform(f.begin(), f.end(), f.begin(), ::tolower);
|
||||
if (f == "json") {
|
||||
return Reference<ITraceLogFormatter>(new JsonTraceLogFormatter());
|
||||
} else if (f == "xml") {
|
||||
return Reference<ITraceLogFormatter>(new XmlTraceLogFormatter());
|
||||
} else {
|
||||
ASSERT(false);
|
||||
return Reference<ITraceLogFormatter>(new XmlTraceLogFormatter());
|
||||
}
|
||||
}
|
||||
|
||||
class DummyThreadPool : public IThreadPool, ReferenceCounted<DummyThreadPool> {
|
||||
public:
|
||||
~DummyThreadPool() {}
|
||||
|
@ -280,7 +295,14 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
TraceLog() : bufferLength(0), loggedLength(0), opened(false), preopenOverflowCount(0), barriers(new BarrierList), logTraceEventMetrics(false), formatter(new XmlTraceLogFormatter()) {}
|
||||
TraceLog()
|
||||
: bufferLength(0)
|
||||
, loggedLength(0)
|
||||
, opened(false)
|
||||
, preopenOverflowCount(0)
|
||||
, barriers(new BarrierList)
|
||||
, logTraceEventMetrics(false)
|
||||
, formatter(createLogFormatter()) {}
|
||||
|
||||
bool isOpen() const { return opened; }
|
||||
|
||||
|
|
Loading…
Reference in New Issue