You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
2.5 KiB

3 years ago
package entry
import (
"fmt"
3 years ago
"strings"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level"
)
const (
defaultCap = 5
)
type Option func(*Entry)
func WithCapacity(c int) Option {
3 years ago
return func(e *Entry) {
e.fields = make(field.Fields, 0, c+1)
3 years ago
}
}
func WithFields(fields ...field.Field) Option {
return func(e *Entry) {
e.fields = fields
}
}
func WithMessage(msg string) Option {
return func(e *Entry) {
e.format = msg
}
}
func WithMessagef(format string, args ...interface{}) Option {
return func(e *Entry) {
e.format = format
e.args = args
3 years ago
}
}
func WithLevel(lvl level.Level) Option {
return func(e *Entry) {
e.level = lvl
}
}
func New(opts ...Option) *Entry {
3 years ago
entry := &Entry{
3 years ago
fields: make(field.Fields, 0, defaultCap+1),
level: level.Debug,
format: "",
args: make([]interface{}, 0, defaultCap+1),
3 years ago
}
for _, opt := range opts {
3 years ago
opt(entry)
3 years ago
}
3 years ago
return entry
3 years ago
}
// Entry slice field.
type Entry struct {
format string
args []interface{}
3 years ago
level level.Level
fields field.Fields
}
func (e *Entry) Reset() {
e.fields = e.fields[:0]
e.args = e.args[:0]
e.format = ""
3 years ago
}
func (e *Entry) Fields() field.Fields {
return e.fields
}
// String implement stringer.
func (e *Entry) String() string {
if e == nil {
return ""
}
str := make([]string, len(e.fields)+1)
str[0] = e.Message()
3 years ago
for i, field := range e.fields {
str[i+1] = field.String()
}
return strings.Join(str, " ")
}
func (e *Entry) Message() string {
switch {
case len(e.args) > 0 && e.format != "":
return fmt.Sprintf(e.format, e.args...)
case len(e.args) > 0:
return fmt.Sprint(e.args...)
default:
return e.format
}
3 years ago
}
func (e *Entry) Level() level.Level {
if e == nil {
return level.Debug
}
return e.level
}
func (e *Entry) SetLevel(level level.Level) *Entry {
if e == nil {
return New().SetLevel(level)
}
e.level = level
return e
}
func (e *Entry) SetMessage(msg string) *Entry {
if e == nil {
return New().SetMessage(msg)
}
e.format = msg
return e
}
func (e *Entry) SetMessagef(format string, args ...interface{}) *Entry {
if e == nil {
return New().SetMessagef(format, args...)
}
e.format = format
e.args = append(e.args[:0], args...)
3 years ago
return e
}
func (e *Entry) Add(fields ...field.Field) *Entry {
if e == nil {
return New(WithFields(fields...))
}
e.fields = e.fields.Append(fields...)
return e
}
func (e *Entry) AddAny(key string, value interface{}) *Entry {
return e.Add(field.Any(key, value))
}
func (e *Entry) AddString(key, value string) *Entry {
return e.Add(field.String(key, value))
}