feat: add logging and support for custom logger (#36)

* feat: add support for logging

* fix linting

* chore: remove old APIError in favor of ServerError
This commit is contained in:
Mateusz Filipowicz
2025-02-23 12:59:46 +01:00
committed by GitHub
parent 95a4ff87ea
commit e79dcb13f0
13 changed files with 260 additions and 63 deletions

View File

@@ -94,8 +94,76 @@ if err != nil {
## Debugging and Logging
For troubleshooting, it might be useful to enable verbose logging. You can implement an interceptor to log additional
details like headers, body content, and timings. This can be enabled conditionally in your application's debug mode.
The SDK provides flexible logging capabilities through the `Logger` interface. You can either use the default logger or implement your own custom logger.
### Using the Default Logger
The SDK includes a default logger based on [logrus](https://github.com/sirupsen/logrus). You can configure it with different logging levels:
```go
// Configure client with default logger at Debug level
config := &unifi.ClientConfig{
URL: "https://unifi.localdomain",
APIKey: "your-api-key",
Logger: unifi.NewDefaultLogger(unifi.DebugLevel),
}
client, err := unifi.NewClient(config)
```
Available logging levels are:
- `unifi.DisabledLevel` - no logging
- `unifi.TraceLevel` - most verbose level
- `unifi.DebugLevel` - debug information
- `unifi.InfoLevel` - default level, informational messages
- `unifi.WarnLevel` - warning messages
- `unifi.ErrorLevel` - error messages only
Then `Logger` methods are available to be used within the client:
```go
client.Logger.Trace("Trace message")
client.Logger.Tracef("Trace message with %s", "formatting")
client.Logger.Debug("Debug message")
client.Logger.Debugf("Debug message with %s", "formatting")
client.Logger.Info("Info message")
client.Logger.Infof("Info message with %s", "formatting")
client.Logger.Warn("Warn message")
client.Logger.Warnf("Warn message with %s", "formatting")
client.Logger.Error("Error message")
client.Logger.Errorf("Error message with %s", "formatting")
```
### Custom Logger Implementation
You can implement your own logger by implementing the `Logger` interface:
```go
type MyCustomLogger struct {
// your logger fields
}
// Implement all required methods
func (l *MyCustomLogger) Trace(msg string) { /* implementation */ }
func (l *MyCustomLogger) Debug(msg string) { /* implementation */ }
func (l *MyCustomLogger) Info(msg string) { /* implementation */ }
func (l *MyCustomLogger) Error(msg string) { /* implementation */ }
func (l *MyCustomLogger) Warn(msg string) { /* implementation */ }
func (l *MyCustomLogger) Tracef(format string, args ...interface{}) { /* implementation */ }
func (l *MyCustomLogger) Debugf(format string, args ...interface{}) { /* implementation */ }
func (l *MyCustomLogger) Infof(format string, args ...interface{}) { /* implementation */ }
func (l *MyCustomLogger) Errorf(format string, args ...interface{}) { /* implementation */ }
func (l *MyCustomLogger) Warnf(format string, args ...interface{}) { /* implementation */ }
// Use custom logger in client configuration
config := &unifi.ClientConfig{
URL: "https://unifi.localdomain",
APIKey: "your-api-key",
Logger: &MyCustomLogger{},
}
client, err := unifi.NewClient(config)
```
If no logger is specified in the configuration, the SDK will use the default logger with `Info` level.
## Advanced Error Handling
@@ -143,4 +211,4 @@ For more details on contributing, see the [Contributing Guidelines](https://gith
---
This document is intended for advanced users who need deeper control and customization over the UniFi client.
For most users, the basic configuration and usage examples should suffice.
For most users, the basic configuration and usage examples should suffice.

View File

@@ -71,6 +71,10 @@ if err != nil {
- `HttpTransportCustomizer` for transport-level customization
- `HttpRoundTripperProvider` for complete HTTP client control
4. **Removed unifi.APIError**:
- Old: `unifi.APIError` struct for API errors
- New: Standard `unifi.ServerError` struct for API errors
5. **Additional Features in filipowm/go-unifi**:
- Validation modes (Soft, Hard, Disabled)
- Request/Response interceptors
@@ -90,6 +94,7 @@ if err != nil {
})
```
4. Remove explicit `Login()` calls as they are now handled automatically, unless you use [bare client initialization](./getting_started.md#BareClientInitialization)
5. Replace usage of `unifi.APIError` with `unifi.ServerError`
The rest of your code using the client methods should continue to work as before, as the API methods remain the same.