phuslu / log

Fastest structured logging
MIT License
672 stars 44 forks source link

Single Quotation Mark #33

Closed ogimenezb closed 3 years ago

ogimenezb commented 3 years ago

Given the following example:

var logger = struct {
    Console plog.Logger
    Pretty  plog.Logger
    File    plog.Logger
}{
    Console: plog.Logger{
        Level:  plog.TraceLevel,
        Caller: 1,
        Writer: &plog.IOWriter{os.Stdout},
    },
    Pretty: plog.Logger{
        Level:  plog.TraceLevel,
        Caller: 1,
        Writer: &plog.ConsoleWriter{
            ColorOutput:    true,
            QuoteString:    true,
            EndWithMessage: true,
        },
    },
    File: plog.Logger{
        Level:  plog.TraceLevel,
        Caller: 1,
        Writer: &plog.FileWriter{
            Filename:     "plog_test.log",
            FileMode:     0600,
            MaxSize:      100 * 1024 * 1024,
            MaxBackups:   7,
            EnsureFolder: true,
            LocalTime:    true,
        },
    },
}

func testSpecial() {
    logger.Console.Info().Msg("can't handle single quotation mark")
    logger.Pretty.Info().Msg("can't handle single quotation mark")
    logger.File.Info().Msg("can't handle single quotation mark")
}

I have the following outputs: Console: {"time":"2021-06-12T18:10:42.379+02:00","level":"info","caller":"main.go:233","goid":1,"message":"can\u0027t handle single quotation mark"} Pretty: 2021-06-12T18:10:42.379+02:00 INF 1 main.go:234 > can't handle single quotation mark File: {"time":"2021-06-12T18:10:42.379+02:00","level":"info","caller":"main.go:235","goid":1,"message":"can\u0027t handle single quotation mark"}

It seems it can\u0027t handle it correctly... lol

phuslu commented 3 years ago

By design indeed.

Because we found some software cannot handle ' in nested json. So we escape it in advance.

ogimenezb commented 3 years ago

Ok, will try not to use contractions.

ogimenezb commented 3 years ago

I have just got and error, external no mine with a contraction ' .

Could we add an option not to escape? if not then, well, that's life... lol

phuslu commented 3 years ago

I curious that what "error" you got, by right "\u0027" is valid format for the json string.

ogimenezb commented 3 years ago

Yes, is a valid json string. Sometimes just to be quick I open log on VSCode or Notepad ar whatever editor I have available and reed ir directly.

I suppose ideally I would create a simple app to see logs.

ogimenezb commented 3 years ago

Sorry, one las question. Do you now any good parser / viewer, preferably in html for json log?

phuslu commented 3 years ago

in terminal, I usually use jq to query/view log, for example

cat info.log | jq -r '[time, level, msg] | @csv'

for large amount log files, I usually import to a external database (e.g. Clickhouse, but I know someone use ELK instead) the query/view them with SQL