0

This may be a noop question to ask but since I have no idea what and how to search for, I decided to to ask this to public. Please leave a comment if you think this question needs an improvement or need more information. I'll try to respond and edit as soon as possible.

I have this simple Go program that uses thirty party logging library called zerolog and prints logs.

package main

import (
    "os"
    "time"

    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
)

func init() {
    zerolog.SetGlobalLevel(zerolog.DebugLevel)
    zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
    log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano})
}

func main() {
    log.Debug().Msg("This if debugging message")
    log.Info().Msg("This is information message")
}

If you simply run this program with $ go run main.go, it prints out the following result.

2021-06-23T12:45:29.409Z DBG This if debugging message
2021-06-23T12:45:29.41Z INF This is information message

However, if I use nohup to run this program $ nohup go run main.go &, then it creates an output file called nohup.out and writes different log format. Why is that?

[90m2021-06-23T12:59:37.755Z[0m [33mDBG[0m This if debugging message
[90m2021-06-23T12:59:37.756Z[0m [32mINF[0m This is information message

Can anyone kindly explain to me what format this is and how do I convert this to human readable format like the first result?

James Z
  • 12,209
  • 10
  • 24
  • 44
JayB Kim
  • 327
  • 5
  • 16
  • 2
    It's not a different format, those are ANSI escape sequences for the color codes. – Adrian Jun 23 '21 at 13:11
  • @Adrian Thanks for the quick comment. Then I believe I need to search for how do I do to convert ANSI escape sequences in go. Right? – JayB Kim Jun 23 '21 at 13:14
  • I'm not sure why you'd need to convert them in Go; you need to convert them wherever you're reading `nohup.out`. – Adrian Jun 23 '21 at 13:22
  • 1
    For example, if you're reading the file with `less`, use `less -R` to have it render raw control characters. Instead of seeing the escape sequences in text, you'll see colored output. – Adrian Jun 23 '21 at 13:24
  • I use `nano` to read the already generated `nohup.out` file. It shows the escape sequences in text. But like you said, using `less` to read the file renders the text well. I didn't even know the terminology `escape sequences`. That's great to know. Thanks a lot for your help. – JayB Kim Jun 23 '21 at 13:25

1 Answers1

4

It is using the zerolog ConsoleWriter with color, so what you see in the second output is the escape sequences for colored output. You can turn color off (copied from zerolog examples):


func main() {
    out := zerolog.NewConsoleWriter()
    out.NoColor = true 
    log := zerolog.New(out)

    log.Debug().Str("foo", "bar").Msg("Hello World")
}
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
  • So basically this occurs due to the colorized output. Thank you so much! I wonder if there is a way to convert already generated file. – JayB Kim Jun 23 '21 at 13:19
  • This might help: https://stackoverflow.com/questions/6534556/how-to-remove-and-all-of-the-escape-sequences-in-a-file-using-linux-shell-sc – Burak Serdar Jun 23 '21 at 13:39