Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] spdlog v2.x status #3316

Open
jan-moravec opened this issue Jan 10, 2025 · 2 comments
Open

[question] spdlog v2.x status #3316

jan-moravec opened this issue Jan 10, 2025 · 2 comments

Comments

@jan-moravec
Copy link

Hello, I like the changes in v2.x branch and I would like to use it in my project. I'm just worried if it's stable and well tested as the v1.x branch, since there is no release yet.

I guess my question is whether it is recommended using the v2.x and if there is release planned soon.

Thanks.

@gabime
Copy link
Owner

gabime commented Jan 10, 2025

Well it passes all the tests, so it seems ok. But, it is wip and API will probably change. There is no release date yet.

Which changes in particular are you interested in ?

@jan-moravec
Copy link
Author

jan-moravec commented Jan 11, 2025

I like logs in JSON format with custom properties, so I wrote a wrapper around spdlog.

Here is example of log line:

{"timestamp":"2025-01-10T08:56:33.930+01:00","logger":"HFApi","level":"debug","process":8353,"thread":8353,"errorCode":0,"function":"HFParseResultInt","message":"Exit: HFParseResultInt(value=3) -> 0 [HFERROR_OK]"}

Here is a little code snippet:

class JsonLogger
{
public:
    explicit JsonLogger(std::shared_ptr<spdlog::logger> &&logger) : mLogger(std::move(logger))
    {
    }

    using Level = spdlog::level;
    template <typename... Args> using format_string_t = fmt::format_string<Args...>;

    template <typename... Args> void Info(const JsonProperties &props, format_string_t<Args...> fmt, Args &&...args)
    {
        Log(Level::info, props, fmt, std::forward<Args>(args)...);
    }

private:
    template <typename... Args>
    void Log(Level lvl, const JsonProperties &props, format_string_t<Args...> fmt, Args &&...args)
    {
        if (!mLogger->should_log(lvl))
        {
            return;
        }

        Log(lvl, props, fmt::format(fmt, std::forward<Args>(args)...));
    }

    void Log(Level lvl, const JsonProperties &props, std::string &&msg);
    {
        mLogger->log(
            lvl, props.ToString() + R"(,"message":)" + nlohmann::json(std::move(msg)).dump()
        );
    }

    std::shared_ptr<spdlog::logger> mLogger;
};

I found it easier to use the v2.x, I remember for exmaple that the check !mLogger->should_log(lvl) was not as easy in the version v1.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants