2 * Copyright 2004-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <folly/experimental/logging/LogLevel.h>
25 class LogHandlerConfig;
29 * LogHandler represents a generic API for processing log messages.
31 * LogHandlers have an associated log level. The LogHandler will discard any
32 * messages below its log level. This allows specific LogHandlers to perform
33 * additional filtering of messages even if the messages were enabled at the
34 * LogCategory level. For instance, a single LogCategory may have two
35 * LogHandlers attached, one that logs locally to a file, and one that sends
36 * messages to a remote logging service. The local LogHandler may be
37 * configured to record all messages, but the remote LogHandler may want to
38 * only process ERROR messages and above, even when debug logging is enabled
39 * for this LogCategory.
41 * By default the LogHandler level is set to LogLevel::NONE, which means that
42 * all log messages will be processed.
46 virtual ~LogHandler() = default;
49 * handleMessage() is called when a log message is processed by a LogCategory
50 * that this handler is attached to.
52 * This must be implemented by LogHandler subclasses.
54 * handleMessage() will always be invoked from the thread that logged the
55 * message. LogMessage::getThreadID() contains the thread ID, but the
56 * LogHandler can also include any other thread-local state they desire, and
57 * this will always be data for the thread that originated the log message.
59 * @param message The LogMessage objet.
60 * @param handlerCategory The LogCategory that invoked handleMessage().
61 * This is the category that this LogHandler is attached to. Note that
62 * this may be different than the category that this message was
63 * originally logged at. message->getCategory() returns the category of
66 virtual void handleMessage(
67 const LogMessage& message,
68 const LogCategory* handlerCategory) = 0;
71 * Block until all messages that have already been sent to this LogHandler
72 * have been processed.
74 * For LogHandlers that perform asynchronous processing of log messages,
75 * this ensures that messages already sent to this handler have finished
78 * Other threads may still call handleMessage() while flush() is running.
79 * handleMessage() calls that did not complete before the flush() call
80 * started will not necessarily be processed by the flush call.
82 virtual void flush() = 0;
85 * Return a LogHandlerConfig object describing the configuration of this
88 virtual LogHandlerConfig getConfig() const = 0;