]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | * Copyright Andrey Semashev 2007 - 2015. | |
3 | * Distributed under the Boost Software License, Version 1.0. | |
4 | * (See accompanying file LICENSE_1_0.txt or copy at | |
5 | * http://www.boost.org/LICENSE_1_0.txt) | |
6 | */ | |
7 | ||
8 | #include <string> | |
9 | #include <iostream> | |
10 | #include <boost/smart_ptr/shared_ptr.hpp> | |
11 | #include <boost/smart_ptr/make_shared_object.hpp> | |
12 | #include <boost/log/core.hpp> | |
13 | #include <boost/log/expressions.hpp> | |
14 | #include <boost/log/sinks/sync_frontend.hpp> | |
15 | #include <boost/log/sinks/text_multifile_backend.hpp> | |
16 | #include <boost/log/sources/logger.hpp> | |
17 | #include <boost/log/sources/record_ostream.hpp> | |
18 | #include <boost/log/attributes/scoped_attribute.hpp> | |
19 | ||
20 | namespace logging = boost::log; | |
21 | namespace src = boost::log::sources; | |
22 | namespace expr = boost::log::expressions; | |
23 | namespace sinks = boost::log::sinks; | |
24 | namespace keywords = boost::log::keywords; | |
25 | ||
26 | //[ example_sinks_multifile | |
27 | void init_logging() | |
28 | { | |
29 | boost::shared_ptr< logging::core > core = logging::core::get(); | |
30 | ||
31 | boost::shared_ptr< sinks::text_multifile_backend > backend = | |
32 | boost::make_shared< sinks::text_multifile_backend >(); | |
33 | ||
34 | // Set up the file naming pattern | |
35 | backend->set_file_name_composer | |
36 | ( | |
37 | sinks::file::as_file_name_composer(expr::stream << "logs/" << expr::attr< std::string >("RequestID") << ".log") | |
38 | ); | |
39 | ||
40 | // Wrap it into the frontend and register in the core. | |
41 | // The backend requires synchronization in the frontend. | |
42 | typedef sinks::synchronous_sink< sinks::text_multifile_backend > sink_t; | |
43 | boost::shared_ptr< sink_t > sink(new sink_t(backend)); | |
44 | ||
45 | // Set the formatter | |
46 | sink->set_formatter | |
47 | ( | |
48 | expr::stream | |
49 | << "[RequestID: " << expr::attr< std::string >("RequestID") | |
50 | << "] " << expr::smessage | |
51 | ); | |
52 | ||
53 | core->add_sink(sink); | |
54 | } | |
55 | //] | |
56 | ||
57 | void logging_function() | |
58 | { | |
59 | src::logger lg; | |
60 | BOOST_LOG(lg) << "Hello, world!"; | |
61 | } | |
62 | ||
63 | int main(int, char*[]) | |
64 | { | |
65 | init_logging(); | |
66 | ||
67 | { | |
68 | BOOST_LOG_SCOPED_THREAD_TAG("RequestID", "Request1"); | |
69 | logging_function(); | |
70 | } | |
71 | { | |
72 | BOOST_LOG_SCOPED_THREAD_TAG("RequestID", "Request2"); | |
73 | logging_function(); | |
74 | } | |
75 | ||
76 | return 0; | |
77 | } |