Logging
APIMatic's SDKs enable developers to conveniently log SDK activities using their preferred loggers. This capability is a vital tool for debugging and optimizing SDK performance in both production and development environments.
Key Benefits
- Enhanced Debugging: Easily track API calls, responses, and errors within the SDK, simplifying the process of identifying and resolving issues.
- Comprehensive Monitoring: Monitor SDK performance and behavior through detailed logs, ensuring smoother integration and maintenance.
- Cross-Language Consistency: Consistent logging support across PHP, Ruby, C#, and Python ensures a unified experience for developers working in multiple ecosystems.
- Customizable Logging Levels: Control the granularity of logs to suit development, testing, or production environments.
- Sensitive Data Masking: Automatically masks sensitive headers in logs, ensuring secure handling of confidential information.
How to Configure Logging
Enable Logging:
- Generate an SDK with the
EnableLogging
CodeGen Setting set totrue
.
- Generate an SDK with the
Specify Log Levels:
- Choose the desired log level (for example,
DEBUG
,INFO
,ERROR
). - Update the SDK configuration to include the selected log level.
- Choose the desired log level (for example,
Configure logging:
Request Logging Options
- Log Body: Controls the logging of the request body.
- Log Headers: Controls the logging of request headers.
- Exclude Headers: Excludes specified headers from the log output.
- Include Headers: Includes only specified headers in the log output.
- Unmask Headers: Logs specified headers without masking, revealing their actual values.
- Include Query in Path: Determines whether to include query parameters in the logged request path.
Response Logging Options
- Log Body: Controls the logging of the response body.
- Log Headers: Controls the logging of response headers.
- Exclude Headers: Excludes specified headers from the log output.
- Include Headers: Includes only specified headers in the log output.
- Unmask Headers: Logs specified headers without masking, revealing their actual values.
- TypeScript
- Java
- Python
- .NET
- Go
- PHP
- Ruby
const client = new SDKClient({
logging: {
logLevel: LogLevel.Debug,
maskSensitiveHeaders: true,
logRequest: {
logBody: true,
logHeaders: true,
includeQueryInPath: true,
headersToInclude: ["Content-Type", "Content-Encoding"]
},
logResponse: {
logHeaders: true,
headersToExclude: ["X-Powered-By"]
}
}
});
SDKClient client = new SDKClient.Builder()
.loggingConfig(builder -> builder
.level(Level.DEBUG)
.maskSensitiveHeaders(true)
.requestConfig(reqConfig -> reqConfig
.body(true)
.headers(true)
.includeQueryInPath(true)
.includeHeaders("Content-Type", "Content-Encoding"))
.responseConfig(resConfig -> resConfig
.headers(true)
.excludeHeaders("X-Powered-By")))
.build();
client = SDKClient(
logging_configuration=LoggingConfiguration(
log_level=logging.INFO,
mask_sensitive_headers=True,
request_logging_config=RequestLoggingConfiguration(
log_body=True,
log_headers=True,
include_query_in_path=True,
headers_to_include=['Content-Type', 'Content-Encoding']
),
response_logging_config=ResponseLoggingConfiguration(
log_headers=True,
headers_to_exclude=['X-Powered-By']
)
)
)
SdkClient client = new SdkClient.Builder()
.LoggingConfig(config => config
.LogLevel(LogLevel.Information)
.MaskSensitiveHeaders(true)
.RequestConfig(reqConfig => reqConfig
.Body(true)
.Headers(true)
.IncludeQueryInPath(true)
.IncludeHeaders("Content-Type", "Content-Encoding"))
.ResponseConfig(respConfig => respConfig
.Headers(true)
.ExcludeHeaders("X-Powered-By"))
)
.Build();
config := CreateConfigurationFromEnvironment(
WithLoggerConfiguration(
WithLevel("info"),
WithMaskSensitiveHeaders(true),
WithRequestConfiguration(
WithRequestBody(true),
WithRequestHeaders(true),
WithIncludeQueryInPath(true),
WithIncludeRequestHeaders("Content-Type", "Content-Encoding"),
),
WithResponseConfiguration(
WithResponseHeaders(true),
WithExcludeResponseHeaders("X-Powered-By"),
),
),
)
client := NewClient(config)
$client = SdkClientBuilder::init()
->loggingConfiguration(
LoggingConfigurationBuilder::init()
->level(LogLevel::INFO)
->maskSensitiveHeaders(true)
->requestConfiguration(
RequestLoggingConfigurationBuilder::init()
->body(true)
->headers(true)
->includeQueryInPath(true)
->includeHeaders('Content-Type', 'Content-Encoding')
)
->responseConfiguration(
ResponseLoggingConfigurationBuilder::init()
->headers(true)
->excludeHeaders('X-Powered-By')
)
)
->build();
client = SDKClient.new(
logging_configuration: LoggingConfiguration.new(
log_level: Logger::INFO,
mask_sensitive_headers: true,
request_logging_config: RequestLoggingConfiguration.new(
log_body: true,
log_headers: true,
include_query_in_path: true,
headers_to_include: %w[Content-Type Content-Encoding]
),
response_logging_config: ResponseLoggingConfiguration.new(
log_headers: true,
headers_to_exclude: ['X-Powered-By']
)
)
)
Use Cases
- Error Diagnosis: Quickly identify and troubleshoot errors in API calls.
- Performance Monitoring: Analyze response times and performance metrics for API operations.
- Compliance Tracking: Maintain logs for audit trails and compliance purposes.
- Development Insights: Gain detailed insights into SDK usage during development.