ozzo-dbx icon indicating copy to clipboard operation
ozzo-dbx copied to clipboard

Formatting of `[]byte` in Query.logSQL()

Open BourgeoisBear opened this issue 6 years ago • 5 comments

I'm not sure what the ins and outs are for everyone here, but a problem I'm encountering with SQL logging in ozzo is that it logs byte slices as raw bytes. For certain values, this will corrupt the terminal when I cat the log files. By changing line 87 from

strings.Replace(string(bs), ...

to

"0x" + hex.EncodeToString(bs)

my logs are far less troublesome.

Any opinions on making this change to ozzo?

BourgeoisBear avatar Mar 22 '18 02:03 BourgeoisBear

Go for it! As the comment buttresses it's only logging and one should really not parse it's outcome to an extend that depends on the content of binary fields.

kPshi avatar Aug 01 '18 08:08 kPshi

@kPshi Any opinion on serialization format? I'm using hex since that's how my query tool displays binary fields. Base64 would be more compact though.

BourgeoisBear avatar Aug 01 '18 08:08 BourgeoisBear

As you seem to be ambitious: how about extracting the logSQL function into an own field (maybe named SQLFormatFunc) of the Query struct? That would allow to change the format per project.

Personally I'd prefer hex as default format, though, as that makes bytes more readable (you see e.g. a zero terminated field within binary data easily). A sophisticated format might be like hexdump -C prints it with non-US-ASCII bytes replaced by periods on the right and hex on the left.

That might be left to individual formatting functions, of course.

kPshi avatar Aug 01 '18 10:08 kPshi

If you're going to extract the func as a field, I'd suggest to set the default to your new format and also prepare a copy of the legacy function if anyone depends on that (what I do not expect, but "should" is none of my favourite words in IT. ;-) )

kPshi avatar Aug 01 '18 10:08 kPshi

Does @qiangxue have an opinion on this?

BourgeoisBear avatar Aug 02 '18 09:08 BourgeoisBear