Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maximal nested depth #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ slog.SetDefault(logger)
| TimeFormat | Time format for timestamp. | "[15:04:05]" | string |
| NewLineAfterLog | Add blank line after each log | false | bool |
| StringIndentation | Indent \n in strings | false | bool |
| MaxNestedDepth | Maximal nested depth for elements | 20 | uint |
| DebugColor | Color for Debug level | devslog.Blue | devslog.Color (uint) |
| InfoColor | Color for Info level | devslog.Green | devslog.Color (uint) |
| WarnColor | Color for Warn level | devslog.Yellow | devslog.Color (uint) |
Expand Down
20 changes: 18 additions & 2 deletions devslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type Options struct {
// Indent \n in strings
StringIndentation bool

// Maximal nested depth for elements
MaxNestedDepth uint

// Set color for Debug level, default: devslog.Blue
DebugColor Color

Expand Down Expand Up @@ -91,6 +94,10 @@ func NewHandler(out io.Writer, o *Options) *developHandler {
h.opts.TimeFormat = "[15:04:05]"
}

if o.MaxNestedDepth == 0 {
h.opts.MaxNestedDepth = 20
}

h.opts.DebugColor = ensureValidColor(o.DebugColor, Blue)
h.opts.InfoColor = ensureValidColor(o.InfoColor, Green)
h.opts.WarnColor = ensureValidColor(o.WarnColor, Yellow)
Expand All @@ -102,6 +109,7 @@ func NewHandler(out io.Writer, o *Options) *developHandler {
MaxSlicePrintSize: 50,
SortKeys: false,
TimeFormat: "[15:04:05]",
MaxNestedDepth: 20,
DebugColor: Blue,
InfoColor: Green,
WarnColor: Yellow,
Expand Down Expand Up @@ -541,7 +549,11 @@ func (h *developHandler) elementType(t reflect.Type, v reflect.Value, l int, p i
if v.IsNil() {
b = nilString()
} else {
b = h.elementType(t, v.Elem(), l, p)
if l >= int(h.opts.MaxNestedDepth) {
b = atb(v)
} else {
b = h.elementType(t, v.Elem(), l, p)
}
}
case reflect.Float32, reflect.Float64:
b = cs(atb(v.Float()), fgYellow)
Expand All @@ -566,7 +578,11 @@ func (h *developHandler) elementType(t reflect.Type, v reflect.Value, l int, p i
}
case reflect.Interface:
v = reflect.ValueOf(v.Interface())
b = h.elementType(v.Type(), v, l, p)
if l >= int(h.opts.MaxNestedDepth) {
b = atb(v)
} else {
b = h.elementType(v.Type(), v, l, p)
}
default:
b = atb("Unknown type: ")
b = append(b, atb(v.Kind())...)
Expand Down