# Poco

# class JSONFormatter

Library: Foundation
Package: Logging
Header: Poco/JSONFormatter.h

## Description

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.

## Inheritance

Direct Base Classes: Formatter

All Base Classes: Configurable, Formatter, RefCountedObject

## Member Summary

Member Functions: format, getPriorityName, getProperty, getThread, setProperty

Inherited Functions: duplicate, format, getProperty, referenceCount, release, setProperty

## Types Aliases

### Ptr

using Ptr = AutoPtr < JSONFormatter >;

## Enumerations

### ThreadFormat

JSONF_THREAD_NONE = 0

JSONF_THREAD_NAME = 1

JSONF_THREAD_ID = 2

JSONF_THREAD_OS_ID = 3

## Constructors

### JSONFormatter

JSONFormatter() = default;

Creates a JSONFormatter.

## Destructor

### ~JSONFormatter

~JSONFormatter() = default;

Destroys the JSONFormatter.

## Member Functions

### format

void format(
const Message & msg,
std::string & text
);

Formats the message as a JSON string.

See also: Poco::Formatter::format()

### getProperty

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.

See also: Poco::Formatter::getProperty()

### setProperty

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.

See also: Poco::Formatter::setProperty()

### getPriorityName

static const std::string & getPriorityName(
int prio
);

### getThread

std::string getThread(
const Message & message
) const;

## Variables

### PROP_THREAD

static const std::string PROP_THREAD;

### PROP_TIMES

static const std::string PROP_TIMES;