/

Logger SDK Configuration

Configuration overview

ByteHide Logger for Go is configured through package-level functions, providing a simple and flexible way to set up logging behavior, file management, security, and performance optimization.

Configuration Overview

The Go SDK uses package-level functions for configuration. Here's how to set up basic configuration:

Go
import logs "github.com/bytehide/bytehide-logs-go"

// Set your project token
logs.SetProjectToken("your-project-token")

// Configure logging behavior
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)

Project Token Configuration

By simply adding your project token, all logs will be automatically sent to the ByteHide cloud platform without any additional configuration. If you don't set a project token, logs will only be stored in local files. For more information about obtaining and configuring your project token, see Project Token setup.

Configuration Functions

FunctionDescriptionDefault
SetProjectToken(string)Set project token manually""
SetProject()Load token from environment variable-
SetConsoleEnabled(bool)Enable/disable console outputfalse
SetMinimumLevel(LogLevel)Minimum log level to processLogLevelWarn
SetPersist(bool)Enable/disable file persistencefalse
SetFilePath(string)Log file path"logs.txt"
SetRollingInterval(RollingInterval)File rotation intervalRollingIntervalDay
AddSensitiveKey(string)Add sensitive data key to mask-
SetDuplicateSuppressionWindow(int64)Duplicate suppression window (ms)1000
SetUser(*AuthUser)Set authenticated usernil
SetAnonymousID(string)Set anonymous user ID""
AddMetaContext(string, interface{})Add global context metadata-
Flush()Flush pending logs-
Shutdown()Shutdown logger-
Disable()Disable all logging-
Enable()Enable logging after disable-

File Persistence Settings

Control how logs are saved to files:

Go
// Enable file logging
logs.SetPersist(true)
logs.SetFilePath("logs/application.log")

// File rotation settings
logs.SetRollingInterval(logs.RollingIntervalDay)

Available Rolling Intervals:

  • logs.RollingIntervalMinute: Every minute
  • logs.RollingIntervalHour: Every hour
  • logs.RollingIntervalDay: Daily (recommended)
  • logs.RollingIntervalWeek: Weekly
  • logs.RollingIntervalMonth: Monthly
  • logs.RollingIntervalYear: Yearly

Console Output Settings

Configure console logging:

Go
logs.SetConsoleEnabled(true)  // Default: false

Log Level Configuration

Set the minimum log level to process:

Go
logs.SetMinimumLevel(logs.LogLevelInfo)  // Default: LogLevelWarn

Available Log Levels (in order of severity):

  • logs.LogLevelTrace: Most detailed diagnostic information
  • logs.LogLevelDebug: Detailed diagnostic information
  • logs.LogLevelInfo: General information messages
  • logs.LogLevelWarn: Warning messages for potential issues
  • logs.LogLevelError: Error messages for failures
  • logs.LogLevelCritical: Critical errors that may cause application termination

Security Settings

Configure data masking for sensitive information:

Go
// Add sensitive keys to mask in logs
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")
logs.AddSensitiveKey("apiKey")
logs.AddSensitiveKey("creditCard")

Performance Settings

Configure duplicate suppression:

Go
// Suppress duplicate messages within time window (milliseconds)
logs.SetDuplicateSuppressionWindow(5000)  // 5 seconds

Configuration Examples

Development Environment

Go
logs.SetProjectToken("your-dev-token")
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelDebug)

// File logging
logs.SetPersist(true)
logs.SetFilePath("logs/debug.log")
logs.SetRollingInterval(logs.RollingIntervalHour)

// Basic security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")

Production Environment

Go
logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
logs.SetConsoleEnabled(false)
logs.SetMinimumLevel(logs.LogLevelWarn)

// Robust file logging
logs.SetPersist(true)
logs.SetFilePath("logs/production.log")
logs.SetRollingInterval(logs.RollingIntervalDay)

// Security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")
logs.AddSensitiveKey("apiKey")
logs.AddSensitiveKey("creditCard")
logs.AddSensitiveKey("secret")

// Performance
logs.SetDuplicateSuppressionWindow(10000)

Staging Environment

Go
logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
logs.SetConsoleEnabled(true)
logs.SetMinimumLevel(logs.LogLevelInfo)

// File logging
logs.SetPersist(true)
logs.SetFilePath("logs/staging.log")
logs.SetRollingInterval(logs.RollingIntervalDay)

// Moderate security
logs.AddSensitiveKey("password")
logs.AddSensitiveKey("token")

Environment-Based Configuration

Configure logging based on your application environment:

Go
import (
    "os"
    logs "github.com/bytehide/bytehide-logs-go"
)

func initializeLogger() {
    environment := os.Getenv("APP_ENVIRONMENT")
    if environment == "" {
        environment = "development"
    }

    // Always set project token from environment
    logs.SetProject()

    switch environment {
    case "production":
        logs.SetConsoleEnabled(false)
        logs.SetMinimumLevel(logs.LogLevelWarn)
        logs.SetDuplicateSuppressionWindow(10000)
    case "staging":
        logs.SetConsoleEnabled(true)
        logs.SetMinimumLevel(logs.LogLevelInfo)
    case "development":
        logs.SetConsoleEnabled(true)
        logs.SetMinimumLevel(logs.LogLevelDebug)
    }

    logs.SetPersist(true)
    logs.SetFilePath("logs/" + environment + "/app.log")
    logs.SetRollingInterval(logs.RollingIntervalDay)
}

Global Metadata Context

Add metadata to all logs:

Go
logs.AddMetaContext("service", "user-service")
logs.AddMetaContext("version", "1.0.0")
logs.AddMetaContext("environment", "production")

// All logs will include this metadata
logs.Info("User created")

User Identification

Set authenticated user information:

Go
user := &logs.AuthUser{
    ID:    "user-123",
    Email: "john.doe@company.com",
    Token: "token-xyz",
}
logs.SetUser(user)

// Or set anonymous ID
logs.SetAnonymousID("anon-456")

Next Steps

Previous
Installation