e2.0.0 Init and config pattern changed, builder added, docs updated, examples removed (deprecated).
This commit is contained in:
@ -1,18 +1,7 @@
|
||||
# Getting Started
|
||||
|
||||
[← Back to README](../README.md) | [Configuration →](configuration.md)
|
||||
|
||||
This guide will help you get started with the lixenwraith/log package, from installation through basic usage.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Installation](#installation)
|
||||
- [Basic Usage](#basic-usage)
|
||||
- [Initialization Methods](#initialization-methods)
|
||||
- [Your First Logger](#your-first-logger)
|
||||
- [Console Output](#console-output)
|
||||
- [Next Steps](#next-steps)
|
||||
|
||||
## Installation
|
||||
|
||||
Install the logger package:
|
||||
@ -39,140 +28,22 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a new logger instance
|
||||
// Create a new logger instance with default configuration
|
||||
// Writes to file ./logs/log.log
|
||||
logger := log.NewLogger()
|
||||
|
||||
// Initialize with defaults
|
||||
err := logger.InitWithDefaults()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer logger.Shutdown()
|
||||
|
||||
|
||||
// Start logging!
|
||||
logger.Info("Application started")
|
||||
logger.Debug("Debug mode enabled", "verbose", true)
|
||||
}
|
||||
```
|
||||
|
||||
## Initialization Methods
|
||||
|
||||
The logger provides two initialization methods:
|
||||
|
||||
### 1. Simple Initialization (Recommended for most cases)
|
||||
|
||||
Use `InitWithDefaults` with optional string overrides:
|
||||
|
||||
```go
|
||||
logger := log.NewLogger()
|
||||
err := logger.InitWithDefaults(
|
||||
"directory=/var/log/myapp",
|
||||
"level=-4", // Debug level
|
||||
"format=json",
|
||||
)
|
||||
```
|
||||
|
||||
### 2. Configuration-Based Initialization
|
||||
|
||||
For complex applications with centralized configuration:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/lixenwraith/config"
|
||||
"github.com/lixenwraith/log"
|
||||
)
|
||||
|
||||
// Load configuration
|
||||
cfg := config.New()
|
||||
cfg.Load("app.toml", os.Args[1:])
|
||||
|
||||
// Initialize logger with config
|
||||
logger := log.NewLogger()
|
||||
err := logger.Init(cfg, "logging") // Uses [logging] section in config
|
||||
```
|
||||
|
||||
## Your First Logger
|
||||
|
||||
Here's a complete example demonstrating basic logging features:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/lixenwraith/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create logger
|
||||
logger := log.NewLogger()
|
||||
|
||||
// Initialize with custom settings
|
||||
err := logger.InitWithDefaults(
|
||||
"directory=./logs", // Log directory
|
||||
"name=myapp", // Log file prefix
|
||||
"level=0", // Info level and above
|
||||
"format=txt", // Human-readable format
|
||||
"max_size_mb=10", // Rotate at 10MB
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to initialize logger: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Always shut down gracefully
|
||||
defer func() {
|
||||
if err := logger.Shutdown(2 * time.Second); err != nil {
|
||||
fmt.Printf("Logger shutdown error: %v\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Log at different levels
|
||||
logger.Debug("This won't appear (below Info level)")
|
||||
logger.Info("Application started", "pid", 12345)
|
||||
logger.Warn("Resource usage high", "cpu", 85.5)
|
||||
logger.Error("Failed to connect", "host", "db.example.com", "port", 5432)
|
||||
|
||||
// Structured logging with key-value pairs
|
||||
logger.Info("User action",
|
||||
"user_id", 42,
|
||||
"action", "login",
|
||||
"ip", "192.168.1.100",
|
||||
"timestamp", time.Now(),
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## Console Output
|
||||
|
||||
For development or container environments, you might want console output:
|
||||
|
||||
```go
|
||||
// Console-only logging (no files)
|
||||
logger.InitWithDefaults(
|
||||
"enable_stdout=true",
|
||||
"disable_file=true",
|
||||
"level=-4", // Debug level
|
||||
)
|
||||
|
||||
// Dual output (both file and console)
|
||||
logger.InitWithDefaults(
|
||||
"directory=/var/log/app",
|
||||
"enable_stdout=true",
|
||||
"stdout_target=stderr", // Keep stdout clean
|
||||
)
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
Now that you have a working logger:
|
||||
|
||||
1. **[Learn about configuration options](configuration.md)** - Customize behavior for your needs
|
||||
2. **[Explore the API](api-reference.md)** - See all available methods
|
||||
3. **[Understand logging best practices](logging-guide.md)** - Write better logs
|
||||
4. **[Check out examples](examples.md)** - See real-world usage patterns
|
||||
3. **[Logging patterns and examples](logging-guide.md)** - Write better logs
|
||||
|
||||
## Common Patterns
|
||||
|
||||
@ -186,7 +57,7 @@ type Service struct {
|
||||
|
||||
func NewService() (*Service, error) {
|
||||
logger := log.NewLogger()
|
||||
if err := logger.InitWithDefaults(
|
||||
if err := logger.ApplyOverride(
|
||||
"directory=/var/log/service",
|
||||
"name=service",
|
||||
"format=json",
|
||||
|
||||
Reference in New Issue
Block a user