Skip to content

Commit efab3b2

Browse files
committed
remove spdlog logger
1 parent bbd4987 commit efab3b2

26 files changed

+442
-221
lines changed

CMakeLists.txt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ set(HTTPLIB_DEFINITIONS
2424
CPPHTTPLIB_THREAD_POOL_COUNT=8
2525
)
2626

27+
include(cmake/CPM.cmake)
28+
29+
CPMAddPackage(
30+
NAME nlohmann_json
31+
GITHUB_REPOSITORY nlohmann/json
32+
VERSION 3.11.3
33+
OPTIONS
34+
"JSON_BuildTests OFF"
35+
"JSON_Install ON"
36+
)
37+
2738
# Find required packages through vcpkg
28-
find_package(nlohmann_json CONFIG REQUIRED)
29-
find_package(spdlog CONFIG REQUIRED)
3039
find_package(httplib CONFIG REQUIRED)
3140
find_package(OpenSSL REQUIRED)
3241
find_package(unofficial-concurrentqueue CONFIG REQUIRED)
@@ -125,9 +134,9 @@ target_sources(ai-sdk-cpp-anthropic
125134

126135
# Link dependencies for core component
127136
target_link_libraries(ai-sdk-cpp-core
128-
PRIVATE
137+
PUBLIC
129138
nlohmann_json::nlohmann_json
130-
spdlog::spdlog
139+
PRIVATE
131140
httplib::httplib
132141
OpenSSL::SSL
133142
OpenSSL::Crypto
@@ -138,9 +147,9 @@ target_link_libraries(ai-sdk-cpp-core
138147
target_link_libraries(ai-sdk-cpp-openai
139148
PUBLIC
140149
ai::core
141-
PRIVATE
142150
nlohmann_json::nlohmann_json
143-
spdlog::spdlog
151+
PRIVATE
152+
httplib::httplib
144153
)
145154

146155
# Define component availability for OpenAI
@@ -155,9 +164,9 @@ target_compile_definitions(ai-sdk-cpp-openai
155164
target_link_libraries(ai-sdk-cpp-anthropic
156165
PUBLIC
157166
ai::core
158-
PRIVATE
159167
nlohmann_json::nlohmann_json
160-
spdlog::spdlog
168+
PRIVATE
169+
httplib::httplib
161170
)
162171

163172
# Define component availability for Anthropic

DEVELOPMENT.md

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -302,52 +302,55 @@ cd build && ctest
302302

303303
### Configuring Log Levels
304304

305-
AI SDK C++ uses [spdlog](https://github.com/gabime/spdlog) for logging.
305+
AI SDK C++ uses a built-in logging system defined in `ai/logger.h`.
306306

307307
### Setting Log Levels
308308

309-
You can control the logging verbosity by setting the spdlog level in your application:
309+
You can control the logging verbosity by configuring the logger in your application:
310310

311311
```cpp
312-
#include <spdlog/spdlog.h>
312+
#include "ai/logger.h"
313313

314314
// In your main() or initialization code:
315315

316316
// Enable debug logging (most verbose)
317-
spdlog::set_level(spdlog::level::debug);
317+
ai::logger::install_logger(
318+
std::make_shared<ai::logger::ConsoleLogger>(ai::logger::LogLevel::kLogLevelDebug)
319+
);
318320

319321
// Enable info logging (operational information)
320-
spdlog::set_level(spdlog::level::info);
322+
ai::logger::install_logger(
323+
std::make_shared<ai::logger::ConsoleLogger>(ai::logger::LogLevel::kLogLevelInfo)
324+
);
321325

322326
// Enable warning logging (default)
323-
spdlog::set_level(spdlog::level::warn);
327+
ai::logger::install_logger(
328+
std::make_shared<ai::logger::ConsoleLogger>(ai::logger::LogLevel::kLogLevelWarn)
329+
);
324330

325331
// Enable error logging only
326-
spdlog::set_level(spdlog::level::err);
332+
ai::logger::install_logger(
333+
std::make_shared<ai::logger::ConsoleLogger>(ai::logger::LogLevel::kLogLevelError)
334+
);
327335
```
328336

329337
### Available Log Levels
330338

331339
From most to least verbose:
332340

333-
1. **trace**: Most detailed information (not commonly used in AI SDK)
334-
2. **debug**: Detailed flow information, request/response bodies, connection details
335-
3. **info**: Important operational events (successful completions, stream events)
336-
4. **warn**: Warning conditions that don't prevent operation
337-
5. **err**: Error conditions and exceptions
338-
6. **critical**: Critical failures (not commonly used in AI SDK)
339-
7. **off**: Disable all logging
341+
1. **debug**: Detailed flow information, request/response bodies, connection details
342+
2. **info**: Important operational events (successful completions, stream events)
343+
3. **warn**: Warning conditions that don't prevent operation
344+
4. **error**: Error conditions and exceptions
340345

341-
### Environment Variable Configuration
346+
### Null Logger
342347

343-
You can also set the log level via environment variable:
348+
To disable all logging, you can install a null logger:
344349

345-
```bash
346-
# Enable debug logging
347-
export SPDLOG_LEVEL=debug
348-
349-
# Enable info logging
350-
export SPDLOG_LEVEL=info
350+
```cpp
351+
ai::logger::install_logger(
352+
std::make_shared<ai::logger::NullLogger>()
353+
);
351354
```
352355

353356
### Example Log Output
@@ -374,25 +377,44 @@ With **info** level enabled:
374377

375378
### Custom Logger Configuration
376379

377-
For more advanced logging configurations:
380+
You can create your own logger by implementing the `ai::logger::Logger` interface:
378381

379382
```cpp
380-
#include <spdlog/spdlog.h>
381-
#include <spdlog/sinks/basic_file_sink.h>
382-
#include <spdlog/sinks/stdout_color_sinks.h>
383-
384-
// Create a multi-sink logger (console + file)
385-
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
386-
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("ai_sdk.log", true);
387-
388-
std::vector<spdlog::sink_ptr> sinks {console_sink, file_sink};
389-
auto logger = std::make_shared<spdlog::logger>("ai_sdk", sinks.begin(), sinks.end());
390-
391-
// Set as default logger
392-
spdlog::set_default_logger(logger);
383+
#include "ai/logger.h"
384+
#include <fstream>
385+
386+
class FileLogger : public ai::logger::Logger {
387+
public:
388+
FileLogger(const std::string& filename)
389+
: file_(filename, std::ios::app) {}
390+
391+
void log(ai::logger::LogLevel level, std::string_view message) override {
392+
if (is_enabled(level)) {
393+
file_ << "[" << level_to_string(level) << "] " << message << std::endl;
394+
}
395+
}
396+
397+
bool is_enabled(ai::logger::LogLevel level) const override {
398+
return level >= min_level_;
399+
}
400+
401+
private:
402+
std::ofstream file_;
403+
ai::logger::LogLevel min_level_ = ai::logger::LogLevel::kLogLevelInfo;
404+
405+
static std::string_view level_to_string(ai::logger::LogLevel level) {
406+
switch (level) {
407+
case ai::logger::LogLevel::kLogLevelDebug: return "DEBUG";
408+
case ai::logger::LogLevel::kLogLevelInfo: return "INFO";
409+
case ai::logger::LogLevel::kLogLevelWarn: return "WARN";
410+
case ai::logger::LogLevel::kLogLevelError: return "ERROR";
411+
}
412+
return "UNKNOWN";
413+
}
414+
};
393415

394-
// Configure format
395-
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%t] %v");
416+
// Install custom logger
417+
ai::logger::install_logger(std::make_shared<FileLogger>("ai_sdk.log"));
396418
```
397419
398420
### Development Recommendations
@@ -417,7 +439,9 @@ class AITestFixture : public ::testing::Test {
417439
protected:
418440
void SetUp() override {
419441
// Enable debug logging for tests
420-
spdlog::set_level(spdlog::level::debug);
442+
ai::logger::install_logger(
443+
std::make_shared<ai::logger::ConsoleLogger>(ai::logger::LogLevel::kLogLevelDebug)
444+
);
421445
}
422446
};
423447
```
@@ -430,7 +454,7 @@ Managed via `vcpkg.json`:
430454
431455
- **fmt**: Fast formatting library
432456
- **nlohmann-json**: JSON parsing and generation
433-
- **spdlog**: Fast logging library
457+
- **Built-in logging**: ai::logger provides flexible logging capabilities
434458
- **cpp-httplib**: HTTP client library with OpenSSL and Brotli support
435459
- **openssl**: Cryptographic library
436460

cmake/CPM.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: MIT
2+
#
3+
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors
4+
5+
set(CPM_DOWNLOAD_VERSION 0.42.0)
6+
set(CPM_HASH_SUM "2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a")
7+
8+
if(CPM_SOURCE_CACHE)
9+
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
10+
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
11+
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
12+
else()
13+
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
14+
endif()
15+
16+
# Expand relative path. This is important if the provided path contains a tilde (~)
17+
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
18+
19+
file(DOWNLOAD
20+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
21+
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
22+
)
23+
24+
include(${CPM_DOWNLOAD_LOCATION})

cmake/ai-sdk-cpp-config.cmake.in

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44

55
set(AI_SDK_VERSION "@PROJECT_VERSION@")
66

7-
# Find dependencies
8-
find_dependency(fmt CONFIG REQUIRED)
7+
# Find PUBLIC dependencies only
98
find_dependency(nlohmann_json CONFIG REQUIRED)
10-
find_dependency(spdlog CONFIG REQUIRED)
11-
find_dependency(httplib CONFIG REQUIRED)
12-
find_dependency(OpenSSL REQUIRED)
13-
find_dependency(unofficial-concurrentqueue CONFIG REQUIRED)
149

1510
# Include the targets file
1611
include("${CMAKE_CURRENT_LIST_DIR}/ai-sdk-cpp-targets.cmake")

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Helper function to create examples
44
function(add_ai_example name source_file)
55
add_executable(${name} ${source_file})
6-
target_link_libraries(${name} PRIVATE ai::sdk spdlog::spdlog)
6+
target_link_libraries(${name} PRIVATE ai::sdk)
77

88
# Set output directory
99
set_target_properties(${name} PROPERTIES

examples/components/all/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
add_executable(all_components_demo main.cpp)
44
target_link_libraries(all_components_demo PRIVATE
55
ai::sdk # This includes all components
6-
spdlog::spdlog
76
)
87

98
set_target_properties(all_components_demo PROPERTIES

examples/components/anthropic/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
add_executable(anthropic_component_demo main.cpp)
44
target_link_libraries(anthropic_component_demo PRIVATE
55
ai::core
6-
ai::anthropic
7-
spdlog::spdlog
6+
ai::anthropic
87
)
98

109
set_target_properties(anthropic_component_demo PROPERTIES

examples/components/openai/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
add_executable(openai_component_demo main.cpp)
44
target_link_libraries(openai_component_demo PRIVATE
55
ai::core
6-
ai::openai
7-
spdlog::spdlog
6+
ai::openai
87
)
98

109
set_target_properties(openai_component_demo PROPERTIES

examples/test_anthropic.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
#include "ai/logger.h"
2+
13
#include <iostream>
24

35
#include <ai/ai.h>
4-
#include <spdlog/common.h>
5-
#include <spdlog/spdlog.h>
66

77
int main() {
88
try {
99
// Enable debug logging
10-
spdlog::set_level(spdlog::level::info);
10+
ai::logger::install_logger(std::make_shared<ai::logger::ConsoleLogger>(
11+
ai::logger::LogLevel::kLogLevelInfo));
1112

1213
// Create Anthropic client
1314
auto client = ai::anthropic::create_client();

examples/test_openai.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
#include "ai/logger.h"
2+
13
#include <iostream>
24

35
#include <ai/ai.h>
4-
#include <spdlog/common.h>
5-
#include <spdlog/spdlog.h>
66

77
int main() {
88
try {
99
// Enable debug logging
10-
spdlog::set_level(spdlog::level::info);
10+
ai::logger::install_logger(std::make_shared<ai::logger::ConsoleLogger>(
11+
ai::logger::LogLevel::kLogLevelInfo));
1112

1213
// Create OpenAI client
1314
auto client = ai::openai::create_client();

0 commit comments

Comments
 (0)