Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::integrated::adapters::logger_adapter Class Reference

Unified logging adapter for database operations. More...

#include <logger_adapter.h>

Collaboration diagram for database::integrated::adapters::logger_adapter:
Collaboration graph

Public Member Functions

 logger_adapter (const db_logger_config &config, logger_backend_type backend_type=logger_backend_type::auto_select)
 Construct logger adapter with configuration.
 
 ~logger_adapter ()
 Destructor - ensures proper shutdown.
 
 logger_adapter (const logger_adapter &)=delete
 
logger_adapteroperator= (const logger_adapter &)=delete
 
 logger_adapter (logger_adapter &&) noexcept
 
logger_adapteroperator= (logger_adapter &&)=delete
 
common::VoidResult initialize ()
 Initialize the logger.
 
common::VoidResult shutdown ()
 Shutdown the logger gracefully.
 
bool is_initialized () const
 Check if logger is initialized.
 
void log_query (db_log_level level, const std::string &query, std::chrono::microseconds duration)
 Log a SQL query execution.
 
void log_slow_query (const std::string &query, std::chrono::microseconds duration, std::chrono::milliseconds threshold)
 Log a slow query with warning.
 
void log_connection_event (const std::string &event, const std::string &details)
 Log a connection pool event.
 
void log_transaction (const std::string &operation, bool success, const std::string &details)
 Log a transaction operation.
 
void log_pool_event (const std::string &event, std::size_t active, std::size_t idle)
 Log a connection pool state change.
 
void log_error (const std::string &operation, const std::string &error_msg, const std::string &sql_state="")
 Log a database error.
 
void log (db_log_level level, const std::string &message)
 Generic log message.
 
void flush ()
 Flush pending log messages.
 

Static Private Member Functions

static std::unique_ptr< backends::logger_backendcreate_backend (const db_logger_config &config, logger_backend_type backend_type)
 Create appropriate backend based on type.
 

Private Attributes

const db_logger_configconfig_
 
std::unique_ptr< backends::logger_backendbackend_
 Logger backend implementation.
 

Detailed Description

Unified logging adapter for database operations.

Provides a consistent logging interface with runtime backend selection. No longer uses conditional compilation - backend is selected at runtime.

Thread Safety: All public methods are thread-safe.

Examples
/home/runner/work/database_system/database_system/database/integrated/core/database_coordinator.h.

Definition at line 108 of file logger_adapter.h.

Constructor & Destructor Documentation

◆ logger_adapter() [1/3]

database::integrated::adapters::logger_adapter::logger_adapter ( const db_logger_config & config,
logger_backend_type backend_type = logger_backend_type::auto_select )
explicit

Construct logger adapter with configuration.

Parameters
configLogger configuration settings
backend_typeBackend type to use (default: auto_select)
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 129 of file logger_adapter.cpp.

132 : config_(config)
134{
135 // Backend is created and potentially initialized (if auto_select)
136 // If auto_select didn't initialize, caller must call initialize()
138 {
139 // For explicit backend selection, don't auto-initialize
140 // Caller must call initialize() explicitly
141 }
142}
static std::unique_ptr< backends::logger_backend > create_backend(const db_logger_config &config, logger_backend_type backend_type)
Create appropriate backend based on type.
std::unique_ptr< backends::logger_backend > backend_
Logger backend implementation.
@ auto_select
Automatically select best available backend.
backend_type
Database backend type enumeration.

References database::integrated::adapters::auto_select.

◆ ~logger_adapter()

database::integrated::adapters::logger_adapter::~logger_adapter ( )

Destructor - ensures proper shutdown.

Automatically calls shutdown() if still initialized.

Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 144 of file logger_adapter.cpp.

145{
146 if (backend_ && backend_->is_initialized())
147 {
148 backend_->shutdown();
149 }
150}

References backend_.

◆ logger_adapter() [2/3]

database::integrated::adapters::logger_adapter::logger_adapter ( const logger_adapter & )
delete

◆ logger_adapter() [3/3]

database::integrated::adapters::logger_adapter::logger_adapter ( logger_adapter && )
defaultnoexcept

Member Function Documentation

◆ create_backend()

std::unique_ptr< backends::logger_backend > database::integrated::adapters::logger_adapter::create_backend ( const db_logger_config & config,
logger_backend_type backend_type )
staticprivate

Create appropriate backend based on type.

Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 75 of file logger_adapter.cpp.

78{
79 switch (backend_type)
80 {
82 {
83#ifdef HAVE_COMMON_LOGGER_BACKEND
84 // Try common_system backend first
85 try
86 {
87 auto backend = std::make_unique<backends::common_logger_backend>(config);
88 auto init_result = backend->initialize();
89 if (init_result.is_ok())
90 {
91 return backend;
92 }
93 // If initialization failed, fall back to fallback backend
94 }
95 catch (...)
96 {
97 // If common backend construction/init failed, fall back
98 }
99#endif
100 // Fall back to fallback_logger_backend
101 return std::make_unique<backends::fallback_logger_backend>(config);
102 }
103
105 {
106#ifdef HAVE_COMMON_LOGGER_BACKEND
107 return std::make_unique<backends::common_logger_backend>(config);
108#else
109 throw std::runtime_error(
110 "common_logger_backend not available (common_system not found)");
111#endif
112 }
113
115 return std::make_unique<backends::fallback_logger_backend>(config);
116
118 return std::make_unique<backends::null_logger_backend>(config);
119
120 default:
121 return std::make_unique<backends::fallback_logger_backend>(config);
122 }
123}
@ null
No-op backend (discard all logs)
@ system
Use common_system ILogger (fails if unavailable)

References database::integrated::adapters::auto_select, database::integrated::adapters::fallback, database::integrated::adapters::null, and database::integrated::adapters::system.

◆ flush()

void database::integrated::adapters::logger_adapter::flush ( )

Flush pending log messages.

Forces all buffered log messages to be written immediately. Useful before application exit or critical sections.

Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 298 of file logger_adapter.cpp.

299{
300 if (backend_)
301 {
302 backend_->flush();
303 }
304}

References backend_.

Referenced by test_basic_logging(), test_connection_logging(), test_error_logging(), test_log_level_filtering(), test_query_logging(), test_slow_query_detection(), test_thread_safety(), and test_transaction_logging().

Here is the caller graph for this function:

◆ initialize()

common::VoidResult database::integrated::adapters::logger_adapter::initialize ( )

Initialize the logger.

Sets up output writers (console, file) and starts the logger. Must be called before any logging operations.

Returns
VoidResult::ok() on success, error on failure
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 154 of file logger_adapter.cpp.

155{
156 if (!backend_)
157 {
158 return common::VoidResult(
159 common::error_info{ -1, "Backend not created", "" });
160 }
161
162 return backend_->initialize();
163}
Result< std::monostate > VoidResult

References initialize().

Referenced by initialize(), test_basic_logging(), test_connection_logging(), test_error_logging(), test_initialization(), test_log_level_filtering(), test_query_logging(), test_slow_query_detection(), test_thread_safety(), and test_transaction_logging().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_initialized()

bool database::integrated::adapters::logger_adapter::is_initialized ( ) const

Check if logger is initialized.

Returns
true if initialized and ready to log
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 175 of file logger_adapter.cpp.

176{
177 return backend_ && backend_->is_initialized();
178}

References backend_.

Referenced by test_initialization().

Here is the caller graph for this function:

◆ log()

void database::integrated::adapters::logger_adapter::log ( db_log_level level,
const std::string & message )

Generic log message.

Parameters
levelLog level
messageMessage to log
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 290 of file logger_adapter.cpp.

291{
292 if (backend_)
293 {
294 backend_->log(level, message);
295 }
296}

References backend_.

Referenced by test_basic_logging(), test_log_level_filtering(), and test_thread_safety().

Here is the caller graph for this function:

◆ log_connection_event()

void database::integrated::adapters::logger_adapter::log_connection_event ( const std::string & event,
const std::string & details )

Log a connection pool event.

Examples: "acquired", "released", "timeout", "health_check"

Parameters
eventEvent type (e.g., "acquired", "released")
detailsAdditional details (e.g., "Pool: main_pool, Priority: high")
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 227 of file logger_adapter.cpp.

228{
229 if (!backend_)
230 {
231 return;
232 }
233
234 std::stringstream ss;
235 ss << "Connection event [" << event << "]: " << details;
236
237 backend_->log(db_log_level::debug, ss.str());
238}
@ debug
Debug information for development.

References backend_, and database::integrated::debug.

Referenced by test_connection_logging().

Here is the caller graph for this function:

◆ log_error()

void database::integrated::adapters::logger_adapter::log_error ( const std::string & operation,
const std::string & error_msg,
const std::string & sql_state = "" )

Log a database error.

Parameters
operationOperation that failed (e.g., "execute_query", "connect")
error_msgError message
sql_stateSQL state code (e.g., "08006" for connection failure)
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 272 of file logger_adapter.cpp.

274{
275 if (!backend_)
276 {
277 return;
278 }
279
280 std::stringstream ss;
281 ss << "ERROR in " << operation << ": " << error_msg;
282 if (!sql_state.empty())
283 {
284 ss << " (SQL state: " << sql_state << ")";
285 }
286
287 backend_->log(db_log_level::error, ss.str());
288}

References backend_, and database::integrated::error.

Referenced by test_error_logging().

Here is the caller graph for this function:

◆ log_pool_event()

void database::integrated::adapters::logger_adapter::log_pool_event ( const std::string & event,
std::size_t active,
std::size_t idle )

Log a connection pool state change.

Examples: Pool resize, capacity changes

Parameters
eventEvent type (e.g., "resized", "shrunk", "health_check_failed")
activeNumber of active connections
idleNumber of idle connections
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 258 of file logger_adapter.cpp.

259{
260 if (!backend_)
261 {
262 return;
263 }
264
265 std::stringstream ss;
266 ss << "Pool event [" << event << "]: active=" << active << ", idle=" << idle
267 << ", total=" << (active + idle);
268
269 backend_->log(db_log_level::info, ss.str());
270}
@ info
Informational messages (default)

References backend_, and database::integrated::info.

Referenced by test_connection_logging().

Here is the caller graph for this function:

◆ log_query()

void database::integrated::adapters::logger_adapter::log_query ( db_log_level level,
const std::string & query,
std::chrono::microseconds duration )

Log a SQL query execution.

Automatically sanitizes the query (removes passwords, truncates if long). If duration exceeds slow_query_threshold, automatically logs as slow query.

Parameters
levelLog level (typically info or debug)
querySQL query string (will be sanitized)
durationQuery execution time
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 184 of file logger_adapter.cpp.

186{
187 if (!backend_)
188 {
189 return;
190 }
191
192 // Sanitize query
193 std::string safe_query = sanitize_query(query);
194
195 // Format message
196 std::stringstream ss;
197 ss << "Query executed in " << duration.count() << "μs: " << safe_query;
198
199 backend_->log(level, ss.str());
200
201 // Check for slow query
202 auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
204 {
206 }
207}
void log_slow_query(const std::string &query, std::chrono::microseconds duration, std::chrono::milliseconds threshold)
Log a slow query with warning.
std::chrono::milliseconds slow_query_threshold
Threshold for considering a query "slow".
bool log_slow_queries
Automatically detect and log slow queries.

References backend_, config_, database::integrated::db_logger_config::log_slow_queries, log_slow_query(), and database::integrated::db_logger_config::slow_query_threshold.

Referenced by test_query_logging(), and test_slow_query_detection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_slow_query()

void database::integrated::adapters::logger_adapter::log_slow_query ( const std::string & query,
std::chrono::microseconds duration,
std::chrono::milliseconds threshold )

Log a slow query with warning.

Called automatically by log_query() when threshold exceeded, but can also be called manually.

Parameters
querySQL query string
durationActual execution time
thresholdConfigured slow query threshold
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 209 of file logger_adapter.cpp.

211{
212 if (!backend_)
213 {
214 return;
215 }
216
217 std::string safe_query = sanitize_query(query);
218
219 std::stringstream ss;
220 ss << "SLOW QUERY detected (threshold: " << threshold.count() << "ms, actual: "
221 << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()
222 << "ms): " << safe_query;
223
224 backend_->log(db_log_level::warning, ss.str());
225}

References backend_, and database::integrated::warning.

Referenced by log_query(), and test_slow_query_detection().

Here is the caller graph for this function:

◆ log_transaction()

void database::integrated::adapters::logger_adapter::log_transaction ( const std::string & operation,
bool success,
const std::string & details )

Log a transaction operation.

Parameters
operationTransaction operation (e.g., "begin", "commit", "rollback")
successWhether operation succeeded
detailsAdditional details (e.g., isolation level, error message)
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 240 of file logger_adapter.cpp.

242{
243 if (!backend_)
244 {
245 return;
246 }
247
248 std::stringstream ss;
249 ss << "Transaction " << operation << " " << (success ? "SUCCESS" : "FAILED");
250 if (!details.empty())
251 {
252 ss << ": " << details;
253 }
254
256}

References backend_, database::integrated::error, database::integrated::info, and database::success.

Referenced by test_transaction_logging().

Here is the caller graph for this function:

◆ operator=() [1/2]

logger_adapter & database::integrated::adapters::logger_adapter::operator= ( const logger_adapter & )
delete

◆ operator=() [2/2]

logger_adapter & database::integrated::adapters::logger_adapter::operator= ( logger_adapter && )
delete

◆ shutdown()

common::VoidResult database::integrated::adapters::logger_adapter::shutdown ( )

Shutdown the logger gracefully.

Flushes all pending logs and closes files. Safe to call multiple times.

Returns
VoidResult::ok() on success, error on failure
Examples
/home/runner/work/database_system/database_system/database/integrated/adapters/logger_adapter.h.

Definition at line 165 of file logger_adapter.cpp.

166{
167 if (!backend_)
168 {
169 return common::ok();
170 }
171
172 return backend_->shutdown();
173}
VoidResult ok()

References backend_, and common::ok().

Referenced by test_basic_logging(), test_connection_logging(), test_error_logging(), test_initialization(), test_log_level_filtering(), test_query_logging(), test_slow_query_detection(), test_thread_safety(), and test_transaction_logging().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ backend_

std::unique_ptr<backends::logger_backend> database::integrated::adapters::logger_adapter::backend_
private

◆ config_

const db_logger_config& database::integrated::adapters::logger_adapter::config_
private

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