proxygen
folly::StandardLogHandler Class Reference

#include <StandardLogHandler.h>

Inheritance diagram for folly::StandardLogHandler:
folly::LogHandler

Public Member Functions

 StandardLogHandler (LogHandlerConfig config, std::shared_ptr< LogFormatter > formatter, std::shared_ptr< LogWriter > writer, LogLevel syncLevel=LogLevel::MAX_LEVEL)
 
 ~StandardLogHandler ()
 
const std::shared_ptr< LogFormatter > & getFormatter () const
 
const std::shared_ptr< LogWriter > & getWriter () const
 
LogLevel getLevel () const
 
void setLevel (LogLevel level)
 
void handleMessage (const LogMessage &message, const LogCategory *handlerCategory) override
 
void flush () override
 
LogHandlerConfig getConfig () const override
 
- Public Member Functions inherited from folly::LogHandler
virtual ~LogHandler ()=default
 

Private Attributes

std::atomic< LogLevellevel_ {LogLevel::NONE}
 
std::atomic< LogLevelsyncLevel_ {LogLevel::MAX_LEVEL}
 
const std::shared_ptr< LogFormatterformatter_
 
const std::shared_ptr< LogWriterwriter_
 
const LogHandlerConfig config_
 

Detailed Description

StandardLogHandler is a LogHandler implementation that uses a LogFormatter class to serialize the LogMessage into a string, and then gives it to a LogWriter object.

This basically is a simple glue class that helps chain together configurable LogFormatter and LogWriter objects.

StandardLogHandler also supports ignoring messages less than a specific LogLevel. By default it processes all messages.

Definition at line 41 of file StandardLogHandler.h.

Constructor & Destructor Documentation

folly::StandardLogHandler::StandardLogHandler ( LogHandlerConfig  config,
std::shared_ptr< LogFormatter formatter,
std::shared_ptr< LogWriter writer,
LogLevel  syncLevel = LogLevel::MAX_LEVEL 
)

Definition at line 24 of file StandardLogHandler.cpp.

References config, config_, folly::gen::move, and writer_.

29  : syncLevel_(syncLevel),
30  formatter_{std::move(formatter)},
31  writer_{std::move(writer)},
32  config_{config} {}
const std::shared_ptr< LogWriter > writer_
const LogHandlerConfig config_
std::atomic< LogLevel > syncLevel_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const std::shared_ptr< LogFormatter > formatter_
AHArrayT::Config config
folly::StandardLogHandler::~StandardLogHandler ( )

Definition at line 34 of file StandardLogHandler.cpp.

34 {}

Member Function Documentation

void folly::StandardLogHandler::flush ( )
overridevirtual

Block until all messages that have already been sent to this LogHandler have been processed.

For LogHandlers that perform asynchronous processing of log messages, this ensures that messages already sent to this handler have finished being processed.

Other threads may still call handleMessage() while flush() is running. handleMessage() calls that did not complete before the flush() call started will not necessarily be processed by the flush call.

Implements folly::LogHandler.

Definition at line 48 of file StandardLogHandler.cpp.

References writer_.

Referenced by handleMessage(), and setLevel().

48  {
49  writer_->flush();
50 }
const std::shared_ptr< LogWriter > writer_
LogHandlerConfig folly::StandardLogHandler::getConfig ( ) const
overridevirtual

Return a LogHandlerConfig object describing the configuration of this LogHandler.

Implements folly::LogHandler.

Definition at line 52 of file StandardLogHandler.cpp.

References config_.

Referenced by setLevel().

52  {
53  return config_;
54 }
const LogHandlerConfig config_
const std::shared_ptr<LogFormatter>& folly::StandardLogHandler::getFormatter ( ) const
inline

Get the LogFormatter used by this handler.

Definition at line 53 of file StandardLogHandler.h.

References formatter_.

53  {
54  return formatter_;
55  }
const std::shared_ptr< LogFormatter > formatter_
LogLevel folly::StandardLogHandler::getLevel ( ) const
inline

Get the handler's current LogLevel.

Messages less than this LogLevel will be ignored. This defaults to LogLevel::NONE when the handler is constructed.

Definition at line 70 of file StandardLogHandler.h.

References level_.

Referenced by handleMessage().

70  {
71  return level_.load(std::memory_order_acquire);
72  }
std::atomic< LogLevel > level_
const std::shared_ptr<LogWriter>& folly::StandardLogHandler::getWriter ( ) const
inline

Get the LogWriter used by this handler.

Definition at line 60 of file StandardLogHandler.h.

References writer_.

60  {
61  return writer_;
62  }
const std::shared_ptr< LogWriter > writer_
void folly::StandardLogHandler::handleMessage ( const LogMessage message,
const LogCategory handlerCategory 
)
overridevirtual

handleMessage() is called when a log message is processed by a LogCategory that this handler is attached to.

This must be implemented by LogHandler subclasses.

handleMessage() will always be invoked from the thread that logged the message. LogMessage::getThreadID() contains the thread ID, but the LogHandler can also include any other thread-local state they desire, and this will always be data for the thread that originated the log message.

Parameters
messageThe LogMessage objet.
handlerCategoryThe LogCategory that invoked handleMessage(). This is the category that this LogHandler is attached to. Note that this may be different than the category that this message was originally logged at. message->getCategory() returns the category of the log message.

Implements folly::LogHandler.

Definition at line 36 of file StandardLogHandler.cpp.

References flush(), formatter_, getLevel(), folly::LogMessage::getLevel(), syncLevel_, and writer_.

Referenced by setLevel(), and TEST().

38  {
39  if (message.getLevel() < getLevel()) {
40  return;
41  }
42  writer_->writeMessage(formatter_->formatMessage(message, handlerCategory));
43  if (message.getLevel() >= syncLevel_.load(std::memory_order_relaxed)) {
44  flush();
45  }
46 }
Definition: test.c:42
const std::shared_ptr< LogWriter > writer_
std::atomic< LogLevel > syncLevel_
const std::shared_ptr< LogFormatter > formatter_
void folly::StandardLogHandler::setLevel ( LogLevel  level)
inline

Set the handler's current LogLevel.

Messages less than this LogLevel will be ignored.

Definition at line 79 of file StandardLogHandler.h.

References flush(), getConfig(), handleMessage(), and level_.

Referenced by TEST().

79  {
80  return level_.store(level, std::memory_order_release);
81  }
std::atomic< LogLevel > level_

Member Data Documentation

const LogHandlerConfig folly::StandardLogHandler::config_
private

Definition at line 103 of file StandardLogHandler.h.

Referenced by getConfig(), and StandardLogHandler().

const std::shared_ptr<LogFormatter> folly::StandardLogHandler::formatter_
private

Definition at line 101 of file StandardLogHandler.h.

Referenced by getFormatter(), and handleMessage().

std::atomic<LogLevel> folly::StandardLogHandler::level_ {LogLevel::NONE}
private

Definition at line 92 of file StandardLogHandler.h.

Referenced by getLevel(), and setLevel().

std::atomic<LogLevel> folly::StandardLogHandler::syncLevel_ {LogLevel::MAX_LEVEL}
private

Definition at line 93 of file StandardLogHandler.h.

Referenced by handleMessage().

const std::shared_ptr<LogWriter> folly::StandardLogHandler::writer_
private

Definition at line 102 of file StandardLogHandler.h.

Referenced by flush(), getWriter(), handleMessage(), and StandardLogHandler().


The documentation for this class was generated from the following files: