feat(log): Formatted console logging with file output
This commit is contained in:
10
example/app.log
Normal file
10
example/app.log
Normal file
@@ -0,0 +1,10 @@
|
||||
2025-11-10T05:00:34+01:00 INF proc.go:285 > This will be written to file go_version=go1.25.3 pid=71020
|
||||
2025-11-10T05:01:36+01:00 INF proc.go:285 > This will be written to file go_version=go1.25.3 pid=71657
|
||||
2025-11-10T05:01:59+01:00 INF proc.go:285 > This will be written to file go_version=go1.25.3 pid=72132
|
||||
2025-11-10T05:02:14+01:00 INF proc.go:285 > This will be written to file go_version=go1.25.3 pid=72401
|
||||
2025-11-10T05:05:14+01:00 INF main.go:41 > This will be written to file go_version=go1.25.3 pid=73856
|
||||
2025-11-10T05:06:02+01:00 INF main.go:41 > This will be written to file go_version=go1.25.3 pid=74313
|
||||
2025-11-10T05:07:19+01:00 INF main.go:41 > This will be written to file go_version=go1.25.3 pid=75031
|
||||
2025-11-10T05:09:48+01:00 INF main.go:41 > This will be written to file go_version=go1.25.3 pid=76511
|
||||
2025-11-10T05:15:16+01:00 INF main.go:41 > This will be written to file go_version=go1.25.3 pid=79210
|
||||
2025-11-10T05:15:26+01:00 INF main.go:41 > This will be written to file go_version=go1.25.3 pid=79455
|
||||
7
example/go.mod
Normal file
7
example/go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module example
|
||||
|
||||
go 1.25.3
|
||||
|
||||
replace git.secnex.io/secnex/masterlog => ../
|
||||
|
||||
require git.secnex.io/secnex/masterlog v0.0.0-00010101000000-000000000000
|
||||
83
example/main.go
Normal file
83
example/main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Example 1: Simple formatted logging to console (with colors if terminal)
|
||||
masterlog.Info("Hello, World!")
|
||||
|
||||
// Example 2: Logging with fields
|
||||
masterlog.Info("User logged in", map[string]interface{}{
|
||||
"user_id": 12345,
|
||||
"ip": "192.168.1.1",
|
||||
})
|
||||
|
||||
// Example 3: Different log levels (colored output)
|
||||
masterlog.Trace("trace message")
|
||||
masterlog.Debug("debug message")
|
||||
masterlog.Info("info message")
|
||||
masterlog.Warn("warning message")
|
||||
masterlog.Error("error message")
|
||||
|
||||
// Example 4: Create logger with specific log level
|
||||
traceLogger := masterlog.New(masterlog.LevelTrace)
|
||||
traceLogger.Info("Logger created with LevelTrace")
|
||||
|
||||
// Example 5: Create custom logger with JSON encoder
|
||||
jsonLogger := masterlog.New()
|
||||
jsonLogger.AddEncoder(&masterlog.JSONEncoder{})
|
||||
jsonLogger.Info("JSON formatted log", map[string]interface{}{
|
||||
"key": "value",
|
||||
})
|
||||
|
||||
// Example 6: Create logger with file writer
|
||||
fileLogger := masterlog.New()
|
||||
fileWriter, err := masterlog.NewFileWriter("app.log")
|
||||
if err == nil {
|
||||
fileLogger.AddWriter(fileWriter)
|
||||
defer fileWriter.Close()
|
||||
fileLogger.Info("This will be written to file")
|
||||
}
|
||||
|
||||
// Example 7: Logger with multiple encoders (formatted + JSON)
|
||||
multiLogger := masterlog.New()
|
||||
multiLogger.AddEncoder(&masterlog.JSONEncoder{})
|
||||
multiLogger.Info("This will be logged in both formats", map[string]interface{}{
|
||||
"key": "value",
|
||||
})
|
||||
|
||||
// Example 8: Set log level dynamically
|
||||
masterlog.SetLevel(masterlog.LevelDebug)
|
||||
masterlog.Trace("This won't be logged (level too low)")
|
||||
masterlog.Debug("This will be logged")
|
||||
|
||||
// Example 9: Create logger with custom level and colored output
|
||||
debugLogger := masterlog.New(masterlog.LevelDebug)
|
||||
debugLogger.Debug("Debug logger with colors")
|
||||
debugLogger.Trace("This won't be logged")
|
||||
|
||||
// Example 10: Pseudonymization of sensitive data
|
||||
pseudoLogger := masterlog.New()
|
||||
// Create a pseudonymizer with a secret (in production, use a secure secret from config/env)
|
||||
pseudonymizer := masterlog.NewPseudonymizerFromString("my-secret-key-change-in-production")
|
||||
pseudoLogger.SetPseudonymizer(pseudonymizer)
|
||||
|
||||
// Mark fields as sensitive
|
||||
pseudoLogger.AddSensitiveFields("user_id", "email", "ip")
|
||||
|
||||
// Log with sensitive data - it will be pseudonymized
|
||||
pseudoLogger.Info("User logged in", map[string]interface{}{
|
||||
"user_id": 12345,
|
||||
"email": "user@example.com",
|
||||
"ip": "192.168.1.1",
|
||||
"action": "login", // This field is not sensitive, so it won't be pseudonymized
|
||||
})
|
||||
|
||||
// Same user_id will produce the same pseudonymized value (deterministic)
|
||||
pseudoLogger.Info("User performed action", map[string]interface{}{
|
||||
"user_id": 12345, // Same value, same pseudonymized output
|
||||
"action": "purchase",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user