Skip to content

Commit

Permalink
added args to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilliams0305 committed Nov 6, 2023
1 parent 840c07e commit 3633bd9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
19 changes: 12 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@ func main() {
logger := golog.LoggingConfiguration().
Configure(golog.Verbose, "[%l %t] %m").
WriteTo(sink1).MinimuLevel(golog.Verbose).
WriteTo(sink2).MinimuLevel(golog.Verbose).
WriteTo(sink2).MinimuLevel(golog.Information).
CreateLogger()

logger.Verbose("Verbose Message", nil)
logger.Debug("Debug Message", nil)
logger.Information("Information Message", nil)
logger.Warn("Warn Message", nil)
logger.Error("Error Message", errors.New("ERROR"), nil)
logger.Fatal("Fatal Message", errors.New("FATAL"), nil)
logger.Verbose("Verbose Message %s", "VERNON")
logger.Debug("Debug Message %s %d", "BILLY", 20)
logger.Information("Information Message %s", "IMAC")
logger.Warn("Warn Message %s %d", []interface{}{"Alice", 30})
logger.Error("Error Message", errors.New("ERROR"))
logger.Fatal("Fatal Message", errors.New("FATAL"))

}

func formatTemplate(template string, args ...interface{}) string {
return fmt.Sprintf(template, args...)
}

/***************************
*
* Mock Logger that is FAST
Expand All @@ -38,6 +42,7 @@ type FmtPrinter struct {
}

func (f FmtPrinter) WriteTo(message golog.LogEvent) error {

_, e := fmt.Println(message.RenderMessage())
return e
}
Expand Down
2 changes: 1 addition & 1 deletion logger/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type createLogger interface {

/******************************************************************************************
* Builder interface implemenations.
* All functions below pass the golog struct between each call augmenting it with addtional
* All functions below pass the golog struct between each call augmenting it with additional
* behavior. Once the builder is completed the golog struct is used to map messages to sinks.
*******************************************************************************************/

Expand Down
27 changes: 17 additions & 10 deletions logger/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ import (
// LogEvents are generated by golog.
type LogEvent struct {
// The time a log message was received.
timestamp time.Time
// The level of the log message.
level LogLevel
// The message templated used to render the displayed log message.
message string
Timestamp time.Time
// The Level of the log message.
Level LogLevel
// The Message templated used to render the displayed log Message.
Message string
// Optional properties to add to your structured logs.
// Properties can include complex objects, simply strings, any
props properties
Args []interface{}
}

// Properties to use in a structured log event.
type properties map[string]any

// Renders a message as a string.
// The format interface is used to generate a string from a log event
type FormatMessage interface {
Expand All @@ -31,5 +28,15 @@ type FormatMessage interface {
// Converts a logevent to a string to display on an output.
// TODO: Provide message template to facilitate formatting.
func (e *LogEvent) RenderMessage() string {
return fmt.Sprintf("[%s %v] %s", e.level.ToString(), e.timestamp.Format("2006-01-02T15:04:05"), e.message)

if len(e.Args) > 0 {

formattedArgs := formatTemplate(e.Message, e.Args...)
return fmt.Sprintf("[%s %v] %s", e.Level.ToString(), e.Timestamp.Format("2006-01-02T15:04:05"), formattedArgs)
}
return fmt.Sprintf("[%s %v] %s", e.Level.ToString(), e.Timestamp.Format("2006-01-02T15:04:05"), e.Message)
}

func formatTemplate(template string, args ...any) string {
return fmt.Sprintf(template, args...)
}
46 changes: 23 additions & 23 deletions logger/golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,45 +55,45 @@ type loggingSink struct {
// When a Logger method is invoked these events are passed to the sinks and rendered for display.
// The logger is Generate by the builder pattern and then used throughout your application.
type Logger interface {
Verbose(message string, props properties)
Debug(message string, props properties)
Information(message string, props properties)
Warn(message string, props properties)
Error(message string, err error, props properties)
Fatal(message string, err error, props properties)
Verbose(message string, args ...interface{})
Debug(message string, args ...interface{})
Information(message string, args ...interface{})
Warn(message string, args ...interface{})
Error(message string, err error, args ...interface{})
Fatal(message string, err error, args ...interface{})
}

func (gl *goLog) Verbose(message string, props properties) {
func (gl *goLog) Verbose(message string, args ...interface{}) {

gl.write(message, Verbose, props)
gl.write(message, Verbose, args...)
}

func (gl *goLog) Debug(message string, props properties) {
func (gl *goLog) Debug(message string, args ...interface{}) {

gl.write(message, Debug, props)
gl.write(message, Debug, args...)
}

func (gl *goLog) Information(message string, props properties) {
func (gl *goLog) Information(message string, args ...interface{}) {

gl.write(message, Information, props)
gl.write(message, Information, args...)
}

func (gl *goLog) Warn(message string, props properties) {
func (gl *goLog) Warn(message string, args ...interface{}) {

gl.write(message, Warn, props)
gl.write(message, Warn, args...)
}

func (gl *goLog) Error(message string, err error, props properties) {
func (gl *goLog) Error(message string, err error, args ...interface{}) {

gl.write(message, Error, props)
gl.write(message, Error, args...)
}

func (gl *goLog) Fatal(message string, err error, props properties) {
func (gl *goLog) Fatal(message string, err error, args ...interface{}) {

gl.write(message, Fatal, props)
gl.write(message, Fatal, args...)
}

func (gl *goLog) write(message string, level LogLevel, props properties) {
func (gl *goLog) write(message string, level LogLevel, args ...interface{}) {

resultChan := make(chan string, len(gl.sinks))
var wg sync.WaitGroup
Expand All @@ -105,10 +105,10 @@ func (gl *goLog) write(message string, level LogLevel, props properties) {

go func(sink SinkWriter) {
writeSink(sink, LogEvent{
timestamp: time.Now(),
level: level,
message: message,
props: props,
Timestamp: time.Now(),
Level: level,
Message: message,
Args: args,
}, resultChan)
defer wg.Done()
}(s.sink)
Expand Down
10 changes: 5 additions & 5 deletions logger/level.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package golog

// The LogLevel type represents a restriction put on a writers output
// The LogLevel supports a total of 6 levels.
// The LogLevel supports a total of 6 levels.
// When a log level is set to Information (2) only log message of
// Information or higher will be generated.
// Information or higher will be generated.
type LogLevel uint8

// The default level values used by the LogLevel
// type. All other values will be be defined as unknown and
// log messages will not be generated.
// log messages will not be generated.
const (
Verbose LogLevel = 0
Debug LogLevel = 1
Expand All @@ -18,8 +18,8 @@ const (
Fatal LogLevel = 5
)

// Converts the LogLevel type to a string value used in log messages.
// All unknown alues will be UNKNOWN.
// Converts the LogLevel type to a string value used in log messages.
// All unknown alues will be UNKNOWN.
func (s LogLevel) ToString() string {
switch s {
case Verbose:
Expand Down

0 comments on commit 3633bd9

Please sign in to comment.