-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logger: add With() method to persist log labels (#5)
* logger: add With() method to persist log labels Add `With()` method to loggers to create a sublogger with a set of persistent labels that can be used to generate consistent log messages for a given resource. * chore: add missing license headers * loggers: share interface between Event and Context
- Loading branch information
Showing
8 changed files
with
482 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package noop | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/loopholelabs/logging/types" | ||
) | ||
|
||
var _ types.Context = (*Context)(nil) | ||
|
||
type Context struct { | ||
l *Logger | ||
} | ||
|
||
func (c *Context) Logger() types.SubLogger { | ||
return c.l | ||
} | ||
|
||
func (c *Context) Str(key string, val string) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Bool(key string, val bool) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Int(key string, val int) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Int8(key string, val int8) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Int16(key string, val int16) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Int32(key string, val int32) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Int64(key string, val int64) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Uint(key string, val uint) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Uint8(key string, val uint8) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Uint16(key string, val uint16) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Uint32(key string, val uint32) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Uint64(key string, val uint64) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Float32(key string, val float32) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Float64(key string, val float64) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) IPAddr(key string, ipAddr net.IP) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) MACAddr(key string, macAddr net.HardwareAddr) types.Context { | ||
return c | ||
} | ||
|
||
func (c *Context) Err(err error) types.Context { | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package slog | ||
|
||
import ( | ||
"log/slog" | ||
"net" | ||
|
||
"github.com/loopholelabs/logging/types" | ||
) | ||
|
||
var _ types.Context = (*Context)(nil) | ||
|
||
type Context struct { | ||
l *Logger | ||
attrs []any | ||
} | ||
|
||
func (c *Context) Logger() types.SubLogger { | ||
l := New(c.l.source, c.l.level, c.l.output) | ||
l.logger = c.l.logger.With(c.attrs...) | ||
return l | ||
} | ||
|
||
func (c *Context) Str(key string, val string) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.StringValue(val), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Bool(key string, val bool) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.BoolValue(val), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Int(key string, val int) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.IntValue(val), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Int8(key string, val int8) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.IntValue(int(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Int16(key string, val int16) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.IntValue(int(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Int32(key string, val int32) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.IntValue(int(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Int64(key string, val int64) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Int64Value(val), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint(key string, val uint) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Uint64Value(uint64(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint8(key string, val uint8) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Uint64Value(uint64(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint16(key string, val uint16) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Uint64Value(uint64(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint32(key string, val uint32) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Uint64Value(uint64(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint64(key string, val uint64) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Uint64Value(val), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Float32(key string, val float32) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Float64Value(float64(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Float64(key string, val float64) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.Float64Value(float64(val)), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) IPAddr(key string, ipAddr net.IP) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.StringValue(ipAddr.String()), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) MACAddr(key string, macAddr net.HardwareAddr) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: key, | ||
Value: slog.StringValue(macAddr.String()), | ||
}) | ||
return c | ||
} | ||
|
||
func (c *Context) Err(err error) types.Context { | ||
c.attrs = append(c.attrs, slog.Attr{ | ||
Key: "error", | ||
Value: slog.StringValue(err.Error()), | ||
}) | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package zerolog | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/loopholelabs/logging/types" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
var _ types.Context = (*Context)(nil) | ||
|
||
type Context struct { | ||
l *Logger | ||
zeroCtx zerolog.Context | ||
} | ||
|
||
func (c *Context) Logger() types.SubLogger { | ||
return &Logger{ | ||
logger: c.zeroCtx.Logger(), | ||
source: c.l.source, | ||
level: c.l.level, | ||
} | ||
} | ||
|
||
func (c *Context) Str(key string, val string) types.Context { | ||
c.zeroCtx = c.zeroCtx.Str(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Bool(key string, val bool) types.Context { | ||
c.zeroCtx = c.zeroCtx.Bool(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Int(key string, val int) types.Context { | ||
c.zeroCtx = c.zeroCtx.Int(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Int8(key string, val int8) types.Context { | ||
c.zeroCtx = c.zeroCtx.Int8(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Int16(key string, val int16) types.Context { | ||
c.zeroCtx = c.zeroCtx.Int16(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Int32(key string, val int32) types.Context { | ||
c.zeroCtx = c.zeroCtx.Int32(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Int64(key string, val int64) types.Context { | ||
c.zeroCtx = c.zeroCtx.Int64(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint(key string, val uint) types.Context { | ||
c.zeroCtx = c.zeroCtx.Uint(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint8(key string, val uint8) types.Context { | ||
c.zeroCtx = c.zeroCtx.Uint8(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint16(key string, val uint16) types.Context { | ||
c.zeroCtx = c.zeroCtx.Uint16(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint32(key string, val uint32) types.Context { | ||
c.zeroCtx = c.zeroCtx.Uint32(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Uint64(key string, val uint64) types.Context { | ||
c.zeroCtx = c.zeroCtx.Uint64(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Float32(key string, val float32) types.Context { | ||
c.zeroCtx = c.zeroCtx.Float32(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Float64(key string, val float64) types.Context { | ||
c.zeroCtx = c.zeroCtx.Float64(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) IPAddr(key string, val net.IP) types.Context { | ||
c.zeroCtx = c.zeroCtx.IPAddr(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) MACAddr(key string, val net.HardwareAddr) types.Context { | ||
c.zeroCtx = c.zeroCtx.MACAddr(key, val) | ||
return c | ||
} | ||
|
||
func (c *Context) Err(err error) types.Context { | ||
c.zeroCtx = c.zeroCtx.Err(err) | ||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.