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

Confusing error message when attempting to log types which can't be stored in a Metadata::Value #12616

Open
dscottboggs opened this issue Oct 15, 2022 · 1 comment

Comments

@dscottboggs
Copy link
Contributor

dscottboggs commented Oct 15, 2022

Bug Report

Minimal example

playground

require "log"

Log.setup level: :trace

log = Log.for "testing"
log.error &.emit "test", does: {"this": :work?}
# symbol is not compatible              ^^^^^^

Expected behavior

An error message at compilation about the symbol :work? being incompatible with Log::Metadata::Value

Actual behavior

An undesirably vague error message at compile time

In /usr/lib/crystal/log/metadata.cr:202:27

 202 | def initialize(hash : NamedTuple | Hash)
                             ^---------
Error: can't use NamedTuple(T) in unions yet, use a more specific type

with trace

In src/test.cr:8:5

 8 | log.error &.emit "test", does: {"this": :work?}
         ^----
Error: instantiating 'Log#error()'


In src/test.cr:8:5

 8 | log.error &.emit "test", does: {"this": :work?}
         ^----
Error: instantiating 'Log#error()'


In src/test.cr:8:13

 8 | log.error &.emit "test", does: {"this": :work?}
                 ^---
Error: instantiating 'Log::Emitter#emit(String)'


In /usr/lib/crystal/log/main.cr:194:7

 194 | emit(message, kwargs)
       ^---
Error: instantiating 'emit(String, NamedTuple(does: NamedTuple(this: Symbol)))'


In /usr/lib/crystal/log/main.cr:198:55

 198 | Entry.new(@source, @severity, message, Metadata.build(data), @exception)
                                                       ^----
Error: instantiating 'Log::Metadata.class#build(NamedTuple(does: NamedTuple(this: Symbol)))'


In /usr/lib/crystal/log/metadata.cr:60:14

 60 | Metadata.new(nil, value)
               ^--
Error: instantiating 'Log::Metadata.class#new(Nil, NamedTuple(does: NamedTuple(this: Symbol)))'


In /usr/lib/crystal/log/metadata.cr:36:10

 36 | data.setup(parent, entries)
           ^----
Error: instantiating 'Log::Metadata#setup(Nil, NamedTuple(does: NamedTuple(this: Symbol)))'


In /usr/lib/crystal/log/metadata.cr:46:15

 46 | entries.each_with_index do |key, value, i|
              ^--------------
Error: instantiating 'NamedTuple(does: NamedTuple(this: Symbol))#each_with_index()'


In /usr/lib/crystal/named_tuple.cr:567:5

 567 | each do |key, value|
       ^---
Error: instantiating 'each()'


In /usr/lib/crystal/named_tuple.cr:567:5

 567 | each do |key, value|
       ^---
Error: instantiating 'each()'


In /usr/lib/crystal/log/metadata.cr:46:15

 46 | entries.each_with_index do |key, value, i|
              ^--------------
Error: instantiating 'NamedTuple(does: NamedTuple(this: Symbol))#each_with_index()'


In /usr/lib/crystal/log/metadata.cr:47:50

 47 | ptr_entries[i] = {key: key, value: Value.to_metadata_value(value)}
                                               ^----------------
Error: instantiating 'Log::Metadata::Value.class#to_metadata_value(NamedTuple(this: Symbol))'


In /usr/lib/crystal/log/metadata.cr:216:42

 216 | value.is_a?(Value) ? value : Value.new(value)
                                          ^--
Error: instantiating 'Log::Metadata::Value.class#new(NamedTuple(this: Symbol))'


In /usr/lib/crystal/log/metadata.cr:204:12

 204 | hash.each do |key, value|
            ^---
Error: instantiating 'NamedTuple(this: Symbol)#each()'


In /usr/lib/crystal/log/metadata.cr:204:12

 204 | hash.each do |key, value|
            ^---
Error: instantiating 'NamedTuple(this: Symbol)#each()'


In /usr/lib/crystal/log/metadata.cr:205:31

 205 | raw[key.to_s] = Value.to_metadata_value(value)
                             ^----------------
Error: instantiating 'Log::Metadata::Value.class#to_metadata_value(Symbol)'


In /usr/lib/crystal/log/metadata.cr:202:27

 202 | def initialize(hash : NamedTuple | Hash)
                             ^---------
Error: can't use NamedTuple(T) in unions yet, use a more specific type

Other info

Crystal version

$ crystal --version
Crystal 1.6.0 (2022-10-10)

LLVM: 14.0.6
Default target: x86_64-pc-linux-gnu

OS Version

$ uname -sor
Linux 5.19.13-arch1-1 GNU/Linux
@z64
Copy link
Contributor

z64 commented Mar 26, 2023

As a suggestion, I would be totally fine with falling back to calling #inspect on types that aren't a member of logging metadata types to simply stringify them. This is usually what I do anyways when I want to throw some data into logs without wanting to go through structuring it into metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants