Implementation:Tensorflow Serving log collector h
| Knowledge Sources | |
|---|---|
| Domains | Model Serving, Request Logging |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
LogCollector defines an abstract interface for collecting and persisting log messages as protocol buffers, with a factory-based registration system.
Description
The LogCollector class provides a pluggable interface for log storage backends in TensorFlow Serving. Each implementation is registered with a string "type" identifier using a factory pattern. When a LogCollector is created via the static Create() method, the system looks up the factory registered for the requested type and uses it to construct the collector.
The interface defines two key operations:
CollectMessage()- Accepts a protobuf message for logging.Flush()- Ensures buffered data survives an application crash (but not an OS crash).
The factory registration system uses a static registry. Factories are registered via RegisterFactory() (which rejects duplicate type registrations) or via the REGISTER_LOG_COLLECTOR macro, which performs registration at global construction time before main().
The Create() method takes a LogCollectorConfig protobuf (containing the type and configuration) and a numeric id for disambiguating logs from replicated servers.
Usage
Implement the LogCollector interface to create a custom log storage backend (e.g., file-based, cloud-based). Register the implementation using the REGISTER_LOG_COLLECTOR macro. The logging pipeline (via RequestLogger) will use the registered factory to create instances.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File: tensorflow_serving/core/log_collector.h
- Lines: 1-92
Signature
class LogCollector {
public:
virtual ~LogCollector() = default;
static Status Create(const LogCollectorConfig& log_collector_config,
const uint32 id,
std::unique_ptr<LogCollector>* log_collector);
using Factory = std::function<decltype(Create)>;
static Status RegisterFactory(const string& type, const Factory& factory);
virtual Status CollectMessage(const google::protobuf::Message& message) = 0;
virtual Status Flush() = 0;
protected:
LogCollector() = default;
};
// Macro for registration
#define REGISTER_LOG_COLLECTOR(type, factory) ...
Import
#include "tensorflow_serving/core/log_collector.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| log_collector_config | const LogCollectorConfig& | Yes | Configuration protobuf containing the type and settings |
| id | uint32 | Yes | Numeric identifier for disambiguating logs from replicated servers |
| message | const google::protobuf::Message& | Yes | The protobuf message to be logged (for CollectMessage) |
Outputs
| Name | Type | Description |
|---|---|---|
| Create() | Status | OK on success; error if no factory registered for the type |
| CollectMessage() | Status | OK on success; error if the message could not be collected |
| Flush() | Status | OK on success; error if buffered data could not be flushed |
Usage Examples
Registering a Custom LogCollector
#include "tensorflow_serving/core/log_collector.h"
using namespace tensorflow::serving;
class FileLogCollector : public LogCollector {
public:
static Status Create(const LogCollectorConfig& config, const uint32 id,
std::unique_ptr<LogCollector>* collector) {
collector->reset(new FileLogCollector(config, id));
return Status();
}
Status CollectMessage(const google::protobuf::Message& message) override {
// Write message to file
return Status();
}
Status Flush() override {
// Flush file buffers
return Status();
}
};
REGISTER_LOG_COLLECTOR("file_log", FileLogCollector::Create);