Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Log.Record("myObject", myObject).Infof("Another message about my object")
Log.Record("?myObject", myObject).Infof("Another message about my object, show myObject even if it is nil")
Log.Recordf("myObject", "format %s %+v". myObject.ID(), myObject).Infof("This record uses a formatted value")

log := Log.Record("dynamic", func() interface{} { return myObject.Callme() })
log := Log.Record("dynamic", func() any { return myObject.Callme() })

log.Infof("This is here")
log.Infof("That is there")
Expand Down Expand Up @@ -301,7 +301,7 @@ The duration will logged in the `msg` record after the given message. It will al
There are 3 more variations for funcs that return an error, a value, an error and a value:

```go
result := log.TimeFuncV("message shown with the duration", func() interface{} {
result := log.TimeFuncV("message shown with the duration", func() any {
log.Info("I am here")
// ... some stuff that takes time
time.Sleep(12*time.Second)
Expand All @@ -315,7 +315,7 @@ err := log.TimeFuncE("message shown with the duration", func() err {
return errors.ArgumentMissing.With("path")
})

result, err := log.TimeFuncVE("message shown with the duration", func() (interface{}, error) {
result, err := log.TimeFuncVE("message shown with the duration", func() (any, error) {
log.Info("I am here")
// ... some stuff that takes time
time.Sleep(12*time.Second)
Expand Down Expand Up @@ -360,7 +360,7 @@ type Customer {
}

// implements logger.Redactable
func (customer Customer) Redact() interface{} {
func (customer Customer) Redact() any {
return Customer{customer.Name, logger.Redact(customer.ID)}
}

Expand All @@ -383,7 +383,7 @@ type Customer {
}

// implements logger.RedactableWithKeys
func (customer Customer) Redact(keyToRedact ...string) interface{} {
func (customer Customer) Redact(keyToRedact ...string) any {
redacted := customer
for _, key := range keyToRedact {
switch key {
Expand Down Expand Up @@ -577,6 +577,17 @@ The `Logger` can be configured completely by environment variables if needed. Th
- `DEBUG`, default: none
If set to "1", this will set the default level to filter to *DEBUG*

You can also use a prefix for the environment variables. When you create a `Logger`, you can pass a prefix to the `Create` method. For example:

```go
func main() {
log := logger.Create("myapp", logger.EnvironmentPrefix("myapp_"))
log.Infof("This is a message")
}
```

The logger will look for the environment variables `myapp_LOG_DESTINATION`, `myapp_LOG_LEVEL`, etc.

## Thanks

Special thanks to [@chakrit](https://github.com/chakrit) for his [chakrit/go-bunyan](https://github.com/chakrit/go-bunyan) that inspired me. In fact earlier versions were wrappers around his library.
Expand Down
2 changes: 1 addition & 1 deletion bufferpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var bufferPool = NewBufferPool()
// NewBufferPool creates a new BufferPool
func NewBufferPool() *BufferPool {
return &BufferPool{
New: func() interface{} {
New: func() any {
return &bytes.Buffer{}
},
}
Expand Down
12 changes: 6 additions & 6 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (user User) IsNil() bool {
return user.ID == ""
}

func (user User) Redact() interface{} {
func (user User) Redact() any {
return User{user.ID, logger.Redact(user.Name), user.logger}
}

Expand All @@ -107,7 +107,7 @@ type Metadata struct {
City string `json:"city"`
}

func (metadata Metadata) Redact(keyToRedact ...string) interface{} {
func (metadata Metadata) Redact(keyToRedact ...string) any {
redacted := metadata
for _, key := range keyToRedact {
switch key {
Expand All @@ -123,7 +123,7 @@ func (metadata Metadata) Redact(keyToRedact ...string) interface{} {
}

// Load loads an object from a file and marshals it
func Load(filename string, object interface{}) (err error) {
func Load(filename string, object any) (err error) {
var payload []byte

if payload, err = os.ReadFile(filepath.Join(".", "testdata", filename)); err != nil {
Expand Down Expand Up @@ -234,7 +234,7 @@ func (suite *LoggerSuite) LogLineEqual(line string, records map[string]string) {
rex_records[key] = regexp.MustCompile(value)
}

properties := map[string]interface{}{}
properties := map[string]any{}
err := json.Unmarshal([]byte(line), &properties)
suite.Require().NoError(err, "Could not unmarshal line, error: %s", err)

Expand All @@ -253,9 +253,9 @@ func (suite *LoggerSuite) LogLineEqual(line string, records map[string]string) {
stringvalue = strconv.FormatFloat(value.(float64), 'f', -1, 64)
case fmt.Stringer:
stringvalue = actual.String()
case map[string]interface{}:
case map[string]any:
stringvalue = fmt.Sprintf("%v", value)
case []interface{}:
case []any:
stringvalue = fmt.Sprintf("%v", value)
default:
suite.Failf(fmt.Sprintf("The value of the key %s cannot be casted to string", key), "Type: %s", reflect.TypeOf(value))
Expand Down
9 changes: 7 additions & 2 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ type Converter interface {
Convert(record *Record) *Record
}

// GetConverterFromEnvironment fetches the Converter from the environment
// GetConverterFromEnvironment fetches the Converter from the LOG_CONVERTER environment
func GetConverterFromEnvironment() Converter {
switch strings.ToLower(core.GetEnvAsString("LOG_CONVERTER", "bunyan")) {
return GetConverterFromEnvironmentWithPrefix("")
}

// GetConverterFromEnvironmentWithPrefix fetches the Converter from the LOG_CONVERTER environment with a prefix
func GetConverterFromEnvironmentWithPrefix(prefix EnvironmentPrefix) Converter {
switch strings.ToLower(core.GetEnvAsString(string(prefix)+"LOG_CONVERTER", "bunyan")) {
case "bunyan", "default":
return &BunyanConverter{}
case "stackdriver", "google", "gcp":
Expand Down
2 changes: 1 addition & 1 deletion converter_stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (converter *StackDriverConverter) Convert(record *Record) *Record {
return record
}

func (converter StackDriverConverter) severity(value interface{}) logging.Severity {
func (converter StackDriverConverter) severity(value any) logging.Severity {
switch level := value.(type) {
case Level:
switch level {
Expand Down
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ More generally, Record fields can be logged like this:
Log.Record("myObject", myObject).Infof("Another message about my object")
Log.Recordf("myObject", "format %s %+v". myObject.ID(), myObject).Infof("This record uses a formatted value")

log := Log.Record("dynamic", func() interface{} { return myObject.Callme() })
log := Log.Record("dynamic", func() any { return myObject.Callme() })

log.Infof("This is here")
log.Infof("That is there")
Expand Down Expand Up @@ -268,7 +268,7 @@ For example:
}

// implements logger.Redactable
func (customer Customer) Redact() interface{} {
func (customer Customer) Redact() any {
return Customer{customer.ID, "REDACTED"}
}

Expand Down
13 changes: 11 additions & 2 deletions level_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ func NewLevelSet(defaultLevel Level) (levels LevelSet) {
//
// If the environment variable DEBUG is set to 1 (or yes, on, true), the default level in the LevelSet will be DEBUG
func ParseLevelsFromEnvironment() (levels LevelSet) {
levels = ParseLevels(core.GetEnvAsString("LOG_LEVEL", "INFO"))
if core.GetEnvAsBool("DEBUG", false) {
return ParseLevelsFromEnvironmentWithPrefix("")
}

// ParseLevelsFromEnvironmentWithPrefix parses the levels from the environment variable ${prefix}LOG_LEVEL
//
// If ${prefix}LOG_LEVEL is not set, it will return a LevelSet with the default level (INFO)
//
// If the environment variable ${prefix}DEBUG is set to 1 (or yes, on, true), the default level in the LevelSet will be DEBUG
func ParseLevelsFromEnvironmentWithPrefix(prefix EnvironmentPrefix) (levels LevelSet) {
levels = ParseLevels(core.GetEnvAsString(string(prefix)+"LOG_LEVEL", "INFO"))
if core.GetEnvAsBool(string(prefix)+"DEBUG", false) {
if levels.Get("any", "any") > DEBUG {
levels.Set(DEBUG, "any", "any")
}
Expand Down
2 changes: 1 addition & 1 deletion logger-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const contextKey key = iota + 12583
// Sources are either LoggerCarrier implemenations or Logger/*Logger objects.
//
// The first source that is a match is returned.
func FromContext(context context.Context, sources ...interface{}) (*Logger, error) {
func FromContext(context context.Context, sources ...any) (*Logger, error) {
if context != nil {
if logger, ok := context.Value(contextKey).(*Logger); ok {
return logger, nil
Expand Down
4 changes: 2 additions & 2 deletions logger-time.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (l *Logger) TimeFunc(message string, code func()) {
}

// TimeFuncV will log the duration of the given function call that returns a value
func (l *Logger) TimeFuncV(message string, code func() interface{}) interface{} {
func (l *Logger) TimeFuncV(message string, code func() any) any {
start := time.Now()
result := code()
duration := time.Since(start)
Expand All @@ -29,7 +29,7 @@ func (l *Logger) TimeFuncE(message string, code func() error) error {
}

// TimeFuncVE will log the duration of the given function call that returns a value and an error
func (l *Logger) TimeFuncVE(message string, code func() (interface{}, error)) (interface{}, error) {
func (l *Logger) TimeFuncVE(message string, code func() (any, error)) (any, error) {
start := time.Now()
result, err := code()
duration := time.Since(start)
Expand Down
Loading
Loading