User Tools

Site Tools


tools:logger:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tools:logger:start [2020/11/26 13:05] – [Default LogWriter] ursgraftools:logger:start [2026/04/13 11:12] (current) – [Adding Output to the SysLog] ursgraf
Line 38: Line 38:
 <code cpp> <code cpp>
   Logger::setDefaultStreamLogger(std::cout);   Logger::setDefaultStreamLogger(std::cout);
-  Logger log;  // this logger will output onto the default ''LogWriter''.+  Logger log = Logger::getLogger();  // this logger will output onto the default ''LogWriter''.
 </code> </code>
 If you forget to assign a default logger, you will not get logging information from components of the library such as the safety system or the executor. If you forget to assign a default logger, you will not get logging information from components of the library such as the safety system or the executor.
Line 48: Line 48:
 </code> </code>
 A log file will be created at the given location. Its file name will be appended with the current date and time of the creation of the file. A log file will be created at the given location. Its file name will be appended with the current date and time of the creation of the file.
 +
 +===== Adding Output to the SysLog =====
 +The simplest way to attach a second sink is via Logger::addWriter(), which promotes the existing writer into a MultiLogWriter automatically:
 +
 +<code cpp>
 +  Logger::setDefaultStreamLogger(std::cout, "/tmp/myLog");
 +  Logger::addWriter(std::make_shared<SysLogWriter>("myLog"));
 +  auto log = Logger::getLogger('A');
 +  log.show(LogLevel::INFO);  // propagates to both sinks
 +</code>
 +
 +Alternatively, build the MultiLogWriter explicitly before any getLogger()
 +call and hand it to Logger::setDefaultWriter():
 + 
 +<code cpp>
 +  #include <eeros/logger/MultiLogWriter.hpp>
 +
 +  auto multi = std::make_shared<MultiLogWriter>();
 +  multi->add(std::make_shared<StreamLogWriter>(std::cout, "/tmp/myLog"));
 +  multi->add(std::make_shared<SysLogWriter>("myLog"));
 +  Logger::setDefaultWriter(multi);
 +</code>
 +
 +View the produced log entries in the system log with 
 +<code bash>
 +$ journalctl -t myLog
 +</code>
tools/logger/start.1606392337.txt.gz · Last modified: by ursgraf