Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#12364 closed Support Requests (invalid)

In boost::log, how to re-connect to a remote server (like logstash)?

Reported by: xinyan.zhang@… Owned by: Andrey Semashev
Milestone: To Be Determined Component: log
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

By using boost::asio::ip::tcp::iostream and boost::log, I am trying to send log records to a remote server like logstash. But I need to handle the network disconnection/reconnection. Unfortunately, I did not find any way for tcp::iostream.

Is it possible to get a notification or exception when the tcp connection is broken? Can I use ip::tcp::socket instead of tcp::iostream in boost::log? Thank you.

My code looks like:

typedef boost::log::sinks::asynchronous_sink< boost::log::sinks::text_ostream_backend > tcp_sink;
boost::shared_ptr< tcp_sink > networkSink;

// init tcp stream
boost::shared_ptr< sinks::text_ostream_backend > backend =
    boost::make_shared< sinks::text_ostream_backend >();

boost::shared_ptr< boost::asio::ip::tcp::iostream > stream =    boost::make_shared< boost::asio::ip::tcp::iostream >();
stream->connect("logstash", "1111");


backend->add_stream(stream);

networkSink = boost::make_shared< tcp_sink >();
networkSink->set_formatter(fmt);
networkSink->set_filter(severity <= severityThreshold); 
networkSink = boost::make_shared<tcp_sink>(backend );
logging::core::get()->add_sink(networkSink);


Change History (4)

comment:1 by Andrey Semashev, 6 years ago

Resolution: invalid
Status: newclosed

Boost.Log does not provide a network-based sink out of the box and the closest you can get with the available components is with text_ostream_backend and tcp::iostream. I'm not familiar with tcp::iostream but I suspect the stream obtains bad iostate when the connection is closed. If so, you can unmask exceptions in the stream and then use exception handlers to reconnect. Please refer to Boost.ASIO documentation to verify that.

You can also implement your own sink backend where you can use the socket directly.

comment:2 by xinyan.zhang@…, 6 years ago

Thank you. I will try.

comment:3 by xinyan.zhang@…, 6 years ago

If I implement the TCP reconnect sink backend, can I contribute to boost open source library? Any code standard or coding rule I need to meet? Where shall I put my code? git hub? (I am serious) Thank you.

comment:4 by Andrey Semashev, 6 years ago

The best way to contribute is to create a pull request on GitHub. The code should meet the common guideleines of Boost and also follow the formatting rules of Boost.Log. Please, don't forget to add tests and documentation. All code/docs must be licensed under the Boost Software License.

Note that I'm not promising to accept the pull request - most likely I will have to review it first. But thank you anyway.

Note: See TracTickets for help on using tickets.