3 Nisan 2018 Salı

log add_file_log metodu

Giriş
Şu satırı dahil ederiz.
#include <boost/log/utility/setup/file.hpp>
Global bir sink oluşturur. Source ve Sink arasındaki fark şöyledir.
                                  +--------------+
                            +---> | console sink | ----> stdout
                            |     +--------------+
                            |
+--------+      +------+    |     +--------------+
| source | ---> | core | ---+---> | file sink    | ----> log1.txt
+--------+      +------+    |     +--------------+
                            |
                            |     +--------------+
                            +---> | file sink    | ----> log2.txt
                                  +--------------+
İmza
Metodun imzası şöyle
shared_ptr< sinks::text_file_backend > > add_file_log (Arg1T... const& args);
Sink tipinden bir nesne döner. Daha kolay kodlamak için şöyle yaparız.
auto fileOutput = logging::add_file_log (...);
Basit kullanımı
Şöyle yaparız.
logging::add_file_log("sample.log");
add_file_log metodu - args
args "named parameter" olduğu için istenilen sırada verilir. Parametreleri kolay kullanmak için şu satır dahil edilebilir.
namespace keywords = boost::log::keywords;
auto_flush parametresi
Şöyle yaparız.
boost::log::add_file_log (boost::log::keywords::auto_flush = true,...);
Şöyle yaparız.
boost::log::add_file_log
(
  boost::log::keywords::auto_flush          = true,
  boost::log::keywords::target              = "Log",        
  boost::log::keywords::file_name           = "Log/App_%Y%m%d.log",
  boost::log::keywords::open_mode           = std::ios::out | std::ios::app,
  boost::log::keywords::time_based_rotation =
    boost::log::sinks::file::rotation_at_time_point(0, 0, 0)
);
file_name parametresi
Dosya ismi örüntüsü ve collector'ı  beraber kullanmak için şöyle yaparız.
keywords::file_name = "application_%N.log",
keywords::rotation_size = 5 * 1024 * 1024,
keywords::max_size = 16 * 1024 * 1024,
Bu durumda dosya isimleri şöyledir.
application_1.log, application_2.log, application_3.log .... application_N.log 
En son dosyayı tail -f ile izleme şansı olmuyor. Açıklaması şöyle
No, this is not supported. You will have to implement your own file collector that derives from collector interface and performs the necessary actions on file rotation. Note that the file name is generated by the sink backend, which means that you may need to modify it as well.
Şöyle yaparız.
std::string file = "...";
boost::log::add_file_log (boost::log::keywords::file_name = file,...);
Şöyle yaparız.
boost::log::keywords::file_name = "log%Y-%m-%d_%H-%M-%S.%N.log",
Şöyle yaparız.
boost::log::add_file_log (boost::log::keywords::file_name = "Log_%3N.log",...);
format parametresi
Şöyle yaparız.
std::string the_format = "...";
boost::log::add_file_log (boost::log::keywords::format = the_format,...);
Şöyle yaparız.
boost::log::keywords::format  = "[%TimeStamp%] (%Severity%) : %Message%",
format string'i yerine expr::stream sınıfı kullanılabilir.

min_free_space parametresi
Şöyle yaparız.
auto x = boost::log::add_file_log(
  boost::log::keywords::min_free_space        = 30 * 1024 * 1024,
  ...
);
max_size parametresi
Şöyle yaparız.
auto x = boost::log::add_file_log(
  boost::log::keywords::max_size              = 20 * 1024,
  ...
);
open_mode parametresi
Şöyle yaparız.
boost::log::add_file_log (
  boost::log::keywords::open_mode = (std::ios::out | std::ios::app),...);
rotation_size parametresi
Şöyle yaparız
boost::log::add_file_log (boost::log::keywords::rotation_size = 10 * 1024 * 1024,
  ...
);
scan_method parametresi
Şöyle yaparız. Mevcut dosyaya ekleme yaparak devam etmeyi sağlar.
auto x = boost::log::add_file_log(
  boost::log::keywords::scan_method = boost::log::sinks::file::scan_matching,
  ...
);
targets parametresi
Açıklaması şöyle.
Two things need to be done for this. First, you have to configure the file collector, which will receive rotated log files and optionally manage them. File collector operates on one target directory, where all the rotated files are stored. You can specify that directory by adding target named parameter to your add_file_log call; this will create a file collector for your sink. Note that the target directory can be the same as the one your sink creates the file in - it will simply mean that moving the rotated log file to the target directory is a no-op.

Second, on your application startup, the scan_for_files method needs to be called on the sink backend. This will cause the file collector scan the target directory for log files that could have been left from the previous runs. If the file name pattern includes a file counter, the call can also detect the next counter value, which is what you want. The add_file_log function will do this automatically for you, if you configured the file collector (i.e. added the target named parameter), but if you create the sink manually, you will have to call it yourself.
Şöyle yaparız.
auto x = boost::log::add_file_log(
  boost::log::keywords::target                = "Logs",
  ...
);
time_based_rotation parametresi
Şöyle yaparız
boost::log::add_file_log
(
  boost::log::keywords::time_based_rotation =
    boost::log::sinks::file::rotation_at_time_point (0, 0, 0),
  ...
);
Şöyle yaparız.
auto x = boost::log::add_file_log(
  boost::log::keywords::time_based_rotation  =
   boost::log::sinks::file::rotation_at_time_point
    (boost::gregorian::greg_day(31)),
  ...
);




Hiç yorum yok:

Yorum Gönder