From 23bf4030b2f74db56083065c4c65524a32d02bff Mon Sep 17 00:00:00 2001 From: Hadi T Date: Thu, 12 Feb 2026 09:11:29 +0000 Subject: [PATCH 1/3] Feat: Added `facility` embeded instantiating logger to avoid repeatition --- README.md | 14 ++++++++++ logger_facility.go | 58 +++++++++++++++++++++++++++++++++++++++++ logger_facility_test.go | 9 +++++++ 3 files changed, 81 insertions(+) create mode 100644 logger_facility.go create mode 100644 logger_facility_test.go diff --git a/README.md b/README.md index df1b4d3..b8b34f6 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,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") +} +``` #### Credits Copyright © 2023-2026 Syniol Limited. All rights Reserved. diff --git a/logger_facility.go b/logger_facility.go new file mode 100644 index 0000000..309dabf --- /dev/null +++ b/logger_facility.go @@ -0,0 +1,58 @@ +package sylog + +type Logger interface { + LogInfo(args ...interface{}) + LogDebug(args ...interface{}) + LogNotice(args ...interface{}) + LogWarning(args ...interface{}) + LogAlert(args ...interface{}) + LogEmergency(args ...interface{}) + LogError(args ...interface{}) + LogCritical(args ...interface{}) +} + +func New(facility string) Logger { + return &logger{ + Facility: facility, + } +} + +// LogInfo logs messages where in output JSON key "level" is "info" +func (l *logger) LogInfo(args ...interface{}) { + write(levelInfo, l.Facility, args) +} + +// LogDebug logs messages where in output JSON key "level" is "debug" +func (l *logger) LogDebug(args ...interface{}) { + write(levelDebug, l.Facility, args) +} + +// LogNotice logs messages where in output JSON key "level" is "notice" +func (l *logger) LogNotice(args ...interface{}) { + write(levelNotice, l.Facility, args) +} + +// LogWarning logs messages where in output JSON key "level" is "warn" +func (l *logger) LogWarning(args ...interface{}) { + write(levelWarning, l.Facility, args) +} + +// LogAlert logs messages where in output JSON key "level" is "alert" +func (l *logger) LogAlert(args ...interface{}) { + write(levelAlert, l.Facility, args) +} + +// LogEmergency logs messages where in output JSON key "level" is "emergency" +func (l *logger) LogEmergency(args ...interface{}) { + write(levelEmergency, l.Facility, args) +} + +// LogError logs messages where in output JSON key "level" is "error" +func (l *logger) LogError(args ...interface{}) { + write(levelError, l.Facility, args) +} + +// LogCritical logs messages where in output JSON key "level" is "crit" +func (l *logger) LogCritical(args ...interface{}) { + write(levelCritical, l.Facility, args) +} diff --git a/logger_facility_test.go b/logger_facility_test.go new file mode 100644 index 0000000..56bb601 --- /dev/null +++ b/logger_facility_test.go @@ -0,0 +1,9 @@ +package sylog + +import "testing" + +func TestNew(t *testing.T) { + newLogger := New("pay-service") + + newLogger.LogInfo("payment has been completed successfully with ID: 2322-1321-3211") +} From 99345143ac96ff536386d08c3a52fe846d81417b Mon Sep 17 00:00:00 2001 From: Hadi T Date: Sat, 14 Feb 2026 02:13:54 +0000 Subject: [PATCH 2/3] Updated logger facility with a new changes from main --- logger.go | 4 ++++ logger_facility.go | 37 +++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/logger.go b/logger.go index ed61d34..567971e 100644 --- a/logger.go +++ b/logger.go @@ -39,12 +39,16 @@ const ( levelCritical logLevel = "crit" ) +type LoggerOptions struct{} + 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 index 309dabf..74a40ae 100644 --- a/logger_facility.go +++ b/logger_facility.go @@ -1,58 +1,59 @@ package sylog type Logger interface { - LogInfo(args ...interface{}) - LogDebug(args ...interface{}) - LogNotice(args ...interface{}) - LogWarning(args ...interface{}) - LogAlert(args ...interface{}) - LogEmergency(args ...interface{}) - LogError(args ...interface{}) - LogCritical(args ...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 New(facility string) Logger { +func New(facility string, opt *LoggerOptions) Logger { return &logger{ - Facility: facility, + Facility: facility, + loggerOptions: opt, } } // LogInfo logs messages where in output JSON key "level" is "info" -func (l *logger) LogInfo(args ...interface{}) { +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 ...interface{}) { +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 ...interface{}) { +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 ...interface{}) { +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 ...interface{}) { +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 ...interface{}) { +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 ...interface{}) { +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 ...interface{}) { +func (l *logger) LogCritical(args ...string) { write(levelCritical, l.Facility, args) } From 4704d23ff0085602adcae84b00f90fff383211af Mon Sep 17 00:00:00 2001 From: Hadi T Date: Sat, 14 Feb 2026 02:16:47 +0000 Subject: [PATCH 3/3] Added Logger Options --- logger.go | 5 ++++- logger_facility.go | 2 +- logger_facility_test.go | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/logger.go b/logger.go index 567971e..3d2c145 100644 --- a/logger.go +++ b/logger.go @@ -39,7 +39,10 @@ const ( levelCritical logLevel = "crit" ) -type LoggerOptions struct{} +type LoggerOptions struct { + Facility string + Writer io.Writer +} type logger struct { Level logLevel `json:"level"` diff --git a/logger_facility.go b/logger_facility.go index 74a40ae..0f64064 100644 --- a/logger_facility.go +++ b/logger_facility.go @@ -11,7 +11,7 @@ type Logger interface { LogCritical(args ...string) } -func New(facility string, opt *LoggerOptions) Logger { +func NewLogger(facility string, opt *LoggerOptions) Logger { return &logger{ Facility: facility, loggerOptions: opt, diff --git a/logger_facility_test.go b/logger_facility_test.go index 56bb601..1d72d13 100644 --- a/logger_facility_test.go +++ b/logger_facility_test.go @@ -3,7 +3,7 @@ package sylog import "testing" func TestNew(t *testing.T) { - newLogger := New("pay-service") + newLogger := NewLogger("pay-service", nil) newLogger.LogInfo("payment has been completed successfully with ID: 2322-1321-3211") }