Skip to content

Commit

Permalink
Merge branch feature/logs-level-based-suppression to main
Browse files Browse the repository at this point in the history
  • Loading branch information
tukaianirban committed Jul 4, 2024
1 parent 0983efe commit 0308bdd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Each package is meant to be standalone and does not have dependencies on other p
- - All contributions must come from feature branches, named after the problem being resolved.
- - Look for constructive review of your change. Every Pull Request must be reviewed by atleast one other person for sanity, cleanliness and value.
- - If you are reviewing a PR, be kind to the developer/contributor, but not to the code.
- - All PRs when being merged to `main` will be squashed, to maintain a 'relatively' clean commit history.
- - Consider suffixing your commits with either of `impl: `, `fix: `, `chore: ` to denote the purpose behind the change.

## Feedback

Expand Down
27 changes: 27 additions & 0 deletions log/defs/defs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package defs

//
// enumerated level definitions
//
type LOG_LEVEL int

const (
LEVEL_DEBUG LOG_LEVEL = iota
LEVEL_INFO
LEVEL_WARNING
LEVEL_ERROR
LEVEL_ALERT
LEVEL_FATAL
)

//
// level-based tag definitions
//
const (
TAG_DEBUG = "D"
TAG_INFO = "I"
TAG_WARNING = "W"
TAG_ERROR = "E"
TAG_ALERT = "A"
TAG_FATAL = "F"
)
14 changes: 11 additions & 3 deletions log/logdefault/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package logdefault

import (
"fmt"

"github.com/tukaianirban/sdk.go.common/log/defs"
)

type providerdefault struct {
isTrace bool
isTrace bool
logLevel defs.LOG_LEVEL
}

func New(isTracingEnabled bool) *providerdefault {
func New(isTracingEnabled bool, logLevel defs.LOG_LEVEL) *providerdefault {

return &providerdefault{
isTrace: isTracingEnabled,
isTrace: isTracingEnabled,
logLevel: logLevel,
}
}

Expand All @@ -26,3 +30,7 @@ func (def *providerdefault) Printf(message string, args ...interface{}) {
func (def *providerdefault) IsTraceEnabled() bool {
return def.isTrace
}

func (def *providerdefault) GetLogLevel() defs.LOG_LEVEL {
return def.logLevel
}
25 changes: 11 additions & 14 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@ It exposes functions of different severity levels which are backed by a logger s
**/
package log

import "github.com/tukaianirban/sdk.go.common/log/logdefault"

//
// severity-based prefixes definition
//
const (
LEVEL_DEBUG = "D"
LEVEL_INFO = "I"
LEVEL_WARNING = "W"
LEVEL_ERROR = "E"
LEVEL_ALERT = "A"
LEVEL_FATAL = "F"
import (
"github.com/tukaianirban/sdk.go.common/log/defs"
"github.com/tukaianirban/sdk.go.common/log/logdefault"
)

//
// the global instance for calls to underlying struct
//
var loggerInstance logger

func Init(flagTrace bool) {
func Init(flagTrace bool, flags ...int) {

logLevel := defs.LEVEL_INFO
if len(flags) > 0 {
logLevel = defs.LOG_LEVEL(flags[0])
}

loggerInstance = logdefault.New(flagTrace)
loggerInstance = logdefault.New(flagTrace, logLevel)
}

/**
Interface definition that each log provider must support
**/
type logger interface {
IsTraceEnabled() bool
GetLogLevel() defs.LOG_LEVEL
Printf(message string, args ...interface{})
GetLastError() error
}
4 changes: 3 additions & 1 deletion log/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package log

import "testing"
import (
"testing"
)

func TestPrint(t *testing.T) {

Expand Down
50 changes: 38 additions & 12 deletions log/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"runtime"
"strings"
"time"

"github.com/tukaianirban/sdk.go.common/log/defs"
)

const format_datetime string = "02-01-2006 15:04:05"
Expand Down Expand Up @@ -44,56 +46,80 @@ The different logging providers only write the message to the appropriate output

func Info(message string, args ...interface{}) {

if loggerInstance.GetLogLevel() > defs.LEVEL_INFO {
return
}

if loggerInstance.IsTraceEnabled() {
loggerInstance.Printf(getLogLineTrace(message, LEVEL_INFO, args...))
loggerInstance.Printf(getLogLineTrace(message, defs.TAG_INFO, args...))
} else {
loggerInstance.Printf(getLogLine(message, LEVEL_INFO, args...))
loggerInstance.Printf(getLogLine(message, defs.TAG_INFO, args...))
}
}

func Warning(message string, args ...interface{}) {

if loggerInstance.GetLogLevel() > defs.LEVEL_WARNING {
return
}

if loggerInstance.IsTraceEnabled() {
loggerInstance.Printf(getLogLineTrace(message, LEVEL_WARNING, args...))
loggerInstance.Printf(getLogLineTrace(message, defs.TAG_WARNING, args...))
} else {
loggerInstance.Printf(getLogLine(message, LEVEL_WARNING, args...))
loggerInstance.Printf(getLogLine(message, defs.TAG_WARNING, args...))
}
}

func Error(message string, args ...interface{}) {

if loggerInstance.GetLogLevel() > defs.LEVEL_ERROR {
return
}

if loggerInstance.IsTraceEnabled() {
loggerInstance.Printf(getLogLineTrace(message, LEVEL_ERROR, args...))
loggerInstance.Printf(getLogLineTrace(message, defs.TAG_ERROR, args...))
} else {
loggerInstance.Printf(getLogLine(message, LEVEL_ERROR, args...))
loggerInstance.Printf(getLogLine(message, defs.TAG_ERROR, args...))
}
}

func Alert(message string, args ...interface{}) {

if loggerInstance.GetLogLevel() > defs.LEVEL_ALERT {
return
}

if loggerInstance.IsTraceEnabled() {
loggerInstance.Printf(getLogLineTrace(message, LEVEL_ALERT, args...))
loggerInstance.Printf(getLogLineTrace(message, defs.TAG_ALERT, args...))
} else {
loggerInstance.Printf(getLogLine(message, LEVEL_ALERT, args...))
loggerInstance.Printf(getLogLine(message, defs.TAG_ALERT, args...))
}
}

func Fatal(message string, args ...interface{}) {

if loggerInstance.GetLogLevel() > defs.LEVEL_FATAL {
return
}

if loggerInstance.IsTraceEnabled() {
loggerInstance.Printf(getLogLineTrace(message, LEVEL_FATAL, args...))
loggerInstance.Printf(getLogLineTrace(message, defs.TAG_FATAL, args...))
} else {
loggerInstance.Printf(getLogLine(message, LEVEL_FATAL, args...))
loggerInstance.Printf(getLogLine(message, defs.TAG_FATAL, args...))
}
os.Exit(1)
}

func Debug(message string, args ...interface{}) {

if loggerInstance.GetLogLevel() > defs.LEVEL_DEBUG {
return
}

if loggerInstance.IsTraceEnabled() {
loggerInstance.Printf(getLogLineTrace(message, LEVEL_DEBUG, args...))
loggerInstance.Printf(getLogLineTrace(message, defs.TAG_DEBUG, args...))
} else {
loggerInstance.Printf(getLogLine(message, LEVEL_DEBUG, args...))
loggerInstance.Printf(getLogLine(message, defs.TAG_DEBUG, args...))
}
}

Expand Down

0 comments on commit 0308bdd

Please sign in to comment.