Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Production-ready Go logging package built on top of `slog` with automatic file r

- **Dual Output Streams**: Separate files for general logs and errors
- **Automatic File Rotation**: Daily rotation with configurable retention
- **Debug/Production Modes**: Easy switching between detailed and minimal logging
- **Flexible Log Levels**: Support for all slog levels (DEBUG, INFO, WARN, ERROR)
- **Structured Logging**: Built on Go's standard `slog` package
- **Thread-Safe**: Concurrent logging support with mutex protection
- **Zero Dependencies**: Uses only Go standard library
Expand Down Expand Up @@ -55,7 +55,7 @@ func main() {
config := islogger.DefaultConfig().
WithAppName("myapp").
WithLogDir("logs").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithRetentionDays(14).
WithJSONFormat(true).
WithTimeFormat("2006-01-02 15:04:05")
Expand All @@ -74,7 +74,7 @@ defer logger.Close()
|--------|---------|-------------|
| `LogDir` | `"logs"` | Directory for log files |
| `AppName` | `"app"` | Application name (used in filenames) |
| `Debug` | `false` | Enable debug level logging |
| `LogLevel` | `INFO` | Minimum log level (DEBUG, INFO, WARN, ERROR) |
| `RetentionDays` | `7` | Days to keep old log files |
| `JSONFormat` | `false` | Use JSON format instead of text |
| `AddSource` | `false` | Include source file and line info |
Expand Down Expand Up @@ -114,16 +114,18 @@ logs/

## 🎯 Usage Examples

### Debug Mode Switching
### Log Level Management

```go
// Start in production mode (only warnings and errors)
logger.SetDebug(false)
// Start with WARN level (only warnings and errors)
logger.SetLevel(slog.LevelWarn)
logger.Debug("Won't be logged")
logger.Info("Won't be logged")
logger.Warn("Will be logged")
logger.Error("Will be logged")

// Switch to debug mode
logger.SetDebug(true)
// Switch to DEBUG level (all messages)
logger.SetLevel(slog.LevelDebug)
logger.Debug("Now this will be logged")
```

Expand Down Expand Up @@ -410,7 +412,7 @@ Complete example for production environments:
config := islogger.DefaultConfig().
WithAppName("myapp").
WithLogDir("/var/log/myapp").
WithDebug(false).
WithLogLevel(slog.LevelWarn).
WithRetentionDays(30).
WithJSONFormat(true).
// Security: mask sensitive fields
Expand Down Expand Up @@ -477,7 +479,6 @@ With(args ...any) *Logger
WithContext(ctx context.Context) *Logger

// Control functions
SetDebug(debug bool) error
SetLevel(level slog.Level) error
Flush() error
Close() error
Expand All @@ -500,7 +501,6 @@ With(args ...any) *Logger
WithContext(ctx context.Context) *Logger

// Management methods
SetDebug(debug bool) error
SetLevel(level slog.Level) error
Flush() error
RotateNow() error
Expand All @@ -516,7 +516,7 @@ Close() error
config := DefaultConfig().
WithAppName("myapp").
WithLogDir("custom-logs").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithRetentionDays(30).
WithJSONFormat(true).
WithTimeFormat("2006-01-02 15:04:05").
Expand Down Expand Up @@ -551,7 +551,7 @@ RegexMaskFilter(pattern, mask string) RegexFilter

## 🎨 Log Levels

- **Debug**: Detailed information for debugging (only in debug mode)
- **Debug**: Detailed information for debugging (when LogLevel is DEBUG)
- **Info**: General information about application flow
- **Warn**: Warning messages (logged to both files)
- **Error**: Error messages (logged to both files)
Expand Down
24 changes: 12 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package iSlogger

Check failure on line 1 in config.go

View workflow job for this annotation

GitHub Actions / Lint Code

don't use MixedCaps in package name; iSlogger should be islogger

import (
"log/slog"
Expand All @@ -6,15 +6,15 @@
"time"
)

type Config struct {

Check failure on line 9 in config.go

View workflow job for this annotation

GitHub Actions / Lint Code

exported type Config should have comment or be unexported
LogDir string // Directory for log files
AppName string // Application name for log file prefix
Debug bool // Enable debug logging
RetentionDays int // Number of days to keep log files
JSONFormat bool // Use JSON format instead of text
AddSource bool // Add source file and line info
TimeFormat string // Custom time format
ConsoleOutput bool // Enable output to console (stdout/stderr)
LogDir string // Directory for log files
AppName string // Application name for log file prefix
LogLevel slog.Level // Minimum log level (DEBUG, INFO, WARN, ERROR)
RetentionDays int // Number of days to keep log files
JSONFormat bool // Use JSON format instead of text
AddSource bool // Add source file and line info
TimeFormat string // Custom time format
ConsoleOutput bool // Enable output to console (stdout/stderr)

// Buffering configuration
BufferSize int // Buffer size in bytes (0 = no buffering)
Expand All @@ -25,11 +25,11 @@
Filters FilterConfig // Filtering and conditional logging configuration
}

func DefaultConfig() Config {

Check failure on line 28 in config.go

View workflow job for this annotation

GitHub Actions / Lint Code

exported function DefaultConfig should have comment or be unexported
return Config{
LogDir: "logs",
AppName: "app",
Debug: false,
LogLevel: slog.LevelInfo, // INFO and above by default
RetentionDays: 7,
JSONFormat: false,
AddSource: false,
Expand All @@ -42,9 +42,9 @@
}
}

// WithDebug enables debug logging
func (c Config) WithDebug(debug bool) Config {
c.Debug = debug
// WithLogLevel sets the minimum log level
func (c Config) WithLogLevel(level slog.Level) Config {
c.LogLevel = level
return c
}

Expand Down
5 changes: 3 additions & 2 deletions console_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package iSlogger

Check failure on line 1 in console_test.go

View workflow job for this annotation

GitHub Actions / Lint Code

don't use MixedCaps in package name; iSlogger should be islogger

import (
"bytes"
"log/slog"
"os"
"strings"
"testing"
Expand All @@ -20,7 +21,7 @@
WithAppName("console-test").
WithLogDir("test-logs").
WithConsoleOutput(true).
WithDebug(true)
WithLogLevel(slog.LevelDebug)

logger, err := New(config)
if err != nil {
Expand Down Expand Up @@ -57,7 +58,7 @@
WithAppName("console-test-disabled").
WithLogDir("test-logs").
WithConsoleOutput(false).
WithDebug(true)
WithLogLevel(slog.LevelDebug)

logger, err := New(config)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
// Advanced configuration with JSON format and custom time
config := iSlogger.DefaultConfig().
WithAppName("advanced-app").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLogDir("advanced-logs").
WithRetentionDays(14).
WithJSONFormat(true).
Expand Down
15 changes: 8 additions & 7 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log/slog"
"time"

"github.com/sarff/iSlogger"
Expand All @@ -10,7 +11,7 @@ func main() {
// Initialize global logger with default configuration
config := iSlogger.DefaultConfig().
WithAppName("myapp").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLogDir("logs")

if err := iSlogger.Init(config); err != nil {
Expand All @@ -29,18 +30,18 @@ func main() {
userLogger.Info("User logged in", "username", "john_doe")
userLogger.Error("User action failed", "action", "purchase", "reason", "insufficient funds")

// Demonstrate debug mode switching
iSlogger.Info("Switching to production mode...")
iSlogger.SetDebug(false) // Only warnings and errors will be logged
// Demonstrate log level switching
iSlogger.Info("Switching to WARN level...")
iSlogger.SetLevel(slog.LevelWarn) // Only warnings and errors will be logged

iSlogger.Debug("This won't appear") // Won't be logged
iSlogger.Info("This won't appear") // Won't be logged
iSlogger.Warn("This will appear") // Will be logged
iSlogger.Error("This will appear") // Will be logged

// Switch back to debug mode
iSlogger.SetDebug(true)
iSlogger.Debug("Debug mode is back!")
// Switch back to debug level
iSlogger.SetLevel(slog.LevelDebug)
iSlogger.Debug("Debug level is back!")

// Structured logging example
iSlogger.Info("Request processed",
Expand Down
9 changes: 5 additions & 4 deletions examples/console/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log/slog"
"time"

"github.com/sarff/iSlogger"
Expand All @@ -10,7 +11,7 @@ func main() {
// Example 1: Default behavior (console output enabled)
config1 := iSlogger.DefaultConfig().
WithAppName("console-app").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLogDir("logs")

logger1, err := iSlogger.New(config1)
Expand All @@ -26,7 +27,7 @@ func main() {
// Example 2: Disable console output (file only)
config2 := iSlogger.DefaultConfig().
WithAppName("file-only-app").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLogDir("logs").
WithConsoleOutput(false)

Expand All @@ -43,7 +44,7 @@ func main() {
// Example 3: Explicitly enable console output
config3 := iSlogger.DefaultConfig().
WithAppName("explicit-console").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLogDir("logs").
WithConsoleOutput(true)

Expand All @@ -59,7 +60,7 @@ func main() {
// Example 4: JSON format with console output
config4 := iSlogger.DefaultConfig().
WithAppName("json-console").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLogDir("logs").
WithJSONFormat(true).
WithConsoleOutput(true)
Expand Down
8 changes: 4 additions & 4 deletions examples/filters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func basicFilteringExample() {
config := iSlogger.DefaultConfig().
WithAppName("filter-basic").
WithLogDir("logs").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
// Mask sensitive fields
WithFieldMask("password", "***").
WithFieldMask("api_key", "***HIDDEN***").
Expand Down Expand Up @@ -60,7 +60,7 @@ func conditionalLoggingExample() {
config := iSlogger.DefaultConfig().
WithAppName("filter-condition").
WithLogDir("logs").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
// Only log warnings/errors OR messages containing "important"
WithCondition(iSlogger.AnyCondition(
iSlogger.LevelCondition(slog.LevelWarn),
Expand Down Expand Up @@ -97,7 +97,7 @@ func rateLimitingExample() {
config := iSlogger.DefaultConfig().
WithAppName("filter-rate").
WithLogDir("logs").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
// Limit DEBUG messages to 5 per minute
WithRateLimit(slog.LevelDebug, 5, time.Minute).
// Limit INFO messages to 20 per minute
Expand Down Expand Up @@ -126,7 +126,7 @@ func productionFilteringExample() {
config := iSlogger.DefaultConfig().
WithAppName("filter-production").
WithLogDir("logs").
WithDebug(false). // Production mode
WithLogLevel(slog.LevelWarn). // Production mode (WARN+)
// Security: mask all sensitive fields
WithFieldMask("password", "***").
WithFieldMask("api_key", "***").
Expand Down
3 changes: 2 additions & 1 deletion examples/web-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
Expand All @@ -27,7 +28,7 @@ func main() {
// Initialize logger for web application
config := iSlogger.DefaultConfig().
WithAppName("webapp").
WithDebug(false). // Production mode
WithLogLevel(slog.LevelWarn). // Production mode
WithLogDir("web-logs").
WithJSONFormat(true) // JSON format for log aggregation

Expand Down
16 changes: 8 additions & 8 deletions filters_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package iSlogger

Check failure on line 1 in filters_test.go

View workflow job for this annotation

GitHub Actions / Lint Code

don't use MixedCaps in package name; iSlogger should be islogger

import (
"log/slog"
Expand All @@ -11,7 +11,7 @@
config := DefaultConfig().
WithAppName("test-mask").
WithLogDir("test-logs-mask").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithFieldMask("password", "***").
WithFieldMask("credit_card", "****-****-****-****")

Expand All @@ -32,7 +32,7 @@
config := DefaultConfig().
WithAppName("test-redact").
WithLogDir("test-logs-redact").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithFieldRedaction("sensitive_data")

logger, err := New(config)
Expand All @@ -51,7 +51,7 @@
config := DefaultConfig().
WithAppName("test-regex").
WithLogDir("test-logs-regex").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithRegexFilter(`\d{4}-\d{4}-\d{4}-\d{4}`, "****-****-****-****"). // Credit card pattern
WithRegexFilter(`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`, "***@***.***") // Email pattern

Expand All @@ -71,7 +71,7 @@
config := DefaultConfig().
WithAppName("test-condition").
WithLogDir("test-logs-condition").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithLevelCondition(slog.LevelWarn). // Only WARN and above
WithMessageContainsCondition("important") // OR contains "important"

Expand All @@ -95,7 +95,7 @@
config := DefaultConfig().
WithAppName("test-attr").
WithLogDir("test-logs-attr").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithAttributeCondition("user_type", "admin") // Only admin users

logger, err := New(config)
Expand All @@ -115,7 +115,7 @@
config := DefaultConfig().
WithAppName("test-time").
WithLogDir("test-logs-time").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithTimeBasedCondition(9, 17) // Only during work hours (9-17)

logger, err := New(config)
Expand All @@ -136,7 +136,7 @@
config := DefaultConfig().
WithAppName("test-rate").
WithLogDir("test-logs-rate").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithRateLimit(slog.LevelDebug, 3, time.Minute) // Max 3 DEBUG per minute

logger, err := New(config)
Expand All @@ -159,7 +159,7 @@
config := DefaultConfig().
WithAppName("test-combined").
WithLogDir("test-logs-combined").
WithDebug(true).
WithLogLevel(slog.LevelDebug).
WithFieldMask("password", "***").
WithRegexFilter(`\d{4}-\d{4}-\d{4}-\d{4}`, "****-****-****-****").
WithLevelCondition(slog.LevelInfo). // Only INFO and above
Expand Down
Loading
Loading