/

Disable Logging Configuration

There are several ways to disable or reduce logging in ByteHide Logger, from complete shutdown to selective filtering. Choose the method that best fits your needs.

Complete Logging Disable

Use the built-in disable method to stop all logging:

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

// Initialize logger normally
logs.SetProjectToken("your-token")
logs.SetConsoleEnabled(true)

// Disable logging completely
logs.Disable()

// All logging calls will be ignored
logs.Info("This will not be logged")
logs.Error("This will also be ignored", nil)

// Re-enable logging when needed
logs.Enable()
logs.Info("This will be logged again")

Method 2: Set Minimum Level to Highest

Go
logs.SetMinimumLevel(logs.LogLevelCritical)  // Only critical messages

// These will be ignored
logs.Info("Ignored")
logs.Warn("Ignored")
logs.Error("Ignored", nil)

// Only this will be logged
logs.Critical("This will be logged")

Conditional Disabling

Environment-Based Disabling

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

environment := os.Getenv("APP_ENVIRONMENT")
isHighTraffic := strings.EqualFold(os.Getenv("HIGH_TRAFFIC_MODE"), "true")

if isHighTraffic {
    logs.Disable()
} else {
    logs.SetMinimumLevel(logs.LogLevelWarn)
}

Feature Flag Disabling

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

func initializeWithFeatureFlag(loggingEnabled bool) {
    logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
    logs.SetMinimumLevel(logs.LogLevelInfo)

    if !loggingEnabled {
        logs.Disable()
    }
}

Selective Disabling

Disable Console Only

Go
logs.SetConsoleEnabled(false)    // Disable console output
logs.SetPersist(true)            // Keep file logging
logs.SetFilePath("logs/app.log")

Disable File Logging Only

Go
logs.SetPersist(false)           // Disable file logging
logs.SetConsoleEnabled(true)     // Keep console output

Disable Cloud Logging Only

Go
// Configure local logging
logs.SetPersist(true)
logs.SetConsoleEnabled(true)

// Don't set project token - logs stay local only
// logs.SetProjectToken("token") // Not called

Performance-Based Disabling

High-Traffic Configuration

Go
logs.SetMinimumLevel(logs.LogLevelError)
logs.SetConsoleEnabled(false)
logs.SetDuplicateSuppressionWindow(300000)  // 5 minutes

Minimal Logging Configuration

Go
logs.SetMinimumLevel(logs.LogLevelCritical)
logs.SetConsoleEnabled(false)
logs.SetPersist(true)
logs.SetFilePath("logs/critical-only.log")

Runtime Disabling

Using Built-in Enable/Disable Methods

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

func disableAllLogging() {
    logs.Disable()
}

func enableLogging() {
    logs.Enable()
}

func toggleLogging(enabled bool) {
    if enabled {
        logs.Enable()
    } else {
        logs.Disable()
    }
}

Graceful Shutdown

Always flush and shut down the logger when your application exits:

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

func main() {
    // ... initialize logger ...

    // Ensure cleanup on exit
    defer func() {
        logs.Flush()
        logs.Shutdown()
    }()

    // ... your application code ...
}

Shutdown Hook for Signals

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

func setupShutdownHook() {
    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

    go func() {
        <-sigChan
        logs.Info("Application shutting down")
        logs.Flush()
        logs.Shutdown()
        os.Exit(0)
    }()
}

Environment Variable Control

Complete Disable via Environment

Bash
export BYTEHIDE_LOGGING_DISABLED=true
Go
import (
    "os"
    "strings"
    logs "github.com/bytehide/bytehide-logs-go"
)

func initializeWithEnvironmentControl() {
    loggingDisabled := os.Getenv("BYTEHIDE_LOGGING_DISABLED")

    if strings.EqualFold(loggingDisabled, "true") {
        logs.Disable()
        return
    }

    logs.SetMinimumLevel(logs.LogLevelInfo)
    logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
}

Complete Configuration Example

Go
package main

import (
    "os"
    "os/signal"
    "strings"
    "syscall"
    logs "github.com/bytehide/bytehide-logs-go"
)

func main() {
    // Initialize logger
    initializeLogger()

    // Setup graceful shutdown
    setupShutdownHook()

    // Your application code
    logs.Info("Application started")

    // Cleanup
    defer func() {
        logs.Flush()
        logs.Shutdown()
    }()
}

func initializeLogger() {
    // Check if logging is disabled
    if strings.EqualFold(os.Getenv("BYTEHIDE_LOGGING_DISABLED"), "true") {
        logs.Disable()
        return
    }

    // Check for high-traffic mode
    if strings.EqualFold(os.Getenv("HIGH_TRAFFIC_MODE"), "true") {
        logs.SetMinimumLevel(logs.LogLevelError)
        logs.SetConsoleEnabled(false)
    } else {
        logs.SetMinimumLevel(logs.LogLevelInfo)
        logs.SetConsoleEnabled(true)
    }

    logs.SetProjectToken(os.Getenv("BYTEHIDE_LOGS_TOKEN"))
    logs.SetPersist(true)
    logs.SetFilePath("logs/app.log")
}

func setupShutdownHook() {
    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

    go func() {
        <-sigChan
        logs.Info("Graceful shutdown initiated")
        logs.Flush()
        logs.Shutdown()
        os.Exit(0)
    }()
}

Best Practices

Disabling Logging Best Practices

  • Use environment variables for flexible logging control
  • Always call Flush() and Shutdown() before application exit
  • Keep critical error logging even in high-performance scenarios
  • Test logging disable/enable in your deployment pipeline
  • Document logging configuration for your operations team

Next Steps

Previous
Environment Variables