File: JSONFormatter.h

package info (click to toggle)
poco 1.14.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 56,460 kB
  • sloc: cpp: 340,542; ansic: 245,601; makefile: 1,742; yacc: 1,005; sh: 698; sql: 312; lex: 282; xml: 128; perl: 29; python: 24
file content (109 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// JSONFormatter.h
//
// Library: Foundation
// Package: Logging
// Module:  JSONFormatter
//
// Definition of the JSONFormatter class.
//
// Copyright (c) 2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#ifndef Foundation_JSONFormatter_INCLUDED
#define Foundation_JSONFormatter_INCLUDED


#include "Poco/Foundation.h"
#include "Poco/Formatter.h"
#include "Poco/Message.h"
#include <vector>


namespace Poco {


class Foundation_API JSONFormatter: public Formatter
	/// This formatter formats log messages as compact 
	/// (no unnecessary whitespace) single-line JSON strings.
	///
	/// The following JSON schema is used:
	///     {
	///         "timestamp": "2024-09-26T13:41:23.324461Z",
	///         "source": "sample",
	///         "level": "information",
	///         "message": "This is a test message.",
	///         "thread": 12,
	///         "file": "source.cpp",
	///         "line": 456,
	///         "params": {
	///             "prop1": "value1"
	///         }
	///     }
	///
	/// The "file" and "line" properties will only be included if the log
	/// message contains a file name and line number.
	///
	/// The "params" object will only be included if custom parameters
	/// have been added to the Message.
{
public:
	using Ptr = AutoPtr<JSONFormatter>;

	JSONFormatter() = default;
		/// Creates a JSONFormatter.

	~JSONFormatter() = default;
		/// Destroys the JSONFormatter.

	void format(const Message& msg, std::string& text);
		/// Formats the message as a JSON string.

	void setProperty(const std::string& name, const std::string& value);
		/// Sets the property with the given name to the given value.
		///
		/// The following properties are supported:
		///
		///     * times: Specifies whether times are adjusted for local time
		///       or taken as they are in UTC. Supported values are "local" and "UTC".
		///     * thread: Specifies the value given for the thread. Can be
		///       "none" (excluded), "name" (thread name), "id" (POCO thread ID) or "osid" 
		///       (operating system thread ID).
		///
		/// If any other property name is given, a PropertyNotSupported
		/// exception is thrown.

	std::string getProperty(const std::string& name) const;
		/// Returns the value of the property with the given name or
		/// throws a PropertyNotSupported exception if the given
		/// name is not recognized.

	static const std::string PROP_TIMES;
	static const std::string PROP_THREAD;

protected:
	std::string getThread(const Message& message) const;
	static const std::string& getPriorityName(int prio);

	enum ThreadFormat
	{
		JSONF_THREAD_NONE = 0,
		JSONF_THREAD_NAME = 1,
		JSONF_THREAD_ID = 2,
		JSONF_THREAD_OS_ID = 3
	};

private:
	bool _localTime = false;
	ThreadFormat _threadFormat = JSONF_THREAD_ID;
};


} // namespace Poco


#endif // Foundation_JSONFormatter_INCLUDED