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

added mongodb sink #1981

Merged
merged 5 commits into from
Jun 27, 2021
Merged

added mongodb sink #1981

merged 5 commits into from
Jun 27, 2021

Conversation

mguludag
Copy link
Contributor

Hello I've added custom sink for writing logs to mongodb. If I will have free time I want to make dynamic mongodb document structure based on format string pattern.

Dependencies

Usage samples

asynchronous logger

#include <spdlog/async.h>
#include <spdlog/sinks/mongo_sink.h>
#include <spdlog/spdlog.h>
#include <thread>

int main() {
  std::thread t1([&] {
    auto logging = spdlog::async_factory::create<spdlog::sinks::mongo_sink>(
        "mongo_logger_1", "db", "collection");
    for (auto i = 0; i < 10; ++i) {
      logging->info("thread_1 iter: {}",i);
      std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
  });

  std::thread t2([&] {
    auto logging = spdlog::async_factory::create<spdlog::sinks::mongo_sink>(
        "mongo_logger_2", "db", "collection");
    for (auto i = 0; i < 10; ++i) {
      logging->info("thread_2 iter: {}",i);
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
  });

  std::this_thread::sleep_for(std::chrono::seconds(10));

  t1.join();
  t2.join();

  return 0;
}

synchronous logger

#include <spdlog/sinks/mongo_sink.h>
#include <spdlog/spdlog.h>
#include <thread>

int main() {
    auto logging = spdlog::create<spdlog::sinks::mongo_sink>(
        "mongo_logger", "db", "collection");
  std::thread t1([&] {
    for (auto i = 0; i < 10; ++i) {
      logging->info("thread_1" + std::to_string(i));
      std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
  });

  std::thread t2([&] {
    for (auto i = 0; i < 10; ++i) {
      logging->info("thread_2" + std::to_string(i));
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
  });

  std::this_thread::sleep_for(std::chrono::seconds(10));

  t1.join();
  t2.join();

  return 0;
}

Example document from mongodb

{
  "_id": {
    "$oid": "60d87492314f000087006980"
  },
  "timestamp": {
    "$date": "2021-06-27T12:52:34.985Z"
  },
  "level": "info",
  "message": "thread_1 5",
  "logger_name": "mongo_logger1",
  "thread_id": 18892
}

@gabime
Copy link
Owner

gabime commented Jun 27, 2021

Thanks. Not sure about the conflicting license though.. spdlog is MIT and yours is GPL3

@mguludag
Copy link
Contributor Author

Thanks. Not sure about the conflicting license though.. spdlog is MIT and yours is GPL3

Ok, I forgot old license template in Qt creator. I'll change it to MIT license

Copy link
Contributor Author

@mguludag mguludag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License changed to MIT

@gabime
Copy link
Owner

gabime commented Jun 27, 2021

can you replace to (like all other spdlog files):

“// Distributed under the MIT License (http://opensource.org/licenses/MIT)”

Copy link
Contributor Author

@mguludag mguludag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I changed the license comment

@gabime
Copy link
Owner

gabime commented Jun 27, 2021

Thanks. It is better to inherit form base sink so that users can choose to create single or multithreaded sink.
Would be nice if you could fix it. see https://github.com/gabime/spdlog/wiki/4.-Sinks#implementing-your-own-sink for example.

Also printing to cerr id taken care of by the exception handker on the upper layer and should be removed here.

@mguludag
Copy link
Contributor Author

Thanks. It is better to inherit form base sink so that users can choose to create single or multithreaded sink.
Would be nice if you could fix it. see https://github.com/gabime/spdlog/wiki/4.-Sinks#implementing-your-own-sink for example.

Also printing to cerr id taken care of by the exception handker on the upper layer and should be removed here.

Also, why the mongocxx::instance instance is static? seems it should be regular member.

I changed the base class to base_sink and added factory functions. mongocxx::instance instance must not be constructed more than once or the it will crash.

@gabime gabime merged commit 13d8b0f into gabime:v1.x Jun 27, 2021
@gabime
Copy link
Owner

gabime commented Jun 27, 2021

Thanks. Merged and added a small fix (2a09f66)

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

Successfully merging this pull request may close these issues.

None yet

2 participants