### Create Basic Go Logger with Mulan-Ext Log Source: https://context7.com/mulan-ext/log/llms.txt Initializes a basic logger instance using Mulan-Ext Log for quick setups or development. It accepts a development mode flag and an optional service name. Logs are output to the console by default in development mode. Dependencies include the 'mulan-ext/log' and 'go.uber.org/zap' packages. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Create a development logger with console output logger, err := log.New(true, "my-service") if err != nil { panic(err) } defer logger.Sync() // Use the logger logger.Info("Application started", zap.String("version", "1.0.0"), zap.Int("port", 8080), ) logger.Debug("Debug information", zap.Any("config", map[string]string{"env": "dev"})) logger.Error("An error occurred", zap.Error(err)) } ``` -------------------------------- ### Configure File-Based Logging with Automatic Directory Creation (Go) Source: https://context7.com/mulan-ext/log/llms.txt This example shows how to configure the mulan-ext log library for file-based logging. It automatically creates directories and sets proper file permissions for specified log file paths. The 'file://' URL scheme is supported for adapter configurations. Dependencies include 'github.com/mulan-ext/log' and 'go.uber.org/zap'. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Configure file logging with automatic directory creation cfg := &log.Config{ Level: "debug", IsDev: false, Adaptors: []string{ "file:///var/log/myapp/application.log", "file:///var/log/myapp/errors.log", }, } logger, err := log.NewWithConfig(cfg, "file-logger-app") if err != nil { panic(err) } defer logger.Sync() // Logs written to both console and files logger.Info("User login successful", zap.String("user_id", "usr_789"), zap.String("ip", "192.168.1.100"), ) logger.Error("Database connection failed", zap.Error(err), zap.String("host", "db.example.com"), zap.Int("port", 5432), ) // Output format in file (JSON): // {"level":"info","ts":"2025-10-30T10:15:30","caller":"main.go:23","msg":"User login successful","service":"file-logger-app","user_id":"usr_789","ip":"192.168.1.100"} } ``` -------------------------------- ### Integrate Logger with Vector for Log Collection (Go) Source: https://context7.com/mulan-ext/log/llms.txt Demonstrates integration with the Vector data pipeline for centralized log collection and processing. The HTTP adapter connects to Vector's HTTP server source for real-time log streaming. This example assumes Vector is configured to listen on `0.0.0.0:3003/log`. This function requires the 'github.com/mulan-ext/log' and 'go.uber.org/zap' packages. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) // Vector configuration file (vector_http.toml): // [sources.my_source_http_server] // type = "http_server" // address = "0.0.0.0:3003" // encoding = "json" // method = "POST" // path = "/log" // // [sinks.my_sink_console] // type = "console" // inputs = ["my_source_http_server"] // target = "stdout" // encoding.codec = "json" func main() { // Start Vector with: vector --config vector_http.toml // Configure logger to send to Vector HTTP endpoint cfg := &log.Config{ Level: "debug", IsDev: false, Adaptors: []string{ "http://localhost:3003/log", // Vector HTTP source }, } logger, err := log.NewWithConfig(cfg, "microservice-01") if err != nil { panic(err) } defer logger.Sync() // Logs are sent to Vector for aggregation, transformation, and routing logger.Info("Service health check", zap.String("status", "healthy"), zap.Int("active_connections", 42), ) logger.Error("Service degradation detected", zap.String("component", "cache"), zap.Float64("hit_rate", 0.65), zap.Duration("avg_latency_ms", 250), ) } ``` -------------------------------- ### Create Configured Go Logger with Mulan-Ext Log Source: https://context7.com/mulan-ext/log/llms.txt Initializes a logger with comprehensive configuration options using Mulan-Ext Log, suitable for production. It allows specifying log level, multiple output adapters (like files and HTTP endpoints), and encoder settings. Dependencies include 'mulan-ext/log' and 'go.uber.org/zap'. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Configure logger with multiple outputs cfg := &log.Config{ Level: "debug", IsDev: false, // Use JSON encoder for production Adaptors: []string{ "file:///var/log/app/application.log", "http://logs.example.com:3003/log", }, } logger, err := log.NewWithConfig(cfg, "payment-service") if err != nil { panic(err) } defer logger.Sync() // Logs will be written to stdout, file, and HTTP endpoint logger.Info("Payment processed", zap.String("transaction_id", "txn_12345"), zap.Float64("amount", 99.99), zap.String("currency", "USD"), ) logger.Warn("Rate limit approaching", zap.Int("current_requests", 950), zap.Int("limit", 1000), ) // Use global logger after ReplaceGlobals zap.L().Error("Critical error", zap.Error(err)) } ``` -------------------------------- ### Integrate CLI Flags with pflag for Logging Configuration (Go) Source: https://context7.com/mulan-ext/log/llms.txt This snippet demonstrates how to integrate the mulan-ext log library with pflag for command-line configuration of logging parameters. It allows dynamic adjustment of log levels, development mode, and adaptors via CLI arguments. Dependencies include 'github.com/mulan-ext/log' and 'github.com/spf13/pflag'. ```go package main import ( "fmt" "github.com/mulan-ext/log" "github.com/spf13/pflag" "go.uber.org/zap" ) func main() { // Create config with default values cfg := &log.Config{ Level: "info", } // Add log flags to CLI fs := cfg.FlagSet() pflag.CommandLine.AddFlagSet(fs) pflag.Parse() // Apply CLI flags if pflag.Lookup("log.dev").Changed { cfg.IsDev, _ = pflag.GetBool("log.dev") } if pflag.Lookup("log.adaptors").Changed { cfg.Adaptors, _ = pflag.GetStringSlice("log.adaptors") } logger, err := log.NewWithConfig(cfg, "cli-app") if err != nil { panic(err) } defer logger.Sync() logger.Info("Application running with CLI config", zap.Bool("dev_mode", cfg.IsDev), zap.Strings("adaptors", cfg.Adaptors), ) } // Example usage: // ./app --log.dev --log.adaptors=file:///tmp/app.log,http://localhost:3003/log ``` -------------------------------- ### Unmarshall Go Logger Configuration from JSON Source: https://context7.com/mulan-ext/log/llms.txt Demonstrates how to load Mulan-Ext Log's logger configuration from a JSON string using Go's standard 'encoding/json' package. This allows external configuration of log level, output adapters, and development mode. Dependencies include 'encoding/json', 'os', 'mulan-ext/log', and 'go.uber.org/zap'. ```go package main import ( "encoding/json" "os" "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Load configuration from JSON file configFile := `{ "level": "info", "adaptors": [ "file:///app/logs/app.log", "http://logger:3003/log" ], "is_dev": false }` var cfg log.Config if err := json.Unmarshal([]byte(configFile), &cfg); err != nil { panic(err) } logger, err := log.NewWithConfig(&cfg, "api-server") if err != nil { panic(err) } defer logger.Sync() // Use structured logging with fields logger.Info("Server initialized", zap.String("host", "0.0.0.0"), zap.Int("port", 8080), zap.Strings("routes", []string{"/api/v1/users", "/api/v1/orders"}), ) } ``` -------------------------------- ### Configure Logger for Development vs. Production Modes (Go) Source: https://context7.com/mulan-ext/log/llms.txt Configures the logger encoder based on the environment. Development mode uses colored console output for readability, while production mode uses structured JSON. The IsDev flag is crucial for selecting the appropriate encoder and output format. This function requires the 'github.com/mulan-ext/log' and 'go.uber.org/zap' packages. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Development mode: human-readable console output devLogger, err := log.NewWithConfig(&log.Config{ Level: "debug", IsDev: true, }, "dev-service") if err != nil { panic(err) } devLogger.Info("Development log entry", zap.String("env", "dev")) // Output: 2025-10-30T10:15:30 INFO main.go:15 Development log entry {"service": "dev-service", "env": "dev"} // Production mode: structured JSON output prodLogger, err := log.NewWithConfig(&log.Config{ Level: "info", IsDev: false, Adaptors: []string{ "file:///var/log/app.log", "http://logs.company.com/ingest", }, }, "prod-service") if err != nil { panic(err) } prodLogger.Info("Production log entry", zap.String("env", "production"), zap.String("version", "2.1.0"), ) // Output: {"level":"info","ts":"2025-10-30T10:15:30","caller":"main.go:28","msg":"Production log entry","service":"prod-service","env":"production","version":"2.1.0"} } ``` -------------------------------- ### Configure Remote Logging via HTTP POST (Go) Source: https://context7.com/mulan-ext/log/llms.txt This snippet illustrates how to configure the mulan-ext log library to send log entries to remote HTTP endpoints using POST requests with JSON payloads. This is useful for integrating with centralized logging systems like Vector, Fluentd, or Logstash. Dependencies include 'github.com/mulan-ext/log' and 'go.uber.org/zap'. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Configure HTTP logging to Vector or other log collectors cfg := &log.Config{ Level: "info", IsDev: false, Adaptors: []string{ "http://vector.internal:3003/log", "https://logs.example.com/api/ingest", }, } logger, err := log.NewWithConfig(cfg, "distributed-app") if err != nil { panic(err) } defer logger.Sync() // Each log line is POST'd as JSON to the HTTP endpoints logger.Info("Request processed", zap.String("request_id", "req_abc123"), zap.Int("status_code", 200), zap.Duration("latency_ms", 45), ) logger.Warn("High memory usage detected", zap.Float64("memory_percent", 85.5), zap.String("hostname", "server-01"), ) // HTTP POST body format: // {"level":"info","ts":"2025-10-30T10:15:30","caller":"main.go:18","msg":"Request processed","service":"distributed-app","request_id":"req_abc123","status_code":200,"latency_ms":45} } ``` -------------------------------- ### Configure Log Levels for Filtering (Go) Source: https://context7.com/mulan-ext/log/llms.txt Controls the minimum severity level for log output, filtering messages below the configured threshold. It supports standard Zap log levels: debug, info, warn, error, dpanic, panic, and fatal. This function requires the 'github.com/mulan-ext/log' and 'go.uber.org/zap' packages. ```go package main import ( "github.com/mulan-ext/log" "go.uber.org/zap" ) func main() { // Set log level to "warn" - only warn, error, and fatal will be logged cfg := &log.Config{ Level: "warn", IsDev: false, Adaptors: []string{"file:///var/log/app.log"}, } logger, err := log.NewWithConfig(cfg, "filtered-app") if err != nil { panic(err) } defer logger.Sync() // These will NOT be logged (below warn level) logger.Debug("Debug message", zap.String("detail", "not shown")) logger.Info("Info message", zap.String("detail", "not shown")) // These WILL be logged logger.Warn("Warning message", zap.String("component", "auth"), zap.Int("retry_count", 3), ) logger.Error("Error occurred", zap.Error(err), zap.String("operation", "db_query"), ) logger.Fatal("Critical failure", zap.String("reason", "system_halt")) // Fatal will log and exit the program } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.