diff --git a/README.md b/README.md index f97f460..6ae5895 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,20 @@ sylog.LogError("order-processor", "Database connection timed out") When this reaches **CloudWatch** or **Datadog**, you can simply run a query like facility: "order-processor" to see the entire lifecycle of that specific service's behavior. +### Encapsulating Facility +**Example code:** +```go +package main + +import "github.com/syniol/go-logger" + +func main() { + logger := sylog.New("pay microservice") + + logger.LogInfo("mock message for alert log") + logger.LogWarning("mock warning message for alert log") +} +``` ## Performance and Benchmarks The document that tracks the performance of the logger package is published in details diff --git a/logger.go b/logger.go index ed61d34..3d2c145 100644 --- a/logger.go +++ b/logger.go @@ -39,12 +39,19 @@ const ( levelCritical logLevel = "crit" ) +type LoggerOptions struct { + Facility string + Writer io.Writer +} + type logger struct { Level logLevel `json:"level"` Facility string `json:"facility"` Message string `json:"message"` Trace []string `json:"trace"` Timestamp string `json:"timestamp"` + + loggerOptions *LoggerOptions } // locCache prevents us from calling the expensive runtime.CallersFrames repeatedly diff --git a/logger_facility.go b/logger_facility.go new file mode 100644 index 0000000..0f64064 --- /dev/null +++ b/logger_facility.go @@ -0,0 +1,59 @@ +package sylog + +type Logger interface { + LogInfo(args ...string) + LogDebug(args ...string) + LogNotice(args ...string) + LogWarning(args ...string) + LogAlert(args ...string) + LogEmergency(args ...string) + LogError(args ...string) + LogCritical(args ...string) +} + +func NewLogger(facility string, opt *LoggerOptions) Logger { + return &logger{ + Facility: facility, + loggerOptions: opt, + } +} + +// LogInfo logs messages where in output JSON key "level" is "info" +func (l *logger) LogInfo(args ...string) { + write(levelInfo, l.Facility, args) +} + +// LogDebug logs messages where in output JSON key "level" is "debug" +func (l *logger) LogDebug(args ...string) { + write(levelDebug, l.Facility, args) +} + +// LogNotice logs messages where in output JSON key "level" is "notice" +func (l *logger) LogNotice(args ...string) { + write(levelNotice, l.Facility, args) +} + +// LogWarning logs messages where in output JSON key "level" is "warn" +func (l *logger) LogWarning(args ...string) { + write(levelWarning, l.Facility, args) +} + +// LogAlert logs messages where in output JSON key "level" is "alert" +func (l *logger) LogAlert(args ...string) { + write(levelAlert, l.Facility, args) +} + +// LogEmergency logs messages where in output JSON key "level" is "emergency" +func (l *logger) LogEmergency(args ...string) { + write(levelEmergency, l.Facility, args) +} + +// LogError logs messages where in output JSON key "level" is "error" +func (l *logger) LogError(args ...string) { + write(levelError, l.Facility, args) +} + +// LogCritical logs messages where in output JSON key "level" is "crit" +func (l *logger) LogCritical(args ...string) { + write(levelCritical, l.Facility, args) +} diff --git a/logger_facility_test.go b/logger_facility_test.go new file mode 100644 index 0000000..1d72d13 --- /dev/null +++ b/logger_facility_test.go @@ -0,0 +1,9 @@ +package sylog + +import "testing" + +func TestNew(t *testing.T) { + newLogger := NewLogger("pay-service", nil) + + newLogger.LogInfo("payment has been completed successfully with ID: 2322-1321-3211") +}